Passed
Push — master ( 1e051c...c8cd94 )
by Petr
02:55
created

ListResponse   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 65
c 1
b 0
f 0
dl 0
loc 187
ccs 73
cts 73
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addRestrictionData() 0 6 1
A addLimit() 0 6 1
C getXML() 0 51 12
A addUserFilterName() 0 6 1
A addFilter() 0 6 1
A getLcFirstType() 0 8 2
B configureOptions() 0 50 6
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;
13
14
use Riesenia\Pohoda\Common\OptionsResolver;
15
use Riesenia\Pohoda\ListRequest\Filter;
16
use Riesenia\Pohoda\ListRequest\Limit;
17
use Riesenia\Pohoda\ListRequest\RestrictionData;
18
use Riesenia\Pohoda\ListRequest\UserFilterName;
19
use Symfony\Component\OptionsResolver\Options;
20
21
class ListResponse extends AbstractAgenda
22
{
23
    /**
24
     * Add limit.
25
     *
26
     * @param array<string,mixed> $data
27
     *
28
     * @return $this
29
     */
30 1
    public function addLimit(array $data): self
31
    {
32 1
        $limit = new Limit($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
33 1
        $this->data['limit'] = $limit->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
34
35 1
        return $this;
36
    }
37
38
    /**
39
     * Add filter.
40
     *
41
     * @param array<string,mixed> $data
42
     *
43
     * @return $this
44
     */
45 1
    public function addFilter(array $data): self
46
    {
47 1
        $filter = new Filter($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
48 1
        $this->data['filter'] = $filter->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
49
50 1
        return $this;
51
    }
52
53
    /**
54
     * Add restriction data.
55
     *
56
     * @param array<string,mixed> $data
57
     *
58
     * @return $this
59
     */
60 1
    public function addRestrictionData(array $data): self
61
    {
62 1
        $restrictionData = new RestrictionData($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
63 1
        $this->data['restrictionData'] = $restrictionData->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * Add user filter name.
70
     *
71
     * @param string $name
72
     *
73
     * @return $this
74
     */
75 1
    public function addUserFilterName(string $name): self
76
    {
77 1
        $userFilterName = new UserFilterName($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
78 1
        $this->data['userFilterName'] = $userFilterName->setDirectionalVariable($this->useOneDirectionalVariables)->setData(['userFilterName' => $name]);
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 15
    public function getXML(): \SimpleXMLElement
87
    {
88
        // UserList is custom
89 15
        if ('UserList' == $this->data['type']) {
90 1
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':listUserCodeResponse', '', $this->namespace(strval($this->data['namespace'])));
91 1
            $xml->addAttribute('version', '1.1');
92 1
            $xml->addAttribute('listVersion', '1.1');
93
        } else {
94 14
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':list' . $this->data['type'], '', $this->namespace(strval($this->data['namespace'])));
95 14
            $xml->addAttribute('version', '2.0');
96
97
            // IntParam doesn't have the version attribute
98 14
            if ('IntParam' != $this->data['type']) {
99 13
                $xml->addAttribute($this->getLcFirstType() . 'Version', '2.0');
100
            }
101
102 14
            if (isset($this->data[$this->getLcFirstType() . 'Type'])) {
103 4
                $xml->addAttribute($this->getLcFirstType() . 'Type', strval($this->data[$this->getLcFirstType() . 'Type']));
104
            }
105
106 14
            $request = $xml->addChild($this->data['namespace'] . ':request' . $this->data['type']);
107
108 14
            $this->addElements($request, ['limit', 'filter', 'userFilterName'], 'ftr');
109
110 14
            if (isset($this->data['restrictionData'])) {
111 1
                $this->addElements($xml, ['restrictionData'], 'lst');
112
            }
113
        }
114
115 15
        if (isset($this->data['timestamp'])) {
116 15
            $date = $this->data['timestamp'];
117 15
            if (is_object($date) && is_a($date, \DateTimeInterface::class)) {
118 15
                $date = $date->format('Y-m-d\TH:i:s');
119
            }
120 15
            $xml->addAttribute('dateTimeStamp', strval($date));
121
        }
122
123 15
        if (isset($this->data['validFrom'])) {
124 3
            $dateFrom = $this->data['validFrom'];
125 3
            if (is_object($dateFrom) && is_a($dateFrom, \DateTimeInterface::class)) {
126 3
                $dateFrom = $dateFrom->format('Y-m-d');
127
            }
128 3
            $xml->addAttribute('dateValidFrom', strval($dateFrom));
129
        }
130
131 15
        if (isset($this->data['state'])) {
132 15
            $xml->addAttribute('state', strval($this->data['state']));
133
        }
134
135
136 15
        return $xml;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 15
    protected function configureOptions(OptionsResolver $resolver): void
143
    {
144
        // available options
145 1
        $resolver->setDefined(['type', 'namespace', 'orderType', 'invoiceType', 'timestamp', 'validFrom', 'state']);
146
147
        // validate / format options
148 1
        $resolver->setRequired('type');
149 1
        $resolver->setNormalizer('type', $this->normalizerFactory->getClosure('list_request_type'));
150 1
        $resolver->setDefault('namespace', function (Options $options) {
151 15
            if ('Stock' == $options['type']) {
152 1
                return 'lStk';
153
            }
154
155 14
            if ('AddressBook' == $options['type']) {
156 1
                return 'lAdb';
157
            }
158
            /*
159
            if ('AccountingUnit' == $options['type']) {
160
                return 'acu';
161
            }
162
            */
163 13
            if ('Contract' == $options['type']) {
164 1
                return 'lCon';
165
            }
166
            /*
167
            if ('Centre' == $options['type']) {
168
                return 'lCen';
169
            }
170
171
            if ('Activity' == $options['type']) {
172
                return 'lAcv';
173
            }
174
            */
175 12
            return 'lst';
176 1
        });
177 1
        $resolver->setAllowedValues('orderType', [null, 'receivedOrder', 'issuedOrder']);
178 1
        $resolver->setDefault('orderType', function (Options $options) {
179 15
            if ('Order' == $options['type']) {
180 1
                return 'receivedOrder';
181
            }
182
183 14
            return null;
184 1
        });
185 1
        $resolver->setAllowedValues('invoiceType', [null, 'issuedInvoice', 'issuedCreditNotice', 'issuedDebitNote', 'issuedAdvanceInvoice', 'receivable', 'issuedProformaInvoice', 'penalty', 'issuedCorrectiveTax', 'receivedInvoice', 'receivedCreditNotice', 'receivedDebitNote', 'receivedAdvanceInvoice', 'commitment', 'receivedProformaInvoice', 'receivedCorrectiveTax']);
186 1
        $resolver->setDefault('invoiceType', function (Options $options) {
187 14
            if ('Invoice' == $options['type']) {
188 2
                return 'issuedInvoice';
189
            }
190
191 12
            return null;
192 1
        });
193
    }
194
195
    /**
196
     * Get LC first type name.
197
     *
198
     * @return string
199
     */
200 14
    protected function getLcFirstType(): string
201
    {
202
        // ActionPrice is custom
203 14
        if ('ActionPrice' == $this->data['type']) {
204 1
            return 'actionPrices';
205
        }
206
207 13
        return \lcfirst(strval($this->data['type']));
208
    }
209
}
210