ListResponse::addRestrictionData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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