Passed
Push — master ( 067151...1e051c )
by Petr
13:11
created

Address::setData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
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\Type;
13
14
use Riesenia\Pohoda\AbstractAgenda;
15
use Riesenia\Pohoda\Common;
16
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding;
17
18
class Address extends AbstractAgenda
19
{
20
    use Common\SetNamespaceTrait;
21
22
    /** @var string[] */
23
    protected array $refElements = ['extId'];
24
25
    /** @var string[] */
26
    protected array $elements = ['id', 'extId', 'address', 'addressLinkToAddress', 'shipToAddress'];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 53
    public function __construct(
32
        Common\NamespacesPaths $namespacesPaths,
33
        SanitizeEncoding $sanitizeEncoding,
34
        string $companyRegistrationNumber,
35
        bool $resolveOptions = true,
36
        Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new Common\OptionsResolver\Normalizers\NormalizerFactory(),
37
    ) {
38
        // init attributes
39 53
        $this->elementsAttributesMapper = [
40 53
            'addressLinkToAddress' => new Common\ElementAttributes('address', 'linkToAddress'),
41 53
        ];
42
43 53
        parent::__construct($namespacesPaths, $sanitizeEncoding, $companyRegistrationNumber, $resolveOptions, $normalizerFactory);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 53
    public function setData(array $data): parent
50
    {
51
        // process address
52 53
        if (isset($data['address'])) {
53 22
            $address = new AddressType($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
54 22
            $data['address'] = $address->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data['address']);
55
        }
56
57
        // process shipping address
58 53
        if (isset($data['shipToAddress'])) {
59 1
            $shipTo = new ShipToAddressType($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
60 1
            $data['shipToAddress'] = $shipTo->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data['shipToAddress']);
61
        }
62
63 53
        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...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    protected function configureOptions(Common\OptionsResolver $resolver): void
70
    {
71
        // available options
72 1
        $resolver->setDefined($this->elements);
73
74
        // validate / format options
75 1
        $resolver->setNormalizer('id', $this->normalizerFactory->getClosure('int'));
76 1
        $resolver->setNormalizer('addressLinkToAddress', $this->normalizerFactory->getClosure('bool'));
77
    }
78
}
79