ListResponse::addLimit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis\Pohoda;
6
7
/**
8
 * @property ListResponse\ListResponseDto $data
9
 */
10
class ListResponse extends AbstractAgenda
11
{
12
    use Common\DirectionAsResponseTrait;
13
14
    /**
15
     * Add limit.
16
     *
17
     * @param ListRequest\LimitDto $data
18
     *
19
     * @return $this
20
     */
21 1
    public function addLimit(ListRequest\LimitDto $data): self
22
    {
23 1
        $limit = new ListRequest\Limit($this->dependenciesFactory);
24 1
        $limit
25 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
26 1
            ->setResolveOptions($this->resolveOptions)
27 1
            ->setData($data);
28 1
        $this->data->limit = $limit;
29
30 1
        return $this;
31
    }
32
33
    /**
34
     * Add filter.
35
     *
36
     * @param ListRequest\FilterDto $data
37
     *
38
     * @return $this
39
     */
40 1
    public function addFilter(ListRequest\FilterDto $data): self
41
    {
42 1
        $filter = new ListRequest\Filter($this->dependenciesFactory);
43 1
        $filter
44 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
45 1
            ->setResolveOptions($this->resolveOptions)
46 1
            ->setData($data);
47 1
        $this->data->filter = $filter;
48
49 1
        return $this;
50
    }
51
52
    /**
53
     * Add restriction data.
54
     *
55
     * @param ListRequest\RestrictionDataDto $data
56
     *
57
     * @return $this
58
     */
59 1
    public function addRestrictionData(ListRequest\RestrictionDataDto $data): self
60
    {
61 1
        $restrictionData = new ListRequest\RestrictionData($this->dependenciesFactory);
62 1
        $restrictionData
63 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
64 1
            ->setResolveOptions($this->resolveOptions)
65 1
            ->setData($data);
66 1
        $this->data->restrictionData = $restrictionData;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * Add user filter name.
73
     *
74
     * @param ListRequest\UserFilterNameDto $data
75
     *
76
     * @return $this
77
     */
78 1
    public function addUserFilterName(ListRequest\UserFilterNameDto $data): self
79
    {
80 1
        $userFilterName = new ListRequest\UserFilterName($this->dependenciesFactory);
81 1
        $userFilterName
82 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
83 1
            ->setResolveOptions($this->resolveOptions)
84 1
            ->setData($data);
85 1
        $this->data->userFilterName = $userFilterName;
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Add Order as content
92
     *
93
     * @param Order\OrderDto $dto
94
     * @param Order\ItemDto[] $items
95
     * @param Order\SummaryDto|null $summary
96
     *
97
     * @return Order
98
     */
99 1
    public function addOrder(Order\OrderDto $dto, array $items = [], ?Order\SummaryDto $summary = null): Order
100
    {
101 1
        $order = new Order($this->dependenciesFactory);
102 1
        $order
103 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
104 1
            ->setResolveOptions($this->resolveOptions)
105 1
            ->setData($dto);
106 1
        $this->data->order[] = $order;
107 1
        foreach ($items as $item) {
108 1
            $order->addItem($item);
109
        }
110 1
        if (!empty($summary)) {
111 1
            $order->addSummary($summary);
112
        }
113
114 1
        return $order;
115
    }
116
117
    /**
118
     * Add Stock as content
119
     *
120
     * @param Stock\StockDto $dto
121
     *
122
     * @return Stock
123
     */
124 1
    public function addStock(Stock\StockDto $dto): Stock
125
    {
126 1
        $stock = new Stock($this->dependenciesFactory);
127 1
        $stock
128 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
129 1
            ->setResolveOptions($this->resolveOptions)
130 1
            ->setData($dto);
131 1
        $this->data->stock[] = $stock;
132
133 1
        return $stock;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 17
    public function getXML(): \SimpleXMLElement
140
    {
141
        // UserList is custom
142 17
        if ('UserList' == $this->data->type) {
143 1
            $xml = $this->createXML()->addChild($this->data->namespace . ':listUserCodeResponse', '', $this->namespace(\strval($this->data->namespace)));
144 1
            $xml->addAttribute('version', '1.1');
145 1
            $xml->addAttribute('listVersion', '1.1');
146
        } else {
147 16
            $xml = $this->createXML()->addChild($this->data->namespace . ':list' . $this->data->type, '', $this->namespace(\strval($this->data->namespace)));
148 16
            $xml->addAttribute('version', '2.0');
149
150
            // IntParam and Order doesn't have the version attribute
151 16
            if (!in_array($this->data->type, ['IntParam', 'Order'])) {
152 13
                if (empty($this->data->stock)) {
153 12
                    $xml->addAttribute($this->getLcFirstType() . 'Version', '2.0');
154
                }
155
            }
156
157 16
            if (isset($this->data->{$this->getLcFirstType() . 'Type'})) {
158 5
                $xml->addAttribute($this->getLcFirstType() . 'Type', \strval($this->data->{$this->getLcFirstType() . 'Type'}));
159
            }
160
161 16
            if ('Order' == $this->data->type && !empty($this->data->order)) {
162 1
                foreach ($this->data->order as $orderElement) {
163 1
                    $this->appendNode($xml, $orderElement->getXML());
164
                }
165
166 15
            } elseif ('Stock' == $this->data->type && !empty($this->data->stock)) {
167 1
                foreach ($this->data->stock as $stockElement) {
168
                    // set namespace
169 1
                    $stockElement->setNamespace('lStk');
170 1
                    $this->appendNode($xml, $stockElement->getXML());
171
                }
172
173
            } else {
174 14
                $request = $xml->addChild($this->data->namespace . ':' . $this->whichDirection($this->directionAsResponse) . $this->data->type);
175
176 14
                $this->addElements($request, ['limit', 'filter', 'userFilterName'], 'ftr');
177
            }
178
179 16
            if (isset($this->data->restrictionData)) {
180 1
                $this->addElements($xml, ['restrictionData'], 'lst');
181
            }
182
        }
183
184 17
        if (isset($this->data->timestamp)) {
185 17
            $date = $this->data->timestamp;
186 17
            if (is_object($date) && is_a($date, \DateTimeInterface::class)) {
187 17
                $date = $date->format('Y-m-d\TH:i:s');
188
            }
189 17
            $xml->addAttribute('dateTimeStamp', \strval($date));
190
        }
191
192 17
        if (isset($this->data->validFrom)) {
193 4
            $dateFrom = $this->data->validFrom;
194 4
            if (is_object($dateFrom) && is_a($dateFrom, \DateTimeInterface::class)) {
195 4
                $dateFrom = $dateFrom->format('Y-m-d');
196
            }
197 4
            $xml->addAttribute('dateValidFrom', \strval($dateFrom));
198
        }
199
200 17
        if (isset($this->data->state)) {
201 17
            $xml->addAttribute('state', \strval($this->data->state));
202
        }
203
204
205 17
        return $xml;
206
    }
207
208 14
    protected function whichDirection(bool $asResponse): string
209
    {
210 14
        return $asResponse ? 'response' : 'request';
211
    }
212
213
    /**
214
     * Get LC first type name.
215
     *
216
     * @return string
217
     */
218 16
    protected function getLcFirstType(): string
219
    {
220
        // ActionPrice is custom
221 16
        if ('ActionPrice' == $this->data->type) {
222 1
            return 'actionPrices';
223
        }
224
225 15
        return \lcfirst(\strval($this->data->type));
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 17
    protected function getDefaultDto(): Common\Dtos\AbstractDto
232
    {
233 17
        return new ListResponse\ListResponseDto();
234
    }
235
}
236