Test Failed
Pull Request — master (#46)
by Yuji
04:29
created

GetItemStockInfoRequest::validateParams()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 5
nop 0
1
<?php
2
3
namespace Shippinno\YahooShoppingJp\Request;
4
5
use InvalidArgumentException;
6
use LogicException;
7
use Shippinno\YahooShoppingJp\Api\GetItemStockInfoApi;
8
use Shippinno\YahooShoppingJp\Exception\InvalidRequestException;
9
use Shippinno\YahooShoppingJp\Response\GetItemStockInfoResponse;
10
11
class GetItemStockInfoRequest extends AbstractRequest
12
{
13
14
15
    /**
16
     * @return GetItemStockInfoApi
17
     */
18
    public function api()
19
    {
20
        return new GetItemStockInfoApi;
21
    }
22
23
    /**
24
     * @return GetItemStockInfoResponse
25
     */
26
    public function response()
27
    {
28
        return new GetItemStockInfoResponse;
29
    }
30
31
    /**
32
     * @throws InvalidRequestException
33
     */
34
    protected function validateParams(): void
35
    {
36
        if (! isset($this->params['seller_id'])) {
37
            throw new InvalidRequestException('seller_id is not set.');
38
        }
39
40
        if (! isset($this->params['itemCodeList'])
41
            ||  !is_array($this->params['itemCodeList'])
42
            || count($this->params['itemCodeList']) == 0) {
43
            throw new InvalidRequestException('item_code is not set.');
44
        }
45
46
        if (count(array_unique($this->params['itemCodeList'])) < count($this->params['itemCodeList'])) {
47
            throw new LogicException('Some of item_code are duplicated.');
48
        }
49
50
        if (count($this->params['itemCodeList']) >= 1000) {
51
            throw new LogicException('The number of the item_code must be less than 1000.');
52
        }
53
    }
54
55
    /**
56
     * @param string $sellerId
57
     * @return self
58
     */
59
    public function setSellerId(string $sellerId): self
60
    {
61
        if (isset($this->params['seller_id'])) {
62
            throw new LogicException('seller_id is already set.');
63
        }
64
65
        $this->params['seller_id'] = $sellerId;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $itemCode
72
     * @return self
73
     */
74
    public function addItemCode(string $itemCode): self
75
    {
76
        if (strlen($itemCode) === 0) {
77
            throw new InvalidArgumentException('The item_code cannot be empty.');
78
        }
79
80
        if (strlen($itemCode) > 99) {
81
            throw new InvalidArgumentException('The item_code must be less than 99 characters.');
82
        }
83
84
        $this->params['itemCodeList'][] = $itemCode;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getParams(): array
93
    {
94
        $this->validateParams();
95
96
        $this->params['item_code'] = implode(',', $this->params['itemCodeList']);
97
98
        return $this->params;
99
    }
100
101
102
}
103