Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
96.55% |
28 / 29 |
|
91.67% |
11 / 12 |
|
75.00% |
6 / 8 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WordService | |
96.55% |
28 / 29 |
|
91.67% |
11 / 12 |
|
75.00% |
6 / 8 |
|
75.00% |
3 / 4 |
7.77 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getByLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createOrUpdate | |
96.15% |
25 / 26 |
|
88.89% |
8 / 9 |
|
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
5.02 | |||
| save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Data; |
| 4 | |
| 5 | use App\Dto\Properties\MaintainWordProperties; |
| 6 | use App\Entity\Word; |
| 7 | use App\Exception\WordNotFoundException; |
| 8 | use App\Repository\WordRepositoryInterface; |
| 9 | use Doctrine\ORM\EntityManagerInterface; |
| 10 | use Psr\Clock\ClockInterface; |
| 11 | |
| 12 | /** |
| 13 | * @author Wilhelm Zwertvaegher |
| 14 | */ |
| 15 | class WordService implements WordServiceInterface |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly WordRepositoryInterface $wordRepository, |
| 19 | private readonly EntityManagerInterface $entityManager, |
| 20 | private readonly WordSluggerInterface $slugger, |
| 21 | private readonly ClockInterface $clock, |
| 22 | ) { |
| 23 | } |
| 24 | |
| 25 | public function getByLabel(string $label): ?Word |
| 26 | { |
| 27 | return $this->wordRepository->findBySlug($this->slugger->slug($label)); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @throws WordNotFoundException |
| 32 | */ |
| 33 | public function createOrUpdate(MaintainWordProperties $spec): Word |
| 34 | { |
| 35 | $slug = $this->slugger->slug($spec->getLabel()); |
| 36 | if ($id = $spec->getWordId()) { |
| 37 | $word = $this->wordRepository->findById($id); |
| 38 | if (!$word) { |
| 39 | throw new WordNotFoundException(); |
| 40 | } |
| 41 | } else { |
| 42 | $word = $this->wordRepository->findBySlug($slug); |
| 43 | } |
| 44 | |
| 45 | $label = ucwords(strtolower($spec->getLabel())); |
| 46 | |
| 47 | if ($word) { |
| 48 | $word->setSlug($slug); |
| 49 | $word->setLabel($label); |
| 50 | $word->setGender($spec->getGender()); |
| 51 | $word->setLang($spec->getLang()); |
| 52 | $word->setOffenseLevel($spec->getOffenseLevel()); |
| 53 | $word->setStatus($spec->getStatus()); |
| 54 | } else { |
| 55 | $word = new Word( |
| 56 | slug: $slug, |
| 57 | label: $label, |
| 58 | gender: $spec->getGender(), |
| 59 | lang: $spec->getLang(), |
| 60 | offenseLevel: $spec->getOffenseLevel(), |
| 61 | status: $spec->getStatus(), |
| 62 | createdAt: $now = $this->clock->now(), |
| 63 | updatedAt: $now, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | $this->save($word); |
| 68 | |
| 69 | return $word; |
| 70 | } |
| 71 | |
| 72 | public function save(Word $word): void |
| 73 | { |
| 74 | $this->entityManager->persist($word); |
| 75 | } |
| 76 | } |