Test Failed
Push — develop ( fab808...f55317 )
by Yuji
03:48
created

GetItemStockInfoRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A api() 0 4 1
A response() 0 4 1
A validateParams() 0 4 1
A setSellerId() 0 10 2
A addItemCode() 0 14 3
A getParams() 0 8 1
A validateRequest() 0 14 4
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
     * @return void
33
     */
34
    protected function validateParams()
35
    {
36
        // TODO: Implement validateParams() method.
37
    }
38
39
    /**
40
     * @param string $sellerId
41
     * @return self
42
     */
43
    public function setSellerId(string $sellerId): self
44
    {
45
        if (isset($this->params['seller_id'])) {
46
            throw new LogicException('seller_id is already set.');
47
        }
48
49
        $this->params['seller_id'] = $sellerId;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $itemCode
56
     * @return self
57
     */
58
    public function addItemCode(string $itemCode): self
59
    {
60
        if (strlen($itemCode) === 0) {
61
            throw new InvalidArgumentException('The item_code cannot be empty.');
62
        }
63
64
        if (strlen($itemCode) > 99) {
65
            throw new InvalidArgumentException('The item_code must be less than 99 characters.');
66
        }
67
68
        $this->params['itemCodeList'][] = $itemCode;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getParams(): array
77
    {
78
        $this->validateRequest();
79
80
        $this->params['item_code'] = implode(',', $this->params['itemCodeList']);
81
82
        return $this->params;
83
    }
84
85
    /**
86
     * @throws InvalidRequestException
87
     */
88
    private function validateRequest(): void
89
    {
90
        if (! isset($this->params['seller_id'])) {
91
            throw new InvalidRequestException;
92
        }
93
94
        if (count(array_unique($this->params['itemCodeList'])) < count($this->params['itemCodeList'])) {
95
            throw new LogicException('Some of item_code are duplicated.');
96
        }
97
98
        if (count($this->params['itemCodeList']) >= 1000) {
99
            throw new LogicException('The number of the item_code must be less than 1000.');
100
        }
101
    }
102
103
}
104