Header::configureOptions()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 55
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 55
ccs 50
cts 50
cp 1
rs 9.1127
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of riesenia/pohoda package.
5
 *
6
 * Licensed under the MIT License
7
 * (c) RIESENIA.com
8
 */
9
10
declare(strict_types=1);
11
12
namespace Riesenia\Pohoda\Stock;
13
14
use Riesenia\Pohoda\AbstractAgenda;
15
use Riesenia\Pohoda\Common;
16
use Riesenia\Pohoda\Type;
17
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding;
18
19
/**
20
 * @property array{
21
 *     parameters?: iterable<Type\Parameter>,
22
 *     intrastat?: Intrastat,
23
 *     recyclingContrib?: RecyclingContrib,
24
 *     pictures?: iterable<Picture>,
25
 *     categories?: iterable<Category>,
26
 *     intParameters?: iterable<IntParameter>,
27
 * } $data
28
 */
29
class Header extends AbstractAgenda
30
{
31
    use Common\AddParameterTrait;
32
33
    /** @var string[] */
34
    protected array $refElements = ['storage', 'typePrice', 'typeRP', 'supplier', 'typeServiceMOSS'];
35
36
    /** @var string[] */
37
    protected array $elements = ['stockType', 'code', 'EAN', 'PLU', 'isSales', 'isSerialNumber', 'isInternet', 'isBatch', 'purchasingRateVAT', 'purchasingRatePayVAT', 'sellingRateVAT', 'sellingRatePayVAT', 'name', 'nameComplement', 'unit', 'unit2', 'unit3', 'coefficient2', 'coefficient3', 'storage', 'typePrice', 'purchasingPrice', 'purchasingPricePayVAT', 'sellingPrice', 'sellingPricePayVAT', 'limitMin', 'limitMax', 'mass', 'volume', 'supplier', 'orderName', 'orderQuantity', 'shortName', 'typeRP', 'guaranteeType', 'guarantee', 'producer', 'typeServiceMOSS', 'description', 'description2', 'note', 'intrastat', 'recyclingContrib'];
38
39
    /** @var string[] */
40
    protected array $additionalElements = ['id', 'weightedPurchasePrice', 'count', 'countIssue', 'countReceivedOrders', 'reservation', 'countIssuedOrders', 'clearanceSale', 'controlLimitTaxLiability', 'discount', 'fixation', 'markRecord', 'news', 'prepare', 'recommended', 'sale', 'reclamation', 'service'];
41
42
    /** @var int */
43
    protected int $imagesCounter = 0;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 21
    public function __construct(
49
        Common\NamespacesPaths $namespacesPaths,
50
        SanitizeEncoding $sanitizeEncoding,
51
        Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new Common\OptionsResolver\Normalizers\NormalizerFactory(),
52
    ) {
53
        // init attributes
54 21
        $this->elementsAttributesMapper = [
55 21
            'purchasingPricePayVAT' => new Common\ElementAttributes('purchasingPrice', 'payVAT'),
56 21
            'sellingPricePayVAT' => new Common\ElementAttributes('sellingPrice', 'payVAT'),
57 21
            'purchasingRatePayVAT' => new Common\ElementAttributes('purchasingRateVAT', 'value'),
58 21
            'sellingRatePayVAT' => new Common\ElementAttributes('sellingRateVAT', 'value'),
59 21
        ];
60
61 21
        parent::__construct($namespacesPaths, $sanitizeEncoding, $normalizerFactory);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 21
    public function setData(array $data): parent
68
    {
69
        // process intrastat
70 21
        if (isset($data['intrastat'])) {
71 10
            $intrastat = new Intrastat($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
72 10
            $intrastat->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data['intrastat']);
73 10
            $data['intrastat'] = $intrastat;
74
        }
75
76
        // process recyclingContrib
77 21
        if (isset($data['recyclingContrib'])) {
78 10
            $recyclingContrib = new RecyclingContrib($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
79 10
            $recyclingContrib->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data['recyclingContrib']);
80 10
            $data['recyclingContrib'] = $recyclingContrib;
81
        }
82
83 21
        return parent::setData($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setData($data) returns the type Riesenia\Pohoda\AbstractAgenda which is incompatible with the type-hinted return parent.
Loading history...
84
    }
85
86
    /**
87
     * Add image.
88
     *
89
     * @param string   $filepath
90
     * @param string   $description
91
     * @param int|null $order
92
     * @param bool     $default
93
     *
94
     * @return void
95
     */
96 1
    public function addImage(string $filepath, string $description = '', int $order = null, bool $default = false): void
97
    {
98 1
        if (!isset($this->data['pictures'])
99 1
            || !(
100 1
                is_array($this->data['pictures'])
101 1
                || (is_a($this->data['pictures'], \ArrayAccess::class))
102 1
            )
103
        ) {
104 1
            $this->data['pictures'] = [];
105
        }
106
107 1
        $picture = new Picture($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
108 1
        $picture->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData([
109 1
            'filepath' => $filepath,
110 1
            'description' => $description,
111 1
            'order' => null === $order ? ++$this->imagesCounter : $order,
112 1
            'default' => $default,
113 1
        ]);
114 1
        $this->data['pictures'][] = $picture;
115
    }
116
117
    /**
118
     * Add category.
119
     *
120
     * @param int $categoryId
121
     *
122
     * @return void
123
     */
124 1
    public function addCategory(int $categoryId): void
125
    {
126 1
        if (!isset($this->data['categories'])
127 1
            || !(
128 1
                is_array($this->data['categories'])
129 1
                || (is_a($this->data['categories'], \ArrayAccess::class))
130 1
            )
131
        ) {
132 1
            $this->data['categories'] = [];
133
        }
134
135 1
        $category = new Category($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
136 1
        $category->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData([
137 1
            'idCategory' => $categoryId,
138 1
        ]);
139 1
        $this->data['categories'][] = $category;
140
    }
141
142
    /**
143
     * Add int parameter.
144
     *
145
     * @param array<string,mixed> $data
146
     *
147
     * @return void
148
     */
149 1
    public function addIntParameter(array $data): void
150
    {
151 1
        if (!isset($this->data['intParameters'])
152 1
            || !(
153 1
                is_array($this->data['intParameters'])
154 1
                || (is_a($this->data['intParameters'], \ArrayAccess::class))
155 1
            )
156
        ) {
157 1
            $this->data['intParameters'] = [];
158
        }
159
160 1
        $intParameters = new IntParameter($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
161 1
        $intParameters->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
162 1
        $this->data['intParameters'][] = $intParameters;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 18
    public function getXML(): \SimpleXMLElement
169
    {
170 18
        $xml = $this->createXML()->addChild('stk:stockHeader', '', $this->namespace('stk'));
171
172 18
        $this->addElements($xml, \array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : []), ['categories', 'pictures', 'parameters', 'intParameters']), 'stk');
173
174 18
        return $xml;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 2
    protected function configureOptions(Common\OptionsResolver $resolver): void
181
    {
182
        // available options
183 2
        $resolver->setDefined(array_merge($this->elements, ($this->useOneDirectionalVariables ? $this->additionalElements : [])));
184
185
        // validate / format options
186 2
        $resolver->setDefault('stockType', 'card');
187 2
        $resolver->setAllowedValues('stockType', ['card', 'text', 'service', 'package', 'set', 'product']);
188 2
        $resolver->setNormalizer('isSales', $this->normalizerFactory->getClosure('bool'));
189 2
        $resolver->setNormalizer('isSerialNumber', $this->normalizerFactory->getClosure('bool'));
190 2
        $resolver->setNormalizer('isInternet', $this->normalizerFactory->getClosure('bool'));
191 2
        $resolver->setNormalizer('isBatch', $this->normalizerFactory->getClosure('bool'));
192 2
        $resolver->setAllowedValues('purchasingRateVAT', ['none', 'third', 'low', 'high']);
193 2
        $resolver->setAllowedValues('sellingRateVAT', ['none', 'third', 'low', 'high']);
194 2
        $resolver->setNormalizer('name', $this->normalizerFactory->getClosure('string90'));
195 2
        $resolver->setNormalizer('nameComplement', $this->normalizerFactory->getClosure('string90'));
196 2
        $resolver->setNormalizer('unit', $this->normalizerFactory->getClosure('string10'));
197 2
        $resolver->setNormalizer('unit2', $this->normalizerFactory->getClosure('string10'));
198 2
        $resolver->setNormalizer('unit3', $this->normalizerFactory->getClosure('string10'));
199 2
        $resolver->setNormalizer('coefficient2', $this->normalizerFactory->getClosure('float'));
200 2
        $resolver->setNormalizer('coefficient3', $this->normalizerFactory->getClosure('float'));
201 2
        $resolver->setNormalizer('purchasingPrice', $this->normalizerFactory->getClosure('float'));
202 2
        $resolver->setNormalizer('purchasingPricePayVAT', $this->normalizerFactory->getClosure('bool'));
203 2
        $resolver->setNormalizer('sellingPrice', $this->normalizerFactory->getClosure('float'));
204 2
        $resolver->setNormalizer('sellingPricePayVAT', $this->normalizerFactory->getClosure('bool'));
205 2
        $resolver->setNormalizer('limitMin', $this->normalizerFactory->getClosure('float'));
206 2
        $resolver->setNormalizer('limitMax', $this->normalizerFactory->getClosure('float'));
207 2
        $resolver->setNormalizer('mass', $this->normalizerFactory->getClosure('float'));
208 2
        $resolver->setNormalizer('volume', $this->normalizerFactory->getClosure('float'));
209 2
        $resolver->setNormalizer('orderName', $this->normalizerFactory->getClosure('string90'));
210 2
        $resolver->setNormalizer('orderQuantity', $this->normalizerFactory->getClosure('float'));
211 2
        $resolver->setNormalizer('shortName', $this->normalizerFactory->getClosure('string24'));
212 2
        $resolver->setAllowedValues('guaranteeType', ['none', 'hour', 'day', 'month', 'year', 'life']);
213 2
        $resolver->setNormalizer('guarantee', $this->normalizerFactory->getClosure('int'));
214 2
        $resolver->setNormalizer('producer', $this->normalizerFactory->getClosure('string90'));
215 2
        $resolver->setNormalizer('description', $this->normalizerFactory->getClosure('string240'));
216
217 2
        if ($this->useOneDirectionalVariables) {
218 1
            $resolver->setNormalizer('weightedPurchasePrice', $this->normalizerFactory->getClosure('float'));
219 1
            $resolver->setNormalizer('count', $this->normalizerFactory->getClosure('float'));
220 1
            $resolver->setNormalizer('countIssue', $this->normalizerFactory->getClosure('float'));
221 1
            $resolver->setNormalizer('countReceivedOrders', $this->normalizerFactory->getClosure('float'));
222 1
            $resolver->setNormalizer('reservation', $this->normalizerFactory->getClosure('float'));
223 1
            $resolver->setNormalizer('countIssuedOrders', $this->normalizerFactory->getClosure('float'));
224 1
            $resolver->setNormalizer('reclamation', $this->normalizerFactory->getClosure('float'));
225 1
            $resolver->setNormalizer('service', $this->normalizerFactory->getClosure('float'));
226 1
            $resolver->setNormalizer('clearanceSale', $this->normalizerFactory->getClosure('bool'));
227 1
            $resolver->setNormalizer('controlLimitTaxLiability', $this->normalizerFactory->getClosure('bool'));
228 1
            $resolver->setNormalizer('discount', $this->normalizerFactory->getClosure('bool'));
229 1
            $resolver->setNormalizer('fixation', $this->normalizerFactory->getClosure('string90'));
230 1
            $resolver->setNormalizer('markRecord', $this->normalizerFactory->getClosure('bool'));
231 1
            $resolver->setNormalizer('news', $this->normalizerFactory->getClosure('bool'));
232 1
            $resolver->setNormalizer('prepare', $this->normalizerFactory->getClosure('bool'));
233 1
            $resolver->setNormalizer('recommended', $this->normalizerFactory->getClosure('bool'));
234 1
            $resolver->setNormalizer('sale', $this->normalizerFactory->getClosure('bool'));
235
        }
236
    }
237
}
238