ListResponse::getXML()   F
last analyzed

Complexity

Conditions 19
Paths 666

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 19

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 67
ccs 36
cts 36
cp 1
rs 0.8138
cc 19
nc 666
nop 0
crap 19

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