Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| GetTranslationsFromMenu | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| provideStringTranslations | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| collect | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace Apie\Cms\Translator; |
| 3 | |
| 4 | use Apie\Cms\MenuStructure\MainMenuBuilder; |
| 5 | use Apie\Common\Concerns\ExpandsTranslationWithBoundedContext; |
| 6 | use Apie\Common\MenuStructure\MenuNode; |
| 7 | use Apie\Common\Translator\TranslationStringProviderInterface; |
| 8 | use Apie\Core\Context\ApieContext; |
| 9 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 10 | use Apie\Core\Translator\ValueObjects\MenuHeader; |
| 11 | use Apie\Core\Translator\ValueObjects\TranslationString; |
| 12 | |
| 13 | class GetTranslationsFromMenu implements TranslationStringProviderInterface |
| 14 | { |
| 15 | use ExpandsTranslationWithBoundedContext; |
| 16 | |
| 17 | public function __construct(private readonly MainMenuBuilder $menuBuilder) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | public function provideStringTranslations(ApieContext $apieContext): TranslationStringSet |
| 22 | { |
| 23 | $root = $this->menuBuilder->buildMenu($apieContext); |
| 24 | /** @var array<int, TranslationString|MenuHeader> $alreadyFound */ |
| 25 | $alreadyFound = []; |
| 26 | return new TranslationStringSet( |
| 27 | iterator_to_array( |
| 28 | $this->iterateOverTranslationWithAllResources( |
| 29 | $apieContext, |
| 30 | $this->collect($root, $alreadyFound), |
| 31 | true |
| 32 | ) |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param array<int, TranslationString|MenuHeader>& $alreadyFound |
| 39 | * @return array<int, TranslationString|MenuHeader> |
| 40 | */ |
| 41 | private function collect(MenuNode $menuNode, array& $alreadyFound): array |
| 42 | { |
| 43 | $translation = is_string($menuNode->name) ? new TranslationString($menuNode->name ? : 'apie.menu.home') : $menuNode->name->withoutBoundedContextId()->withoutResourceIdentifier(); |
| 44 | $alreadyFound[] = $translation; |
| 45 | if ($translation instanceof MenuHeader) { |
| 46 | $alreadyFound[] = $translation->withAuthenticated(true); |
| 47 | $alreadyFound[] = $translation->withAuthenticated(false); |
| 48 | } |
| 49 | |
| 50 | foreach ($menuNode->children as $child) { |
| 51 | $this->collect($child, $alreadyFound); |
| 52 | } |
| 53 | |
| 54 | return $alreadyFound; |
| 55 | } |
| 56 | } |