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

UpdateItemStockInfoRequest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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