Skip to content

Commit

Permalink
Ensure logo path is present when loading teams
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Dec 9, 2023
1 parent d99ce00 commit 83fb441
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 64 deletions.
6 changes: 6 additions & 0 deletions src/Infrastructure/API/GraphQL/TeamType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function __construct()
],
'contact' => [
'type' => ContactType::getInstance()
],
'logo_id' => [
'type' => Type::string(),
],
'logo_path' => [
'type' => Type::string()
]
];
}
Expand Down
9 changes: 6 additions & 3 deletions src/Infrastructure/API/Logos/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

use HexagonalPlayground\Application\Repository\TeamRepositoryInterface;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\Filesystem\TeamLogoRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

class DeleteAction implements ActionInterface
{
use TeamLogoTrait;
use TeamFinderTrait;
private TeamLogoRepository $teamLogoRepository;
private LoggerInterface $logger;

public function __construct(TeamRepositoryInterface $teamRepository, LoggerInterface $logger)
public function __construct(TeamRepositoryInterface $teamRepository, TeamLogoRepository $teamLogoRepository, LoggerInterface $logger)
{
$this->teamRepository = $teamRepository;
$this->teamLogoRepository = $teamLogoRepository;
$this->logger = $logger;
}

Expand All @@ -25,7 +28,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$team = $this->findTeam($request->getQueryParams());

if ($team->getLogoId() !== null) {
$this->deleteLogo($team->getLogoId());
$this->teamLogoRepository->delete($team->getLogoId());
$team->setLogoId(null);
$this->teamRepository->save($team);
$this->teamRepository->flush();
Expand Down
14 changes: 9 additions & 5 deletions src/Infrastructure/API/Logos/GetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

use HexagonalPlayground\Application\Repository\TeamRepositoryInterface;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\Filesystem\TeamLogoRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class GetAction implements ActionInterface
{
use TeamLogoTrait;
use TeamFinderTrait;

public function __construct(TeamRepositoryInterface $teamRepository)
private TeamLogoRepository $teamLogoRepository;

public function __construct(TeamRepositoryInterface $teamRepository, TeamLogoRepository $teamLogoRepository)
{
$this->teamRepository = $teamRepository;
$this->teamLogoRepository = $teamLogoRepository;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
Expand All @@ -25,8 +29,8 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $response->withStatus(404);
}

return $response
->withStatus(302)
->withHeader('Location', $this->buildPublicPath($team->getLogoId()));
$location = $this->teamLogoRepository->generatePublicPath($team->getLogoId());

return $response->withStatus(302)->withHeader('Location', $location);
}
}
23 changes: 23 additions & 0 deletions src/Infrastructure/API/Logos/TeamFinderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace HexagonalPlayground\Infrastructure\API\Logos;

use HexagonalPlayground\Application\Repository\TeamRepositoryInterface;
use HexagonalPlayground\Application\TypeAssert;
use HexagonalPlayground\Domain\Team;

trait TeamFinderTrait
{
private TeamRepositoryInterface $teamRepository;

private function findTeam(array $queryParams): Team
{
TypeAssert::assertString($queryParams['teamId'], 'teamId');

/** @var Team $team */
$team = $this->teamRepository->find($queryParams['teamId']);

return $team;
}
}
50 changes: 0 additions & 50 deletions src/Infrastructure/API/Logos/TeamLogoTrait.php

This file was deleted.

11 changes: 7 additions & 4 deletions src/Infrastructure/API/Logos/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
use HexagonalPlayground\Domain\Exception\InternalException;
use HexagonalPlayground\Domain\Exception\InvalidInputException;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\Filesystem\TeamLogoRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Log\LoggerInterface;

class UploadAction implements ActionInterface
{
use TeamLogoTrait;
use TeamFinderTrait;
private TeamLogoRepository $teamLogoRepository;
private LoggerInterface $logger;

public function __construct(TeamRepositoryInterface $teamRepository, LoggerInterface $logger)
public function __construct(TeamRepositoryInterface $teamRepository, TeamLogoRepository $teamLogoRepository, LoggerInterface $logger)
{
$this->teamRepository = $teamRepository;
$this->teamLogoRepository = $teamLogoRepository;
$this->logger = $logger;
}

Expand All @@ -41,10 +44,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$this->checkUploadedFile($file);

if ($team->getLogoId() !== null) {
$this->deleteLogo($team->getLogoId());
$this->teamLogoRepository->delete($team->getLogoId());
}

$fileId = $this->saveLogo($file);
$fileId = $this->teamLogoRepository->save($file);
$team->setLogoId($fileId);
$this->teamRepository->save($team);
$this->teamRepository->flush();
Expand Down
35 changes: 35 additions & 0 deletions src/Infrastructure/Filesystem/TeamLogoRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace HexagonalPlayground\Infrastructure\Filesystem;

use HexagonalPlayground\Domain\Util\Uuid;
use HexagonalPlayground\Infrastructure\Config;
use Psr\Http\Message\UploadedFileInterface;

class TeamLogoRepository
{
public function delete(string $logoId): void
{
unlink($this->generateStoragePath($logoId));
}

public function generatePublicPath(string $logoId): string
{
return join('/', [Config::getInstance()->appLogosPublicPath, "$logoId.webp"]);
}

private function generateStoragePath(string $logoId): string
{
return join(DIRECTORY_SEPARATOR, [Config::getInstance()->appLogosPath, "$logoId.webp"]);
}

public function save(UploadedFileInterface $uploadedFile): string
{
$logoId = Uuid::create();

$uploadedFile->moveTo($this->generateStoragePath($logoId));

return $logoId;
}
}
2 changes: 1 addition & 1 deletion src/Infrastructure/Persistence/Read/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function hydrateMany(iterable $rows, ?string $groupBy = null): array
* @param array $row
* @return array
*/
private function hydrate(array $row): array
protected function hydrate(array $row): array
{
$result = [];

Expand Down
27 changes: 27 additions & 0 deletions src/Infrastructure/Persistence/Read/TeamHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace HexagonalPlayground\Infrastructure\Persistence\Read;

use HexagonalPlayground\Infrastructure\Filesystem\TeamLogoRepository;

class TeamHydrator extends Hydrator
{
private TeamLogoRepository $teamLogoRepository;

public function __construct(iterable $fields, TeamLogoRepository $teamLogoRepository)
{
parent::__construct($fields);
$this->teamLogoRepository = $teamLogoRepository;
}

protected function hydrate(array $row): array
{
$team = parent::hydrate($row);
if ($team['logo_id'] !== null) {
$team['logo_path'] = $this->teamLogoRepository->generatePublicPath($team['logo_id']);
}

return $team;
}
}
10 changes: 9 additions & 1 deletion src/Infrastructure/Persistence/Read/TeamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace HexagonalPlayground\Infrastructure\Persistence\Read;

use HexagonalPlayground\Infrastructure\Filesystem\TeamLogoRepository;
use HexagonalPlayground\Infrastructure\Persistence\Read\Criteria\EqualityFilter;
use HexagonalPlayground\Infrastructure\Persistence\Read\Criteria\Filter;
use HexagonalPlayground\Infrastructure\Persistence\Read\Field\DateTimeField;
Expand All @@ -11,6 +12,12 @@

class TeamRepository extends AbstractRepository
{
public function __construct(ReadDbGatewayInterface $gateway, TeamLogoRepository $teamLogoRepository)
{
parent::__construct($gateway);
$this->hydrator = new TeamHydrator($this->getFieldDefinitions(), $teamLogoRepository);
}

protected function getTableName(): string
{
return 'teams';
Expand All @@ -27,7 +34,8 @@ protected function getFieldDefinitions(): array
new StringField('first_name', false),
new StringField('last_name', false),
new StringField('phone', false)
])
]),
new StringField('logo_id', true)
];
}

Expand Down

0 comments on commit 83fb441

Please sign in to comment.