ValueAccessor::getHeaderValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Povs\ListerBundle\Service;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Povs\ListerBundle\DependencyInjection\Locator\SelectorTypeLocator;
7
use Povs\ListerBundle\Exception\ListException;
8
use Povs\ListerBundle\Exception\ListQueryException;
9
use Povs\ListerBundle\Mapper\ListField;
10
use Povs\ListerBundle\View\FieldView;
11
use Symfony\Contracts\Translation\TranslatorInterface;
12
use Throwable;
13
14
/**
15
 * @author Povilas Margaiatis <[email protected]>
16
 */
17
class ValueAccessor
18
{
19
    /**
20
     * @var ConfigurationResolver
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var ListTypeResolver
26
     */
27
    private $typeResolver;
28
29
    /**
30
     * @var SelectorTypeLocator
31
     */
32
    private $selectorTypeLocator;
33
34
    /**
35
     * @var TranslatorInterface
36
     */
37
    private $translator;
38
39
    /**
40
     * ValueAccessor constructor.
41
     *
42
     * @param ConfigurationResolver $configurationResolver
43
     * @param ListTypeResolver      $listTypeResolver
44
     * @param SelectorTypeLocator   $selectorTypeLocator
45
     * @param TranslatorInterface   $translator
46
     */
47 7
    public function __construct(
48
        ConfigurationResolver $configurationResolver,
49
        ListTypeResolver $listTypeResolver,
50
        SelectorTypeLocator $selectorTypeLocator,
51
        ?TranslatorInterface $translator
52
    ) {
53 7
        $this->configuration = $configurationResolver;
54 7
        $this->typeResolver = $listTypeResolver;
55 7
        $this->selectorTypeLocator = $selectorTypeLocator;
56 7
        $this->translator = $translator;
57
    }
58
59
    /**
60
     * Fetches lazy loadable data
61
     *
62
     * @param array             $data         data that is fetched not lazily
63
     * @param QueryBuilder|null $queryBuilder queryBuilder for fetching lazy data query
64
     *
65
     * @return array
66
     */
67 2
    public function normalizeData(array $data, ?QueryBuilder $queryBuilder): array
68
    {
69 2
        if ($queryBuilder) {
70 2
            $selector = sprintf(
71 2
                '%s.%s = :identifier',
72 2
                $this->configuration->getAlias(),
73 2
                $this->configuration->getIdentifier()
74 2
            );
75
76
            try {
77 2
                $query = clone $queryBuilder;
78 2
                $lazyData = $query->andWhere($selector)
79 2
                    ->setParameter('identifier', $data[ListQueryBuilder::IDENTIFIER_ALIAS])
80 2
                    ->getQuery()
81 2
                    ->getResult()[0];
82
83 1
                $data = array_merge($data, $lazyData);
84 1
            } catch (Throwable $e) {
85 1
                throw ListQueryException::invalidQueryConfiguration($e->getMessage(), $query->getDQL());
86
            }
87
        }
88
89 1
        unset($data[ListQueryBuilder::IDENTIFIER_ALIAS]);
90
91 1
        return $data;
92
    }
93
94
    /**
95
     * @param FieldView $fieldView
96
     *
97
     * @return string
98
     */
99 2
    public function getHeaderValue(FieldView $fieldView): string
100
    {
101 2
        $value = $fieldView->getLabel();
102 2
        $translate = $this->configuration->getTranslate();
103
104 2
        if ($translate) {
105 2
            $domain = $this->configuration->getTranslationDomain();
106 2
            $value = $this->translate($value, $domain);
107
        }
108
109 1
        return $value;
110
    }
111
112
    /**
113
     * @param FieldView $fieldView
114
     * @param array     $data
115
     *
116
     * @return mixed
117
     */
118 3
    public function getFieldValue(FieldView $fieldView, array $data)
119
    {
120 3
        $listField = $fieldView->getListField();
121 3
        $value = $this->selectorTypeLocator->get($listField->getOption(ListField::OPTION_SELECTOR))
122 3
            ->getValue($data, $listField->getId());
123
124 3
        $this->addIdentifiers($listField, $value);
125 3
        $this->processFieldValue($listField, $value);
126 3
        $this->translateValue($listField, $value);
127
128 3
        return $value;
129
    }
130
131
    /**
132
     * @param ListField    $listField
133
     * @param mixed        $value
134
     */
135 3
    private function processFieldValue(ListField $listField, &$value): void
136
    {
137 3
        if ($callable = $listField->getOption(ListField::OPTION_VALUE)) {
138 1
            $value = $callable($value, $this->typeResolver->getTypeName());
139
        }
140
141 3
        if ($type = $listField->getType()) {
142 1
            $value = $type->getValue(
143 1
                $value,
144 1
                $this->typeResolver->getTypeName(),
145 1
                $listField->getOption(ListField::OPTION_FIELD_TYPE_OPTIONS)
0 ignored issues
show
Bug introduced by
$listField->getOption(Po...ION_FIELD_TYPE_OPTIONS) of type Doctrine\Common\Collections\T|null is incompatible with the type array expected by parameter $options of Povs\ListerBundle\Type\F...peInterface::getValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                /** @scrutinizer ignore-type */ $listField->getOption(ListField::OPTION_FIELD_TYPE_OPTIONS)
Loading history...
146 1
            );
147
        }
148
    }
149
150
    /**
151
     * @param ListField $listField
152
     * @param mixed     $value
153
     */
154 3
    private function addIdentifiers(ListField $listField, &$value): void
155
    {
156 3
        if (is_array($value) && count($listField->getPaths()) === count($value)) {
157 1
            $newValue = [];
158 1
            $i = 0;
159
160 1
            foreach ($listField->getPaths() as $key => $path) {
161 1
                $newValue[$key] = $value[$i];
162 1
                $i++;
163
            }
164
165 1
            $value = $newValue;
166
        }
167
    }
168
169
    /**
170
     * @param ListField $listField
171
     * @param mixed     $value
172
     */
173 3
    private function translateValue(ListField $listField, &$value): void
174
    {
175
        if (
176 3
            (true === $listField->getOption(ListField::OPTION_TRANSLATE)) &&
0 ignored issues
show
introduced by
The condition true === $listField->get...ield::OPTION_TRANSLATE) is always false.
Loading history...
177 3
            (null !== $value || true === $listField->getOption(ListField::OPTION_TRANSLATE_NULL))
178
        ) {
179 1
            $domain = $listField->getOption(ListField::OPTION_TRANSLATION_DOMAIN);
180 1
            $prefix = $listField->getOption(ListField::OPTION_TRANSLATION_PREFIX);
181
182 1
            $value = $this->translate(sprintf('%s%s', $prefix, $value), $domain);
183
        }
184
    }
185
186
    /**
187
     * @param string      $id
188
     * @param string|null $domain
189
     *
190
     * @return string|null
191
     */
192 3
    private function translate(string $id, ?string $domain): ?string
193
    {
194 3
        if (!$this->translator) {
195 1
            throw ListException::missingTranslator();
196
        }
197
198 2
        return $this->translator->trans($id, [], $domain);
199
    }
200
}
201