Test Failed
Pull Request — master (#19)
by Aya
04:08
created

SearchOrdersRequest::setOrderedDateTimeRange()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 30
Code Lines 16

Duplication

Lines 6
Ratio 20 %

Importance

Changes 0
Metric Value
dl 6
loc 30
rs 4.8196
c 0
b 0
f 0
nc 8
cc 10
eloc 16
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Shippinno\YahooShoppingJp\Request;
4
5
use DateTimeImmutable;
6
use FluidXml\FluidXml;
7
use InvalidArgumentException;
8
use LogicException;
9
use Shippinno\YahooShoppingJp\Exception\InvalidRequestException;
10
11
class SearchOrdersRequest 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...
12
{
13
    private $params = [];
14
15
    public function __construct()
16
    {
17
        $this->params['Search']['Field'] = implode(',', [
18
            'OrderId', 'Version', 'OriginalOrderId', 'ParentOrderId', 'DeviceType', 'IsSeen', 'IsSplit', 'IsRoyalty',
19
            'IsSeller', 'IsAffiliate', 'IsRatingB2s', 'OrderTime', 'ExistMultiReleaseDate', 'ReleaseDate',
20
            'LastUpdateTime', 'Suspect', 'OrderStatus', 'StoreStatus', 'RoyaltyFixTime', 'PrintSlipFlag',
21
            'PrintDeliveryFlag', 'PrintBillFlag', 'BuyerCommentsFlag', 'PayStatus', 'SettleStatus', 'PayType',
22
            'PayMethod', 'PayMethodName', 'PayDate', 'SettleId', 'UseWallet', 'NeedBillSlip', 'NeedDetailedSlip',
23
            'NeedReceipt', 'BillFirstName', 'BillFirstNameKana', 'BillLastName', 'BillLastNameKana', 'BillPrefecture',
24
            'ShipStatus', 'ShipMethod', 'ShipRequestDate', 'ShipRequestTime', 'ShipNotes', 'ShipInvoiceNumber1',
25
            'ShipInvoiceNumber2', 'ArriveType', 'ShipDate', 'NeedGiftWrap', 'NeedGiftWrapMessage', 'NeedGiftWrapPaper',
26
            'ShipFirstName', 'ShipFirstNameKana', 'ShipLastName', 'ShipLastNameKana', 'ShipPrefecture', 'PayCharge',
27
            'ShipCharge', 'GiftWrapCharge', 'Discount', 'UsePoint', 'TotalPrice', 'RefundTotalPrice', 'UsePointType',
28
            'IsGetPointFixAll', 'SellerId', 'IsLogin', 'PayNo', 'PayNoIssueDate', 'SellerType', 'IsPayManagement',
29
            'ShipUrl', 'ShipMethodName', 'ArrivalDate', 'TotalMallCouponDiscount',
30
        ]);
31
    }
32
33
    /**
34
     * @param string $sellerId
35
     * @return self
36
     */
37 View Code Duplication
    public function setSellerId(string $sellerId): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        if (isset($this->params['SellerId'])) {
40
            throw new LogicException('SellerId is already set.');
41
        }
42
43
        $this->params['SellerId'] = $sellerId;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param null|DateTimeImmutable $from
50
     * @param null|DateTimeImmutable $to
51
     * @return self
52
     */
53
    public function setOrderedDateTimeRange(DateTimeImmutable $from = null, DateTimeImmutable $to = null): self
54
    {
55
        if (null === $from && null === $to) {
56
            throw new InvalidArgumentException('Either OrderTimeFrom or OrderTimeTo has to be set.');
57
        }
58
59
        if (null !== $from &&
60
            null !== $to &&
61
            $from > $to) {
62
            throw new LogicException('OrderTimeFrom has to be earlier than OrderTimeTo.');
63
        }
64
65
        if (isset($this->params['Search']['Condition']['OrderTimeFrom'])) {
66
            throw new LogicException('OrderTimeFrom is already set.');
67
        }
68
69
        if (isset($this->params['Search']['Condition']['OrderTimeTo'])) {
70
            throw new LogicException('OrderTimeTo is already set.');
71
        }
72
73 View Code Duplication
        if (null !== $from) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $this->params['Search']['Condition']['OrderTimeFrom'] = $from->format('YmdHis');
75
        }
76
77 View Code Duplication
        if (null !== $to) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            $this->params['Search']['Condition']['OrderTimeTo'] = $to->format('YmdHis');
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param int $offset
86
     * @return self
87
     */
88 View Code Duplication
    public function setOffset(int $offset): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        if ($offset < 0) {
91
            throw new InvalidArgumentException;
92
        }
93
94
        if (isset($this->params['Search']['Start'])) {
95
            throw new LogicException('Start is already set.');
96
        }
97
98
        $this->params['Search']['Start'] = $offset + 1;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param int $limit
105
     * @return self
106
     */
107 View Code Duplication
    public function setLimit(int $limit): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if ($limit < 0) {
110
            throw new InvalidArgumentException;
111
        }
112
113
        if (isset($this->params['Search']['Result'])) {
114
            throw new LogicException('Result is already set.');
115
        }
116
117
        $this->params['Search']['Result'] = $limit;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getParams()
126
    {
127
        $this->validateRequest();
128
129
        $fluidXml = new FluidXml('Req');
130
        $fluidXml->add($this->params);
131
132
        return $fluidXml->xml();
133
    }
134
135
    /**
136
     * @throws InvalidRequestException
137
     */
138
    private function validateRequest(): void
139
    {
140
        if (! isset($this->params['SellerId'])) {
141
            throw new InvalidRequestException;
142
        }
143
    }
144
}
145