ogcore/src/Controller/OgRepository/SyncAction.php

48 lines
1.9 KiB
PHP

<?php
namespace App\Controller\OgRepository;
use App\Entity\Image;
use App\Entity\ImageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AsController]
class SyncAction extends AbstractOgRepositoryController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(ImageRepository $data, HttpClientInterface $httpClient, EntityManagerInterface $entityManager): JsonResponse
{
$content = $this->createRequest($httpClient, 'GET', 'http://'.$data->getIp(). '/ogrepository/v1/images');
if (!isset($content['output']['REPOSITORY']['images'])) {
return new JsonResponse(data: 'No images found', status: Response::HTTP_NOT_FOUND);
}
foreach ($content['output']['REPOSITORY']['images'] as $image) {
$imageEntity = $this->entityManager->getRepository(Image::class)->findOneBy(['imageFullsum' => $image['fullsum']]);
if (!$imageEntity) {
$imageEntity = new Image();
$imageEntity->setName($image['name'].$image['type']);
$imageEntity->setImageFullsum($image['fullsum']);
$imageEntity->setRemotePc(false);
}
$this->entityManager->persist($imageEntity);
}
$this->entityManager->flush();
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}