86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
use ApiPlatform\Symfony\EventListener\EventPriorities;
|
|
use App\Controller\OgBoot\PxeBootFile\PostAction;
|
|
use App\Dto\Output\OrganizationalUnitOutput;
|
|
use App\Entity\Client;
|
|
use App\Entity\OrganizationalUnit;
|
|
use App\Model\OrganizationalUnitTypes;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent;
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
|
final readonly class OrganizationalUnitSubscriber implements EventSubscriberInterface
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
private readonly PostAction $postAction,
|
|
)
|
|
{
|
|
|
|
}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
KernelEvents::VIEW => ['updateNetworkSettings', EventPriorities::POST_WRITE],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function updateNetworkSettings(ViewEvent $event): void
|
|
{
|
|
$organizationalUnitOutput = $event->getControllerResult();
|
|
$method = $event->getRequest()->getMethod();
|
|
|
|
if (!$organizationalUnitOutput instanceof OrganizationalUnitOutput ||
|
|
!in_array($method, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH])) {
|
|
return;
|
|
}
|
|
|
|
/** @var OrganizationalUnit $organizationalUnitEntity */
|
|
$organizationalUnitEntity = $organizationalUnitOutput->getEntity();
|
|
|
|
if ($organizationalUnitEntity->getType() === OrganizationalUnitTypes::CLASSROOM) {
|
|
$this->syncOgBoot($organizationalUnitEntity);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
private function syncOgBoot(OrganizationalUnit $organizationalUnitEntity): void
|
|
{
|
|
$clients = $this->entityManager->getRepository(Client::class)->findClientsByOrganizationalUnitAndDescendants($organizationalUnitEntity->getId(), []);
|
|
|
|
if (empty($clients)) {
|
|
return;
|
|
}
|
|
|
|
/** @var Client $client */
|
|
foreach ($clients as $client) {
|
|
if ($client->getTemplate() === null) {
|
|
continue;
|
|
}
|
|
|
|
$this->postAction->__invoke($client, $client->getTemplate());
|
|
}
|
|
}
|
|
} |