* image upload handling
* old static image migrator implementation as cli command * auth id fix, renew works now * templates refactored to work with uploaded images
This commit is contained in:
66
src/App/Command/MigrateImages.php
Normal file
66
src/App/Command/MigrateImages.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use App\Service\JudgeManager;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class MigrateImages extends Command
|
||||
{
|
||||
/** @var AwardeeManager */
|
||||
private $awardeeManager;
|
||||
|
||||
/** @var JudgeManager */
|
||||
private $judgeManager;
|
||||
|
||||
public function __construct(AwardeeManager $awardeeManager, JudgeManager $judgeManager)
|
||||
{
|
||||
$this->awardeeManager = $awardeeManager;
|
||||
$this->judgeManager = $judgeManager;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('image:migrate')
|
||||
->setDescription('Migrate images from old format to new');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->write("Migrating awardee images ...");
|
||||
$awardees = $this->awardeeManager->getAwardees();
|
||||
$this->awardeeManager->ensureImageDirectoryExists();
|
||||
foreach ($awardees as $awardee) {
|
||||
if ($awardee->hasLegacyProfileImage() && !$awardee->hasProfileImage()) {
|
||||
link(
|
||||
sprintf('public/img/awardees/%s.jpg', $awardee->getSlug()),
|
||||
sprintf('%s/%s-profile.jpg',AwardeeManager::IMAGE_DIRECTORY, $awardee->getId())
|
||||
);
|
||||
}
|
||||
if ($awardee->hasLegacyAwardImage() && !$awardee->hasAwardImage()) {
|
||||
link(
|
||||
sprintf('public/img/awardees/%s-atadas.jpg', $awardee->getSlug()),
|
||||
sprintf('%s/%s-award.jpg',AwardeeManager::IMAGE_DIRECTORY, $awardee->getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
$output->writeln("done.");
|
||||
|
||||
$output->write("Migrating judge images ...");
|
||||
$judges = $this->judgeManager->getJudges();
|
||||
$this->judgeManager->ensureImageDirectoryExists();
|
||||
foreach ($judges as $judge) {
|
||||
if ($judge->hasLegacyProfileImage() && !$judge->hasProfileImage()) {
|
||||
link(
|
||||
sprintf('public/img/judges/%s.jpg', $judge->getSlug()),
|
||||
sprintf('%s/%s.jpg',JudgeManager::IMAGE_DIRECTORY, $judge->getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
$output->writeln("done.");
|
||||
}
|
||||
}
|
||||
21
src/App/Command/MigrateImagesFactory.php
Normal file
21
src/App/Command/MigrateImagesFactory.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use App\Service\JudgeManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class MigrateImagesFactory
|
||||
{
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return MigrateImages
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container): MigrateImages
|
||||
{
|
||||
$awardeeManager = $container->get(AwardeeManager::class);
|
||||
$judgeManager = $container->get(JudgeManager::class);
|
||||
return new MigrateImages($awardeeManager, $judgeManager);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ class ConfigProvider
|
||||
return [
|
||||
'dependencies' => $this->getDependencies(),
|
||||
'templates' => $this->getTemplates(),
|
||||
'console' => $this->getConsoleCommands(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -36,6 +37,8 @@ class ConfigProvider
|
||||
Handler\PingHandler::class => Handler\PingHandler::class,
|
||||
],
|
||||
'factories' => [
|
||||
Command\MigrateImages::class => Command\MigrateImagesFactory::class,
|
||||
|
||||
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
|
||||
Handler\ArticleHandler::class => Handler\ArticleHandlerFactory::class,
|
||||
Handler\AwardeeHandler::class => Handler\AwardeeHandlerFactory::class,
|
||||
@@ -45,7 +48,9 @@ class ConfigProvider
|
||||
Handler\PrizeRedirectHandler::class => Handler\PrizeRedirectHandlerFactory::class,
|
||||
|
||||
Handler\Api\AwardeeHandler::class => Handler\Api\AwardeeHandlerFactory::class,
|
||||
Handler\Api\AwardeeImageHandler::class => Handler\Api\AwardeeImageHandlerFactory::class,
|
||||
Handler\Api\JudgesHandler::class => Handler\Api\JudgesHandlerFactory::class,
|
||||
Handler\Api\JudgeImageHandler::class => Handler\Api\JudgeImageHandlerFactory::class,
|
||||
Handler\Api\YearsHandler::class => Handler\Api\YearsHandlerFactory::class,
|
||||
|
||||
Plates\StringExtension::class => Plates\StringExtensionFactory::class,
|
||||
@@ -71,4 +76,13 @@ class ConfigProvider
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getConsoleCommands(): array
|
||||
{
|
||||
return [
|
||||
'commands' => [
|
||||
Command\MigrateImages::class,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Gedmo\Mapping\Annotation as Gedmo;
|
||||
use JsonSerializable;
|
||||
@@ -15,6 +16,7 @@ use JsonSerializable;
|
||||
* @ORM\Index(name="a_name_idx", columns={"name"})
|
||||
* }
|
||||
* )
|
||||
* @ORM\HasLifecycleCallbacks
|
||||
*/
|
||||
class Awardee implements JsonSerializable
|
||||
{
|
||||
@@ -45,7 +47,7 @@ class Awardee implements JsonSerializable
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="image_label", type="string", length=255, unique=true)
|
||||
* @ORM\Column(name="image_label", type="string", length=255)
|
||||
* @var string
|
||||
*/
|
||||
private $imageLabel;
|
||||
@@ -165,6 +167,51 @@ class Awardee implements JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasProfileImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('%s/%s-profile.jpg', AwardeeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLegacyProfileImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('public/img/awardees/%s.jpg', $this->getSlug()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAwardImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('%s/%s-award.jpg', AwardeeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLegacyAwardImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('public/img/awardees/%s-atadas.jpg', $this->getSlug()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @ORM\PreRemove
|
||||
*/
|
||||
public function cleanUpImagesBeforeDelete()
|
||||
{
|
||||
if ($this->hasProfileImage()) {
|
||||
unlink(sprintf('%s/%s-profile.jpg', AwardeeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
if ($this->hasAwardImage()) {
|
||||
unlink(sprintf('%s/%s-award.jpg', AwardeeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -177,6 +224,8 @@ class Awardee implements JsonSerializable
|
||||
'text' => $this->getText(),
|
||||
'imageLabel' => $this->getImageLabel(),
|
||||
'slug' => $this->getSlug(),
|
||||
'hasProfileImage' => $this->hasProfileImage(),
|
||||
'hasAwardImage' => $this->hasAwardImage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Service\JudgeManager;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
@@ -17,6 +18,7 @@ use JsonSerializable;
|
||||
* @ORM\Index(name="j_name_idx", columns={"name"})
|
||||
* }
|
||||
* )
|
||||
* @ORM\HasLifecycleCallbacks
|
||||
*/
|
||||
class Judge implements JsonSerializable
|
||||
{
|
||||
@@ -198,6 +200,26 @@ class Judge implements JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasProfileImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('%s/%s.jpg', JudgeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
|
||||
public function hasLegacyProfileImage(): bool
|
||||
{
|
||||
return file_exists(sprintf('public/img/judges/%s.jpg', $this->getSlug()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @ORM\PreRemove
|
||||
*/
|
||||
public function cleanUpImagesBeforeDelete()
|
||||
{
|
||||
if ($this->hasProfileImage()) {
|
||||
unlink(sprintf('%s/%s.jpg', JudgeManager::IMAGE_DIRECTORY, $this->getId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -209,6 +231,7 @@ class Judge implements JsonSerializable
|
||||
'prefix' => $this->getPrefix(),
|
||||
'titles' => $this->getTitles()->getValues(),
|
||||
'slug' => $this->getSlug(),
|
||||
'hasProfileImage' => $this->hasProfileImage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
68
src/App/Handler/Api/AwardeeImageHandler.php
Normal file
68
src/App/Handler/Api/AwardeeImageHandler.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use UtilityModule\Handler\AbstractCrudHandler;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Diactoros\Response\TextResponse;
|
||||
|
||||
class AwardeeImageHandler extends AbstractCrudHandler
|
||||
{
|
||||
const IDENTIFIER_NAME = 'slug';
|
||||
|
||||
/** @var AwardeeManager */
|
||||
private $awardeeManager;
|
||||
|
||||
public function __construct(
|
||||
AwardeeManager $awardeeManager
|
||||
) {
|
||||
$this->awardeeManager = $awardeeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function get(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$type = $request->getAttribute('type');
|
||||
$imageResponse = new TextResponse($this->awardeeManager->getProfileImage($slug, $type));
|
||||
return $imageResponse
|
||||
->withHeader('Content-Type', 'image/jpeg')
|
||||
->withHeader('Cache-Control', 'must-revalidate')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function create(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$type = $request->getAttribute('type');
|
||||
/** @var UploadedFileInterface[] $files */
|
||||
$files = $request->getUploadedFiles();
|
||||
$imageData = $files['image']->getStream()->getContents();
|
||||
$entity = $this->awardeeManager->setProfileImage($slug, $imageData, $type);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function delete(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$type = $request->getAttribute('type');
|
||||
return new JsonResponse($this->awardeeManager->deleteProfileImage($slug, $type));
|
||||
}
|
||||
}
|
||||
18
src/App/Handler/Api/AwardeeImageHandlerFactory.php
Normal file
18
src/App/Handler/Api/AwardeeImageHandlerFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AwardeeImageHandlerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container): RequestHandlerInterface
|
||||
{
|
||||
$awardeeManager = $container->get(AwardeeManager::class);
|
||||
return new AwardeeImageHandler($awardeeManager);
|
||||
}
|
||||
}
|
||||
65
src/App/Handler/Api/JudgeImageHandler.php
Normal file
65
src/App/Handler/Api/JudgeImageHandler.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\JudgeManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use UtilityModule\Handler\AbstractCrudHandler;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Diactoros\Response\TextResponse;
|
||||
|
||||
class JudgeImageHandler extends AbstractCrudHandler
|
||||
{
|
||||
const IDENTIFIER_NAME = 'slug';
|
||||
|
||||
/** @var JudgeManager */
|
||||
private $judgeManager;
|
||||
|
||||
public function __construct(
|
||||
JudgeManager $judgeManager
|
||||
) {
|
||||
$this->judgeManager = $judgeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function get(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$imageResponse = new TextResponse($this->judgeManager->getProfileImage($slug));
|
||||
return $imageResponse
|
||||
->withHeader('Content-Type', 'image/jpeg')
|
||||
->withHeader('Cache-Control', 'must-revalidate')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function create(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
/** @var UploadedFileInterface[] $files */
|
||||
$files = $request->getUploadedFiles();
|
||||
$imageData = $files['image']->getStream()->getContents();
|
||||
$entity = $this->judgeManager->setProfileImage($slug, $imageData);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function delete(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$slug = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
return new JsonResponse($this->judgeManager->deleteProfileImage($slug));
|
||||
}
|
||||
}
|
||||
18
src/App/Handler/Api/JudgeImageHandlerFactory.php
Normal file
18
src/App/Handler/Api/JudgeImageHandlerFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\JudgeManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class JudgeImageHandlerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container): RequestHandlerInterface
|
||||
{
|
||||
$judgeManager = $container->get(JudgeManager::class);
|
||||
return new JudgeImageHandler($judgeManager);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ use UtilityModule\Hydrator\DoctrineObject;
|
||||
|
||||
class AwardeeManager
|
||||
{
|
||||
const IMAGE_DIRECTORY = 'data/user-data/images/awardee';
|
||||
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
@@ -24,6 +26,9 @@ class AwardeeManager
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Awardee[]|null
|
||||
*/
|
||||
public function getAwardees(): ?array
|
||||
{
|
||||
return $this->entityManager->getRepository(Awardee::class)->findBy([], [
|
||||
@@ -42,6 +47,10 @@ class AwardeeManager
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Awardee|null
|
||||
*/
|
||||
public function getAwardee(int $id): ?Awardee
|
||||
{
|
||||
/** @var Awardee $awardee */
|
||||
@@ -63,12 +72,14 @@ class AwardeeManager
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return Awardee
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function create($data): Awardee
|
||||
{
|
||||
unset($data['slug']);
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, new Awardee());
|
||||
$this->entityManager->persist($awardee);
|
||||
@@ -85,6 +96,7 @@ class AwardeeManager
|
||||
*/
|
||||
public function update(int $id, $data): Awardee
|
||||
{
|
||||
unset($data['slug']);
|
||||
$awardee = $this->entityManager->getRepository(Awardee::class)->find($id);
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, $awardee);
|
||||
@@ -99,7 +111,8 @@ class AwardeeManager
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function delete(int $id): bool {
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
if (null !== ($entity = $this->getAwardee($id))) {
|
||||
$this->entityManager->remove($entity);
|
||||
$this->entityManager->flush();
|
||||
@@ -107,4 +120,69 @@ class AwardeeManager
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public function getProfileImage(string $slug, string $type): string
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
if (file_exists($fileName)) {
|
||||
return file_get_contents($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException("$fileName not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $imageData
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function setProfileImage(string $slug, string $imageData, string $type): bool
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$this->ensureImageDirectoryExists();
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
return false !== file_put_contents($fileName, $imageData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteProfileImage(string $slug, string $type): bool
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
if (file_exists($fileName)) {
|
||||
return unlink($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function ensureImageDirectoryExists()
|
||||
{
|
||||
if (!is_dir(self::IMAGE_DIRECTORY)) {
|
||||
mkdir(self::IMAGE_DIRECTORY, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
private function getImageFileName(int $id, string $type): string
|
||||
{
|
||||
return sprintf('%s/%s-%s.jpg', self::IMAGE_DIRECTORY, $id, $type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ use UtilityModule\Hydrator\DoctrineObject;
|
||||
|
||||
class JudgeManager
|
||||
{
|
||||
const IMAGE_DIRECTORY = 'data/user-data/images/judge';
|
||||
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
@@ -24,7 +26,10 @@ class JudgeManager
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getJudges()
|
||||
/**
|
||||
* @return Judge[]|null
|
||||
*/
|
||||
public function getJudges(): ?array
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j, t')
|
||||
@@ -32,14 +37,14 @@ class JudgeManager
|
||||
->leftJoin('j.titles', 't')
|
||||
->orderBy('j.name', 'ASC')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return array
|
||||
* @return Judge[]|null
|
||||
*/
|
||||
public function getJudgesByYear(int $year)
|
||||
public function getJudgesByYear(int $year): ?array
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j,t')
|
||||
@@ -49,7 +54,7 @@ class JudgeManager
|
||||
->orderBy('j.name', 'ASC')
|
||||
->setParameter('year', $year)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,6 +76,7 @@ class JudgeManager
|
||||
*/
|
||||
public function create($data): Judge
|
||||
{
|
||||
unset($data['slug']);
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, new Judge());
|
||||
$this->entityManager->persist($judge);
|
||||
@@ -87,6 +93,7 @@ class JudgeManager
|
||||
*/
|
||||
public function update(int $id, $data): Judge
|
||||
{
|
||||
unset($data['slug']);
|
||||
$judge = $this->entityManager->getRepository(Judge::class)->find($id);
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, $judge);
|
||||
@@ -110,4 +117,66 @@ class JudgeManager
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return string
|
||||
*/
|
||||
public function getProfileImage(string $slug): string
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
if (file_exists($fileName)) {
|
||||
return file_get_contents($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $imageData
|
||||
* @return bool
|
||||
*/
|
||||
public function setProfileImage(string $slug, string $imageData): bool
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$this->ensureImageDirectoryExists();
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
return false !== file_put_contents($fileName, $imageData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteProfileImage(string $slug): bool
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
if (file_exists($fileName)) {
|
||||
return unlink($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function ensureImageDirectoryExists()
|
||||
{
|
||||
if (!is_dir(self::IMAGE_DIRECTORY)) {
|
||||
mkdir(self::IMAGE_DIRECTORY, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
private function getImageFileName(int $id): string
|
||||
{
|
||||
return sprintf('%s/%s.jpg', self::IMAGE_DIRECTORY, $id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user