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

ListRequest::addLimit()   A

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 0
Metric Value
eloc 3
c 0
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\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 ListRequest 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 14
    public function getXML(): \SimpleXMLElement
87
    {
88
        // UserList is custom
89 14
        if ('UserList' == $this->data['type']) {
90 1
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':listUserCodeRequest', '', $this->namespace(strval($this->data['namespace'])));
91 1
            $xml->addAttribute('version', '1.1');
92 1
            $xml->addAttribute('listVersion', '1.1');
93
        } else {
94 13
            $xml = $this->createXML()->addChild($this->data['namespace'] . ':list' . $this->data['type'] . 'Request', '', $this->namespace(strval($this->data['namespace'])));
95 13
            $xml->addAttribute('version', '2.0');
96
97
            // IntParam doesn't have the version attribute
98 13
            if ('IntParam' != $this->data['type']) {
99 12
                $xml->addAttribute($this->getLcFirstType() . 'Version', '2.0');
100
            }
101
102 13
            if (isset($this->data[$this->getLcFirstType() . 'Type'])) {
103 4
                $xml->addAttribute($this->getLcFirstType() . 'Type', strval($this->data[$this->getLcFirstType() . 'Type']));
104
            }
105
106 13
            $request = $xml->addChild($this->data['namespace'] . ':request' . $this->data['type']);
107
108 13
            $this->addElements($request, ['limit', 'filter', 'userFilterName'], 'ftr');
109
110 13
            if (isset($this->data['restrictionData'])) {
111 1
                $this->addElements($xml, ['restrictionData'], 'lst');
112
            }
113
        }
114
115 14
        return $xml;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 14
    protected function configureOptions(OptionsResolver $resolver): void
122
    {
123
        // available options
124 1
        $resolver->setDefined(['type', 'namespace', 'orderType', 'invoiceType']);
125
126
        // validate / format options
127 1
        $resolver->setRequired('type');
128 1
        $resolver->setNormalizer('type', $this->normalizerFactory->getClosure('list_request_type'));
129 1
        $resolver->setDefault('namespace', function (Options $options) {
130 14
            if ('Stock' == $options['type']) {
131 1
                return 'lStk';
132
            }
133
134 13
            if ('AddressBook' == $options['type']) {
135 1
                return 'lAdb';
136
            }
137
138 12
            return 'lst';
139 1
        });
140 1
        $resolver->setAllowedValues('orderType', [null, 'receivedOrder', 'issuedOrder']);
141 1
        $resolver->setDefault('orderType', function (Options $options) {
142 14
            if ('Order' == $options['type']) {
143 1
                return 'receivedOrder';
144
            }
145
146 13
            return null;
147 1
        });
148 1
        $resolver->setAllowedValues('invoiceType', [null, 'issuedInvoice', 'issuedCreditNotice', 'issuedDebitNote', 'issuedAdvanceInvoice', 'receivable', 'issuedProformaInvoice', 'penalty', 'issuedCorrectiveTax', 'receivedInvoice', 'receivedCreditNotice', 'receivedDebitNote', 'receivedAdvanceInvoice', 'commitment', 'receivedProformaInvoice', 'receivedCorrectiveTax']);
149 1
        $resolver->setDefault('invoiceType', function (Options $options) {
150 13
            if ('Invoice' == $options['type']) {
151 2
                return 'issuedInvoice';
152
            }
153
154 11
            return null;
155 1
        });
156
    }
157
158
    /**
159
     * Get LC first type name.
160
     *
161
     * @return string
162
     */
163 13
    protected function getLcFirstType(): string
164
    {
165
        // ActionPrice is custom
166 13
        if ('ActionPrice' == $this->data['type']) {
167 1
            return 'actionPrices';
168
        }
169
170 12
        return \lcfirst(strval($this->data['type']));
171
    }
172
}
173