Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
10 / 10 |
|
55.56% |
5 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| NickComposer | |
100.00% |
18 / 18 |
|
100.00% |
10 / 10 |
|
55.56% |
5 / 9 |
|
100.00% |
2 / 2 |
7.19 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compose | |
100.00% |
17 / 17 |
|
100.00% |
9 / 9 |
|
50.00% |
4 / 8 |
|
100.00% |
1 / 1 |
6.00 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Nick; |
| 4 | |
| 5 | use App\Dto\Result\ComposedNick; |
| 6 | use App\Entity\GrammaticalRole; |
| 7 | use App\Entity\Qualifier; |
| 8 | use App\Entity\Subject; |
| 9 | use App\Enum\Lang; |
| 10 | use App\Enum\QualifierPosition; |
| 11 | use App\Enum\WordGender; |
| 12 | use Psr\Container\ContainerInterface; |
| 13 | use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; |
| 14 | |
| 15 | /** |
| 16 | * Nick composer. |
| 17 | * |
| 18 | * @author Wilhelm Zwertvaegher |
| 19 | */ |
| 20 | readonly class NickComposer implements NickComposerInterface |
| 21 | { |
| 22 | public function __construct( |
| 23 | #[AutowireLocator('app.composer_rules', indexAttribute: 'index')] |
| 24 | private ContainerInterface $composerRules, |
| 25 | private WordFormatterInterface $wordFormatter, |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Build an actual Nick from a Subject, Qualifier, lang and gender |
| 31 | * In this case, put words in the right order, delegate words formatting, and applying specific rules if available. |
| 32 | */ |
| 33 | public function compose(Subject $subject, Qualifier $qualifier, Lang $lang, WordGender $targetGender): ComposedNick |
| 34 | { |
| 35 | // put grammatical roles in the right order |
| 36 | $grammaticalRoles = [$subject]; |
| 37 | if (QualifierPosition::AFTER === $qualifier->getPosition()) { |
| 38 | $grammaticalRoles[] = $qualifier; |
| 39 | } else { |
| 40 | array_unshift($grammaticalRoles, $qualifier); |
| 41 | } |
| 42 | |
| 43 | // get an array of formated GeneratedNickWord |
| 44 | $formattedWords = array_map( |
| 45 | fn (GrammaticalRole $grammaticalRole) => $this->wordFormatter->format($grammaticalRole, $targetGender), |
| 46 | $grammaticalRoles |
| 47 | ); |
| 48 | |
| 49 | // assemble formated GeneratedNickWord and metadata (gender and offense level) |
| 50 | $composedNick = new ComposedNick( |
| 51 | $targetGender, |
| 52 | $subject->getWord()->getOffenseLevel(), |
| 53 | $formattedWords |
| 54 | ); |
| 55 | |
| 56 | // apply composing rules if available before returning |
| 57 | $composer = $this->composerRules->has($lang->value) ? $this->composerRules->get($lang->value) : null; |
| 58 | if ($composer) { |
| 59 | return $composer->apply($composedNick, $targetGender); |
| 60 | } |
| 61 | |
| 62 | return $composedNick; |
| 63 | } |
| 64 | } |