-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
) { | ||
} | ||
} |