ListRequest::configureOptions()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 34
ccs 22
cts 22
cp 1
rs 9.3222
cc 5
nc 1
nop 1
crap 5
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
 *     namespace: string,
20
 *     orderType?: string,
21
 *     invoiceType?: string,
22
 *     limit?: ListRequest\Limit,
23
 *     filter?: ListRequest\Filter,
24
 *     queryFilter?: ListRequest\QueryFilter,
25
 *     restrictionData?: ListRequest\RestrictionData,
26
 *     userFilterName?: ListRequest\UserFilterName,
27
 * } $data
28
 */
29
class ListRequest extends AbstractAgenda
30
{
31
    /**
32
     * Add limit.
33
     *
34
     * @param array<string,mixed> $data
35
     *
36
     * @return $this
37
     */
38 1
    public function addLimit(array $data): self
39
    {
40 1
        $limit = new ListRequest\Limit($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
41 1
        $limit->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
42 1
        $this->data['limit'] = $limit;
43
44 1
        return $this;
45
    }
46
47
    /**
48
     * Add filter.
49
     *
50
     * @param array<string,mixed> $data
51
     *
52
     * @return $this
53
     */
54 1
    public function addFilter(array $data): self
55
    {
56 1
        $filter = new ListRequest\Filter($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
57 1
        $filter->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
58 1
        $this->data['filter'] = $filter;
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Add query filter.
65
     * Beware! This one is direct SQL!
66
     *
67
     * @param array<string,mixed> $data
68
     *
69
     * @return $this
70
     */
71 1
    public function addQueryFilter(array $data): self
72
    {
73 1
        $filter = new ListRequest\QueryFilter($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
74 1
        $filter->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
75 1
        $this->data['queryFilter'] = $filter;
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Add restriction data.
82
     *
83
     * @param array<string,mixed> $data
84
     *
85
     * @return $this
86
     */
87 1
    public function addRestrictionData(array $data): self
88
    {
89 1
        $restrictionData = new ListRequest\RestrictionData($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
90 1
        $restrictionData->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData($data);
91 1
        $this->data['restrictionData'] = $restrictionData;
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * Add user filter name.
98
     *
99
     * @param string $name
100
     *
101
     * @return $this
102
     */
103 1
    public function addUserFilterName(string $name): self
104
    {
105 1
        $userFilterName = new ListRequest\UserFilterName($this->namespacesPaths, $this->sanitizeEncoding, $this->normalizerFactory);
106 1
        $userFilterName->setDirectionalVariable($this->useOneDirectionalVariables)->setResolveOptions($this->resolveOptions)->setData(['userFilterName' => $name]);
107 1
        $this->data['userFilterName'] = $userFilterName;
108
109 1
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 15
    public function getXML(): \SimpleXMLElement
116
    {
117
        // UserList is custom
118 15
        if ('UserList' == $this->data['type']) {
119 1
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':listUserCodeRequest', '', $this->namespace(strval($this->data['namespace'])));
120 1
            $xml->addAttribute('version', '1.1');
121 1
            $xml->addAttribute('listVersion', '1.1');
122
        } else {
123 14
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':list' . $this->data['type'] . 'Request', '', $this->namespace(strval($this->data['namespace'])));
124 14
            $xml->addAttribute('version', '2.0');
125
126
            // IntParam doesn't have the version attribute
127 14
            if ('IntParam' != $this->data['type']) {
128 13
                $xml->addAttribute($this->getLcFirstType() . 'Version', '2.0');
129
            }
130
131 14
            if (isset($this->data[$this->getLcFirstType() . 'Type'])) {
132 5
                $xml->addAttribute($this->getLcFirstType() . 'Type', strval($this->data[$this->getLcFirstType() . 'Type']));
133
            }
134
135 14
            $request = $xml->addChild($this->data['namespace'] . ':request' . $this->data['type']);
136
137 14
            $this->addElements($request, ['limit', 'filter', 'queryFilter', 'userFilterName'], 'ftr');
138
139 14
            if (isset($this->data['restrictionData'])) {
140 1
                $this->addElements($xml, ['restrictionData'], 'lst');
141
            }
142
        }
143
144 15
        return $xml;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 15
    protected function configureOptions(Common\OptionsResolver $resolver): void
151
    {
152
        // available options
153 1
        $resolver->setDefined(['type', 'namespace', 'orderType', 'invoiceType']);
154
155
        // validate / format options
156 1
        $resolver->setRequired('type');
157 1
        $resolver->setNormalizer('type', $this->normalizerFactory->getClosure('list_request_type'));
158 1
        $resolver->setDefault('namespace', function (Options $options) {
159 15
            if ('Stock' == $options['type']) {
160 1
                return 'lStk';
161
            }
162
163 14
            if ('AddressBook' == $options['type']) {
164 1
                return 'lAdb';
165
            }
166
167 13
            return 'lst';
168 1
        });
169 1
        $resolver->setAllowedValues('orderType', [null, 'receivedOrder', 'issuedOrder']);
170 1
        $resolver->setDefault('orderType', function (Options $options) {
171 15
            if ('Order' == $options['type']) {
172 2
                return 'receivedOrder';
173
            }
174
175 13
            return null;
176 1
        });
177 1
        $resolver->setAllowedValues('invoiceType', [null, 'issuedInvoice', 'issuedCreditNotice', 'issuedDebitNote', 'issuedAdvanceInvoice', 'receivable', 'issuedProformaInvoice', 'penalty', 'issuedCorrectiveTax', 'receivedInvoice', 'receivedCreditNotice', 'receivedDebitNote', 'receivedAdvanceInvoice', 'commitment', 'receivedProformaInvoice', 'receivedCorrectiveTax']);
178 1
        $resolver->setDefault('invoiceType', function (Options $options) {
179 14
            if ('Invoice' == $options['type']) {
180 2
                return 'issuedInvoice';
181
            }
182
183 12
            return null;
184 1
        });
185
    }
186
187
    /**
188
     * Get LC first type name.
189
     *
190
     * @return string
191
     */
192 14
    protected function getLcFirstType(): string
193
    {
194
        // ActionPrice is custom
195 14
        if ('ActionPrice' == $this->data['type']) {
196 1
            return 'actionPrices';
197
        }
198
199 13
        return \lcfirst(strval($this->data['type']));
200
    }
201
}
202