Passed
Push — master ( 2e50a1...d1c1fe )
by Petr
02:53
created

ListResponse::getXML()   F

Complexity

Conditions 18
Paths 666

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 18

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 65
ccs 35
cts 35
cp 1
rs 1.1638
c 1
b 0
f 0
cc 18
nc 666
nop 0
crap 18

How to fix   Long Method    Complexity   

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;
13
14
use Riesenia\Pohoda\Common\DirectionAsResponseTrait;
15
use Riesenia\Pohoda\Common\OptionsResolver;
16
use Riesenia\Pohoda\ListRequest\Filter;
17
use Riesenia\Pohoda\ListRequest\Limit;
18
use Riesenia\Pohoda\ListRequest\RestrictionData;
19
use Riesenia\Pohoda\ListRequest\UserFilterName;
20
use Symfony\Component\OptionsResolver\Options;
21
22
/**
23
 * @property array{
24
 *     type: string,
25
 *     state: string,
26
 *     namespace: string,
27
 *     limit?: Limit,
28
 *     filter?: Filter,
29
 *     restrictionData?: RestrictionData,
30
 *     userFilterName?: UserFilterName,
31
 *     order?: Order,
32
 *     stock?: iterable<Stock>,
33
 *     timestamp?: string|\DateTimeInterface,
34
 *     validFrom?: string|\DateTimeInterface,
35
 * } $data
36
 */
37
class ListResponse extends AbstractAgenda
38
{
39
    use DirectionAsResponseTrait;
40
41
    /**
42
     * Add limit.
43
     *
44
     * @param array<string,mixed> $data
45
     *
46
     * @return $this
47
     */
48 1
    public function addLimit(array $data): self
49
    {
50 1
        $limit = new Limit($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
51 1
        $limit->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
52 1
        $this->data['limit'] = $limit;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * Add filter.
59
     *
60
     * @param array<string,mixed> $data
61
     *
62
     * @return $this
63
     */
64 1
    public function addFilter(array $data): self
65
    {
66 1
        $filter = new Filter($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
67 1
        $filter->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
68 1
        $this->data['filter'] = $filter;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * Add restriction data.
75
     *
76
     * @param array<string,mixed> $data
77
     *
78
     * @return $this
79
     */
80 1
    public function addRestrictionData(array $data): self
81
    {
82 1
        $restrictionData = new RestrictionData($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
83 1
        $restrictionData->setDirectionalVariable($this->useOneDirectionalVariables)->setData($data);
84 1
        $this->data['restrictionData'] = $restrictionData;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * Add user filter name.
91
     *
92
     * @param string $name
93
     *
94
     * @return $this
95
     */
96 1
    public function addUserFilterName(string $name): self
97
    {
98 1
        $userFilterName = new UserFilterName($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
99 1
        $userFilterName->setDirectionalVariable($this->useOneDirectionalVariables)->setData(['userFilterName' => $name]);
100 1
        $this->data['userFilterName'] = $userFilterName;
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * Add Order as content
107
     *
108
     * @param array<string, mixed> $header
109
     * @param array<array<string, mixed>> $items
110
     * @param array<string, mixed> $summary
111
     *
112
     * @return Order
113
     *
114
     * @todo: more orders in one query
115
     */
116 1
    public function addOrder(array $header, array $items = [], array $summary = []): Order
117
    {
118
        /*
119
        if (!isset($this->data['order'])
120
            || !(
121
                is_array($this->data['order'])
122
                || (is_object($this->data['order']) && is_a($this->data['order'], \ArrayAccess::class))
123
            )
124
        ) {
125
            $this->data['order'] = [];
126
        }
127
        */
128 1
        $order = new Order($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
129
        // $this->data['order'][] = $order->setDirectionalVariable($this->useOneDirectionalVariables)->setData($header);
130 1
        $order->setDirectionalVariable($this->useOneDirectionalVariables)->setData($header);
131 1
        $this->data['order'] = $order;
132 1
        foreach ($items as $item) {
133 1
            $order->addItem($item);
134
        }
135 1
        if (!empty($summary)) {
136 1
            $order->addSummary($summary);
137
        }
138
139 1
        return $order;
140
    }
141
142
    /**
143
     * Add Stock as content
144
     *
145
     * @param array<string, mixed> $header
146
     *
147
     * @return Stock
148
     */
149 1
    public function addStock(array $header): Stock
150
    {
151 1
        if (!isset($this->data['stock'])
152 1
            || !(
153 1
                is_array($this->data['stock'])
154 1
                || (is_a($this->data['stock'], \ArrayAccess::class))
155 1
            )
156
        ) {
157 1
            $this->data['stock'] = [];
158
        }
159 1
        $stock = new Stock($this->namespacesPaths, $this->sanitizeEncoding, $this->companyRegistrationNumber, $this->resolveOptions, $this->normalizerFactory);
160 1
        $stock->setDirectionalVariable($this->useOneDirectionalVariables)->setData($header);
161 1
        $this->data['stock'][] = $stock;
162
163 1
        return $stock;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 17
    public function getXML(): \SimpleXMLElement
170
    {
171
        // UserList is custom
172 17
        if ('UserList' == $this->data['type']) {
173 1
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':listUserCodeResponse', '', $this->namespace(strval($this->data['namespace'])));
174 1
            $xml->addAttribute('version', '1.1');
175 1
            $xml->addAttribute('listVersion', '1.1');
176
        } else {
177 16
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':list' . $this->data['type'], '', $this->namespace(strval($this->data['namespace'])));
178 16
            $xml->addAttribute('version', '2.0');
179
180
            // IntParam and Order doesn't have the version attribute
181 16
            if (!in_array($this->data['type'], ['IntParam', 'Order'])) {
182 13
                if (!isset($this->data['stock'])) {
183 12
                    $xml->addAttribute($this->getLcFirstType() . 'Version', '2.0');
184
                }
185
            }
186
187 16
            if (isset($this->data[$this->getLcFirstType() . 'Type'])) {
188 5
                $xml->addAttribute($this->getLcFirstType() . 'Type', strval($this->data[$this->getLcFirstType() . 'Type']));
189
            }
190
191 16
            if ('Order' == $this->data['type'] && (isset($this->data['order']))) {
192 1
                $this->addElements($xml, ['order'], strval($this->data['namespace']));
193
194 15
            } elseif ('Stock' == $this->data['type'] && (isset($this->data['stock']))) {
195 1
                foreach ($this->data['stock'] as $stockElement) {
196
                    // set namespace
197 1
                    $stockElement->setNamespace('lStk');
198 1
                    $this->appendNode($xml, $stockElement->getXML());
199
                }
200
201
            } else {
202 14
                $request = $xml->addChild($this->data['namespace'] . ':' . $this->whichDirection($this->directionAsResponse) . $this->data['type']);
203
204 14
                $this->addElements($request, ['limit', 'filter', 'userFilterName'], 'ftr');
205
            }
206
207 16
            if (isset($this->data['restrictionData'])) {
208 1
                $this->addElements($xml, ['restrictionData'], 'lst');
209
            }
210
        }
211
212 17
        if (isset($this->data['timestamp'])) {
213 17
            $date = $this->data['timestamp'];
214 17
            if (is_object($date) && is_a($date, \DateTimeInterface::class)) {
215 17
                $date = $date->format('Y-m-d\TH:i:s');
216
            }
217 17
            $xml->addAttribute('dateTimeStamp', strval($date));
218
        }
219
220 17
        if (isset($this->data['validFrom'])) {
221 4
            $dateFrom = $this->data['validFrom'];
222 4
            if (is_object($dateFrom) && is_a($dateFrom, \DateTimeInterface::class)) {
223 4
                $dateFrom = $dateFrom->format('Y-m-d');
224
            }
225 4
            $xml->addAttribute('dateValidFrom', strval($dateFrom));
226
        }
227
228 17
        if (isset($this->data['state'])) {
229 17
            $xml->addAttribute('state', strval($this->data['state']));
230
        }
231
232
233 17
        return $xml;
234
    }
235
236 14
    protected function whichDirection(bool $asResponse): string
237
    {
238 14
        return $asResponse ? 'response' : 'request';
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 17
    protected function configureOptions(OptionsResolver $resolver): void
245
    {
246
        // available options
247 2
        $resolver->setDefined(['type', 'namespace', 'order', 'orderType', 'invoiceType', 'timestamp', 'validFrom', 'state']);
248
249
        // validate / format options
250 2
        $resolver->setRequired('type');
251 2
        $resolver->setNormalizer('type', $this->normalizerFactory->getClosure('list_request_type'));
252 2
        $resolver->setDefault('namespace', function (Options $options) {
253 17
            if ('Stock' == $options['type']) {
254 2
                return 'lStk';
255
            }
256
257 15
            if ('AddressBook' == $options['type']) {
258 1
                return 'lAdb';
259
            }
260
            /*
261
            if ('AccountingUnit' == $options['type']) {
262
                return 'acu';
263
            }
264
            */
265 14
            if ('Contract' == $options['type']) {
266 1
                return 'lCon';
267
            }
268
            /*
269
            if ('Centre' == $options['type']) {
270
                return 'lCen';
271
            }
272
273
            if ('Activity' == $options['type']) {
274
                return 'lAcv';
275
            }
276
            */
277 13
            return 'lst';
278 2
        });
279 2
        $resolver->setAllowedValues('orderType', [null, 'receivedOrder', 'issuedOrder']);
280 2
        $resolver->setDefault('orderType', function (Options $options) {
281 17
            if ('Order' == $options['type']) {
282 2
                return 'receivedOrder';
283
            }
284
285 15
            return null;
286 2
        });
287 2
        $resolver->setAllowedValues('invoiceType', [null, 'issuedInvoice', 'issuedCreditNotice', 'issuedDebitNote', 'issuedAdvanceInvoice', 'receivable', 'issuedProformaInvoice', 'penalty', 'issuedCorrectiveTax', 'receivedInvoice', 'receivedCreditNotice', 'receivedDebitNote', 'receivedAdvanceInvoice', 'commitment', 'receivedProformaInvoice', 'receivedCorrectiveTax']);
288 2
        $resolver->setDefault('invoiceType', function (Options $options) {
289 16
            if ('Invoice' == $options['type']) {
290 2
                return 'issuedInvoice';
291
            }
292
293 14
            return null;
294 2
        });
295
    }
296
297
    /**
298
     * Get LC first type name.
299
     *
300
     * @return string
301
     */
302 16
    protected function getLcFirstType(): string
303
    {
304
        // ActionPrice is custom
305 16
        if ('ActionPrice' == $this->data['type']) {
306 1
            return 'actionPrices';
307
        }
308
309 15
        return \lcfirst(strval($this->data['type']));
310
    }
311
}
312