Test Failed
Push — develop ( cf0637...ea0f43 )
by Yuji
08:02
created

GetItemStockInfoRequest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
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\Exception\InvalidRequestException;
8
9
class GetItemStockInfoRequest extends AbstractRequest
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: api, response, validateParams
Loading history...
10
{
11
    /**
12
     * @var array
13
     */
14
    private $params = [];
15
16
    /**
17
     * @param string $sellerId
18
     * @return self
19
     */
20
    public function setSellerId(string $sellerId): self
21
    {
22
        if (isset($this->params['seller_id'])) {
23
            throw new LogicException('seller_id is already set.');
24
        }
25
26
        $this->params['seller_id'] = $sellerId;
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param string $itemCode
33
     * @return self
34
     */
35
    public function addItemCode(string $itemCode): self
36
    {
37
        if (strlen($itemCode) === 0) {
38
            throw new InvalidArgumentException('The item_code cannot be empty.');
39
        }
40
41
        if (strlen($itemCode) > 99) {
42
            throw new InvalidArgumentException('The item_code must be less than 99 characters.');
43
        }
44
45
        $this->params['itemCodeList'][] = $itemCode;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getParams(): array
54
    {
55
        $this->validateRequest();
56
57
        $this->params['item_code'] = implode(',', $this->params['itemCodeList']);
58
59
        return $this->params;
60
    }
61
62
    /**
63
     * @throws InvalidRequestException
64
     */
65
    private function validateRequest(): void
66
    {
67
        if (! isset($this->params['seller_id'])) {
68
            throw new InvalidRequestException;
69
        }
70
71
        if (count(array_unique($this->params['itemCodeList'])) < count($this->params['itemCodeList'])) {
72
            throw new LogicException('Some of item_code are duplicated.');
73
        }
74
75
        if (count($this->params['itemCodeList']) >= 1000) {
76
            throw new LogicException('The number of the item_code must be less than 1000.');
77
        }
78
    }
79
80
}
81