Skip to content

Commit

Permalink
feat(category): keep wip
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wborn committed Sep 13, 2023
1 parent 6425a93 commit 6613197
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Helper/ApiMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class ApiMessages
public const INDEX_STATUS = 'status';
public const INDEX_SUCCESS = 'success';
public const INDEX_WARNING = 'warning';
public const LISTING_ERROR = 'Une erreur est survenue durant la récupération du listing';
public const MESSAGE_OK = 'OK';
public const PROJECT_CREATE_ERROR_MESSAGE = 'An error occurred while persisting project';
public const PROJECT_CREATE_SUCCESS_MESSAGE = 'Le project a bien été créé';
Expand Down
36 changes: 36 additions & 0 deletions src/Service/Category/CategoryDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Service\Category;

final class CategoryDTO
{
public function __construct(
private string $name = '',
private ?string $slug = null,
) {
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getSlug(): ?string
{
return $this->slug;
}

public function setSlug(string $slug): self
{
$this->slug = $slug;

return $this;
}
}
41 changes: 41 additions & 0 deletions src/Service/Category/CategoryFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Service\Category;

use App\Entity\Category;
use App\Entity\Project;
use App\Exception\NotFoundException;
use App\Helper\ApiMessages;
use App\Helper\ApiResponse;
use App\Helper\ExceptionLogger;
use App\Repository\CategoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface;

final class CategoryFinder
{
public function __construct(
private ExceptionLogger $logger,
private EntityManagerInterface $em,
private CategoryRepository $categoryRepository,
) {
}

public function getAllFromProject(?Project $project)
{
try {
!$project && throw new NotFoundException(ApiMessages::PROJECT_UNKNOWN);
$data = CategoryHelper::findNotArchivedFromProject($project);
$result = array_map(fn (Category $category) => CategoryMapper::fromEntityToJson($category), $data);
$response = ApiResponse::createAndFormat($result);
} catch (ExceptionInterface|NotFoundException $exception) {
$this->logger->logNotice($exception);
$response = ApiResponse::createWarningMessage(ApiMessages::LISTING_ERROR);
} catch (\Exception $exception) {
$this->logger->logCriticalAndTrace($exception);
$response = ApiResponse::createErrorMessage(ApiMessages::DEFAULT_ERROR_MESSAGE, exception: $exception);
}

return $response;
}
}
10 changes: 10 additions & 0 deletions src/Service/Category/CategoryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Service\Category;

final class CategoryHandler
{
public function __construct(
) {
}
}
21 changes: 21 additions & 0 deletions src/Service/Category/CategoryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Service\Category;

use App\Entity\Category;
use App\Entity\Project;

final class CategoryHelper
{
public function __construct(
) {
}

public static function findNotArchivedFromProject(Project $project): array
{
return array_map(
static fn (Category $category) => !$category->isArchived() ? $category : null,
$project->getCategories()->toArray()
);
}
}
29 changes: 29 additions & 0 deletions src/Service/Category/CategoryMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Service\Category;

use App\Entity\Category;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

final class CategoryMapper
{
/** @throws ExceptionInterface */
public static function fromEntityToJson(Category $category): mixed
{
$dto = self::fromEntityToDTO($category);
$serializer = new Serializer([new ObjectNormalizer()]);

return $serializer->normalize($dto, JsonEncoder::FORMAT);
}

public static function fromEntityToDTO(Category $category): CategoryDTO
{
return new CategoryDTO(
$category->getName(),
$category->getSlug(),
);
}
}
10 changes: 10 additions & 0 deletions src/Service/Category/CategoryPersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Service\Category;

final class CategoryPersister
{
public function __construct(
) {
}
}
10 changes: 10 additions & 0 deletions src/Service/Category/CategoryValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Service\Category;

final class CategoryValidator
{
public function __construct(
) {
}
}

0 comments on commit 6613197

Please sign in to comment.