Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
45 / 45 |
|
100.00% |
14 / 14 |
|
18.92% |
7 / 37 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MaintainWord | |
100.00% |
45 / 45 |
|
100.00% |
14 / 14 |
|
18.92% |
7 / 37 |
|
100.00% |
2 / 2 |
42.11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
44 / 44 |
|
100.00% |
13 / 13 |
|
16.67% |
6 / 36 |
|
100.00% |
1 / 1 |
35.36 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Application\UseCase; |
| 4 | |
| 5 | use App\Dto\Command\MaintainWordCommand; |
| 6 | use App\Dto\Properties\MaintainQualifierProperties; |
| 7 | use App\Dto\Properties\MaintainWordProperties; |
| 8 | use App\Dto\Response\FullWordDto; |
| 9 | use App\Dto\Response\QualifierDto; |
| 10 | use App\Dto\Response\SubjectDto; |
| 11 | use App\Enum\GrammaticalRoleType; |
| 12 | use App\Service\Data\QualifierServiceInterface; |
| 13 | use App\Service\Data\SubjectServiceInterface; |
| 14 | use App\Service\Data\WordServiceInterface; |
| 15 | use Doctrine\ORM\EntityManagerInterface; |
| 16 | |
| 17 | /** |
| 18 | * @author Wilhelm Zwertvaegher |
| 19 | */ |
| 20 | class MaintainWord implements MaintainWordInterface |
| 21 | { |
| 22 | public function __construct( |
| 23 | private readonly WordServiceInterface $wordService, |
| 24 | private readonly SubjectServiceInterface $subjectService, |
| 25 | private readonly QualifierServiceInterface $qualifierService, |
| 26 | private readonly EntityManagerInterface $entityManager, |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | public function __invoke(MaintainWordCommand $maintainWordCommand): FullWordDto |
| 31 | { |
| 32 | $this->entityManager->beginTransaction(); |
| 33 | |
| 34 | // create or update base word |
| 35 | $spec = new MaintainWordProperties( |
| 36 | $maintainWordCommand->getLabel(), |
| 37 | $maintainWordCommand->getGender(), |
| 38 | $maintainWordCommand->getLang(), |
| 39 | $maintainWordCommand->getOffenseLevel(), |
| 40 | $maintainWordCommand->getStatus(), |
| 41 | $maintainWordCommand->isAsSubject(), |
| 42 | $maintainWordCommand->isAsQualifier(), |
| 43 | $maintainWordCommand->getQualifierPosition(), |
| 44 | $maintainWordCommand->getWordId() |
| 45 | ); |
| 46 | $word = $this->wordService->createOrUpdate($spec); |
| 47 | |
| 48 | $this->entityManager->flush(); |
| 49 | |
| 50 | // handle qualifier and / or subject creation/update or removal |
| 51 | if ($spec->isAsQualifier()) { |
| 52 | $qualifier = $this->qualifierService->createOrUpdate( |
| 53 | $word, |
| 54 | new MaintainQualifierProperties( |
| 55 | $spec->getQualifierPosition() |
| 56 | ) |
| 57 | ); |
| 58 | } elseif ($maintainWordCommand->isHandleDeletion()) { |
| 59 | $this->qualifierService->deleteIfExists($word->getId()); |
| 60 | } |
| 61 | if ($spec->isAsSubject()) { |
| 62 | $subject = $this->subjectService->createOrUpdate($word); |
| 63 | } elseif ($maintainWordCommand->isHandleDeletion()) { |
| 64 | $this->subjectService->deleteIfExists($word->getId()); |
| 65 | } |
| 66 | |
| 67 | // now that all entities have been handled, we can flush the entity manager |
| 68 | $this->entityManager->flush(); |
| 69 | $this->entityManager->commit(); |
| 70 | |
| 71 | // and finally, build and return the Dto |
| 72 | $types = []; |
| 73 | if (isset($qualifier)) { |
| 74 | $types[GrammaticalRoleType::QUALIFIER->value] = new QualifierDto($qualifier->getUsageCount(), $qualifier->getPosition()); |
| 75 | } |
| 76 | |
| 77 | if (isset($subject)) { |
| 78 | $types[GrammaticalRoleType::SUBJECT->value] = new SubjectDto($subject->getUsageCount()); |
| 79 | } |
| 80 | |
| 81 | return new FullWordDto( |
| 82 | $word->getId(), |
| 83 | $word->getLabel(), |
| 84 | $word->getSlug(), |
| 85 | $word->getGender(), |
| 86 | $word->getLang(), |
| 87 | $word->getOffenseLevel(), |
| 88 | $word->getStatus(), |
| 89 | $types |
| 90 | ); |
| 91 | } |
| 92 | } |