Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
88.89% covered (warning)
88.89%
8 / 9
80.00% covered (warning)
80.00%
4 / 5
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WordFormatter
100.00% covered (success)
100.00%
16 / 16
88.89% covered (warning)
88.89%
8 / 9
80.00% covered (warning)
80.00%
4 / 5
100.00% covered (success)
100.00%
3 / 3
4.13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applyCommonFormat
100.00% covered (success)
100.00%
5 / 5
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
1.12
 format
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Service\Nick;
4
5use App\Dto\Result\FormattedNickWord;
6use App\Entity\GrammaticalRole;
7use App\Enum\GrammaticalRoleType;
8use App\Enum\WordGender;
9use Psr\Container\ContainerInterface;
10use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
11
12/**
13 * @author Wilhelm Zwertvaegher
14 */
15readonly class WordFormatter implements WordFormatterInterface
16{
17    public function __construct(
18        #[AutowireLocator('app.word_rules', indexAttribute: 'index')]
19        private ContainerInterface $wordRules,
20    ) {
21    }
22
23    /**
24     * Apply common formatting on a GeneratedNickWord.
25     */
26    private function applyCommonFormat(FormattedNickWord $formattedNickWord): FormattedNickWord
27    {
28        return new FormattedNickWord(
29            $formattedNickWord->id,
30            trim(ucfirst(strtolower($formattedNickWord->label))),
31            $formattedNickWord->type
32        );
33    }
34
35    public function format(GrammaticalRole $grammaticalRole, WordGender $gender): FormattedNickWord
36    {
37        $word = $grammaticalRole->getWord();
38
39        return $this->applyCommonFormat(
40            $this->wordRules->has($word->getLang()->value) ?
41                $this->wordRules->get($word->getLang()->value)->resolve($grammaticalRole, $gender) :
42                new FormattedNickWord(
43                    $word->getId(),
44                    $word->getLabel(),
45                    GrammaticalRoleType::fromClass($grammaticalRole::class)
46                )
47        );
48    }
49}