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

UpdateItemStockInfoRequest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSellerId() 0 14 3
B setItemCode() 0 20 5
A setQuantity() 0 14 4
A setAllowOverdraft() 0 10 2
A getParams() 0 6 1
A validateRequest() 0 14 4
1
<?php
2
3
namespace Shippinno\YahooShoppingJp\Request;
4
5
use LogicException;
6
use Shippinno\YahooShoppingJp\Exception\InvalidRequestException;
7
8
class UpdateItemStockInfoRequest 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...
9
{
10
    private $params = [];
11
12
    /**
13
     * @param string $sellerId
14
     * @return self
15
     * @throws InvalidRequestException
16
     */
17
    public function setSellerId(string $sellerId): self
18
    {
19
        if (isset($this->params['seller_id'])) {
20
            throw new LogicException('seller_id is already set.');
21
        }
22
23
        if (! preg_match('/^[a-z0-9\-]{3,20}$/', $sellerId)) {
24
            throw new InvalidArgumentException;
25
        }
26
27
        $this->params['seller_id'] = $sellerId;
28
29
        return $this;
30
    }
31
32
    /**
33
     * @param string $itemCode
34
     * @param string $subCode
35
     * @return self
36
     * @throws InvalidRequestException
37
     */
38
    public function setItemCode(string $itemCode, string $subCode = ''): self
39
    {
40
        if (isset($this->params['item_code'])) {
41
            throw new LogicException('item_code is already set.');
42
        }
43
44
        if (! preg_match('/^[a-zA-Z0-9\-]{1,99}$/', $itemCode)) {
45
            throw new InvalidRequestException('item_code error.');
46
        }
47
        $this->params['item_code'] = $itemCode;
48
49
        if (strlen($subCode)) {
50
            if (! preg_match('/^[a-zA-Z0-9\-]{1,99}$/', $subCode)) {
51
                throw new InvalidRequestException('sub_code error.');
52
            }
53
            $this->params['item_code'] = $this->params['item_code'].':'.$subCode;
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param int|string $quantity
61
     * @return self
62
     * @throws InvalidRequestException
63
     */
64
    public function setQuantity($quantity): self
65
    {
66
        if (isset($this->params['quantity'])) {
67
            throw new LogicException('quantity is already set.');
68
        }
69
70
        if (! preg_match('/^((\+|-)?[0-9]{1,9}|INI)$/', $quantity)) {
71
            throw new InvalidRequestException('Only number or INI can be set.');
72
        }
73
74
        $this->params['quantity'] = is_int($quantity) ? strval($quantity) : $quantity;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param bool $allowOverdraft
81
     * @return self
82
     */
83
    public function setAllowOverdraft(bool $allowOverdraft): self
84
    {
85
        if (isset($this->params['allow_overdraft'])) {
86
            throw new LogicException('allow_overdraft is already set.');
87
        }
88
89
        $this->params['allow_overdraft'] = (int) $allowOverdraft;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getParams(): array
98
    {
99
        $this->validateRequest();
100
101
        return $this->params;
102
    }
103
104
    /**
105
     * @throws InvalidRequestException
106
     */
107
    private function validateRequest(): void
108
    {
109
        if (! isset($this->params['seller_id'])) {
110
            throw new InvalidRequestException('seller_id is not set.');
111
        }
112
113
        if (! isset($this->params['item_code'])) {
114
            throw new InvalidRequestException('item_code is not set.');
115
        }
116
117
        if (! isset($this->params['quantity'])) {
118
            throw new InvalidRequestException('quantity is not set.');
119
        }
120
    }
121
}
122