Stock::addCategory()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis\Pohoda;
6
7
/**
8
 * @property Stock\StockDto $data
9
 */
10
class Stock extends AbstractAgenda
11
{
12
    use Common\AddActionTypeTrait;
13
    use Common\AddParameterToHeaderTrait;
14
    use Common\SetNamespaceTrait;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 21
    public function setData(Common\Dtos\AbstractDto $data): parent
20
    {
21
        // pass to header
22 21
        if (!empty($data->header)) {
23 21
            $header = new Stock\Header($this->dependenciesFactory);
24 21
            $header
25 21
                ->setDirectionalVariable($this->useOneDirectionalVariables)
26 21
                ->setResolveOptions($this->resolveOptions)
27 21
                ->setData($data->header);
28 21
            $data->header = $header;
29
        }
30
31 21
        return parent::setData($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setData($data) returns the type kalanis\Pohoda\AbstractAgenda which is incompatible with the type-hinted return parent.
Loading history...
32
    }
33
34 1
    public function getImportRoot(): string
35
    {
36 1
        return 'lStk:stock';
37
    }
38
39
    /**
40
     * Add stock item.
41
     *
42
     * @param Stock\StockItemDto $data
43
     *
44
     * @return $this
45
     */
46 1
    public function addStockItem(Stock\StockItemDto $data): self
47
    {
48 1
        $stockDetail = new Stock\StockItem($this->dependenciesFactory);
49 1
        $stockDetail
50 1
            ->setDirectionalVariable($this->useOneDirectionalVariables)
51 1
            ->setResolveOptions($this->resolveOptions)
52 1
            ->setData($data);
53 1
        $this->data->stockDetail[] = $stockDetail;
54
55 1
        return $this;
56
    }
57
58
    /**
59
     * Add price.
60
     *
61
     * @param string $code
62
     * @param float  $value
63
     * @param string $id
64
     *
65
     * @return $this
66
     */
67 2
    public function addPrice(string $code, float $value, string $id = ''): self
68
    {
69 2
        $priceDto = new Stock\PriceDto();
70 2
        if (!empty($id)) {
71 1
            $priceDto->id = $id;
72
        }
73 2
        $priceDto->ids = $code;
74 2
        $priceDto->price = $value;
75 2
        $price = new Stock\Price($this->dependenciesFactory);
76 2
        $price
77 2
            ->setDirectionalVariable($this->useOneDirectionalVariables)
78 2
            ->setResolveOptions($this->resolveOptions)
79 2
            ->setData($priceDto);
80 2
        $this->data->stockPriceItem[] = $price;
81
82 2
        return $this;
83
    }
84
85
    /**
86
     * Add image.
87
     *
88
     * @param string   $filepath
89
     * @param string   $description
90
     * @param int|null $order
91
     * @param bool     $default
92
     *
93
     * @return $this
94
     */
95 1
    public function addImage(string $filepath, string $description = '', int $order = null, bool $default = false): self
96
    {
97 1
        $object = $this->data->header;
98 1
        if (\is_object($object) && \is_a($object, AbstractAgenda::class)) {
99 1
            $object->addImage($filepath, $description, $order, $default);
100
        }
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * Add category.
107
     *
108
     * @param int $categoryId
109
     *
110
     * @return $this
111
     */
112 1
    public function addCategory(int $categoryId): self
113
    {
114 1
        $object = $this->data->header;
115 1
        if (\is_object($object) && \is_a($object, AbstractAgenda::class)) {
116 1
            $object->addCategory($categoryId);
117
        }
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * Add int parameter.
124
     *
125
     * @param Stock\IntParameterDto $data
126
     *
127
     * @return $this
128
     */
129 1
    public function addIntParameter(Stock\IntParameterDto $data): self
130
    {
131 1
        $object = $this->data->header;
132 1
        if (\is_object($object) && \is_a($object, AbstractAgenda::class)) {
133 1
            $object->addIntParameter($data);
134
        }
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 19
    public function getXML(): \SimpleXMLElement
143
    {
144 19
        $namespace = empty($this->namespace) ? 'stk' : $this->namespace;
145 19
        $xml = $this->createXML()->addChild(
146 19
            ($this->useOneDirectionalVariables ? $namespace : 'stk'). ':stock',
147 19
            '',
148 19
            $this->namespace(($this->useOneDirectionalVariables ? $namespace : 'stk')),
149 19
        );
150 19
        $xml->addAttribute('version', '2.0');
151
152 19
        $this->addElements($xml, $this->getDataElements(), 'stk');
153
154 19
        return $xml;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 22
    protected function getDefaultDto(): Common\Dtos\AbstractDto
161
    {
162 22
        return new Stock\StockDto();
163
    }
164
}
165