Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReadAttributeTranslationProvider
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
7.27
0.00% covered (danger)
0.00%
0 / 1
 provideStringTranslations
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
7.27
1<?php
2namespace Apie\Common\Translator;
3
4use Apie\Core\Attributes\ProvideTranslationMethod;
5use Apie\Core\BoundedContext\BoundedContextHashmap;
6use Apie\Core\Context\ApieContext;
7use Apie\Core\Translator\Lists\TranslationStringSet;
8use Apie\Core\Translator\ValueObjects\AbstractTranslation;
9
10/**
11 * Reads ProvideTranslationMethod attribute to add resources
12 *
13 * @see ProvideTranslationMethod
14 */
15class ReadAttributeTranslationProvider implements TranslationStringProviderInterface
16{
17    public function provideStringTranslations(ApieContext $apieContext): TranslationStringSet
18    {
19        $items = [];
20        $boundedContextHashmap = $apieContext->getContext(BoundedContextHashmap::class, false);
21        if ($boundedContextHashmap instanceof BoundedContextHashmap) {
22            foreach ($boundedContextHashmap->getTupleIterator() as $tuple) {
23                foreach ($tuple->resourceClass->getAttributes(ProvideTranslationMethod::class) as $reflAttribute) {
24                    $attribute = $reflAttribute->newInstance();
25                    $method = $tuple->resourceClass->getMethod($attribute->methodName);
26                    if (!$method->isStatic()) {
27                        throw new \LogicException('Method ' . $method->name . ' is not static');
28                    }
29                    $result = $method->invoke(null, $apieContext, $tuple);
30                    if (is_iterable($result)) {
31                        foreach ($result as $item) {
32                            assert($item instanceof  AbstractTranslation);
33                            $items[] = $item;
34                        }
35                    } else {
36                        assert($result instanceof AbstractTranslation);
37                        $items[] = $result;
38                    }
39                }
40            }
41        }
42        return new TranslationStringSet($items);
43    }
44}