Passed
Push — master ( a8f3a0...0e6cb9 )
by C.
02:17
created

BatchService::getBatchStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\PaazlConnector\Services;
14
15
use DateTime;
16
use Etrias\PaazlConnector\Processor\Processor;
17
use Etrias\PaazlConnector\ServiceType\Service as GeneralServiceType;
18
use Etrias\PaazlConnector\StructType\BatchStatusRequest;
19
use Etrias\PaazlConnector\StructType\BatchStatusResponse;
20
use Etrias\PaazlConnector\StructType\CloseBatchRequest;
21
use Etrias\PaazlConnector\StructType\CloseBatchResponse;
22
use Etrias\PaazlConnector\StructType\ListOpenBatchesRequest;
23
use Etrias\PaazlConnector\StructType\ListOpenBatchesResponse;
24
use Etrias\PaazlConnector\StructType\OpenBatchRequest;
25
use Etrias\PaazlConnector\StructType\OpenBatchResponse;
26
27
class BatchService
28
{
29
    use Processor;
30
31
    /**
32
     * @var SecurityServiceInterface
33
     */
34
    protected $securityService;
35
    /**
36
     * @var GeneralServiceType
37
     */
38
    protected $generalServiceType;
39
40
    /**
41
     * DocumentService constructor.
42
     *
43
     * @param GeneralServiceType       $generalServiceType
44
     * @param SecurityServiceInterface $securityService
45
     */
46
    public function __construct(GeneralServiceType $generalServiceType, SecurityServiceInterface $securityService)
47
    {
48
        $this->securityService = $securityService;
49
        $this->generalServiceType = $generalServiceType;
50
    }
51
52
    /**
53
     * @param null $distributor
54
     * @param null $shippingOption
55
     * @param null $country
56
     * @param null $targetWebShop
57
     *
58
     * @return OpenBatchResponse
59
     */
60
    public function openBatch(
61
        $distributor = null,
62
        $shippingOption = null,
63
        $country = null,
64
        $targetWebShop = null
65
    ) {
66
        $today = new DateTime();
67
        $hashInput = implode('', [
68
            $distributor,
69
            $shippingOption,
70
            $country,
71
            $today->format('Ymd'),
72
        ]);
73
74
        $request = new OpenBatchRequest(
75
            $this->securityService->getHash($hashInput),
76
            $this->generalServiceType->getWebShopId(),
77
            $targetWebShop,
78
            $distributor,
79
            $shippingOption,
80
            $country
81
        );
82
83
        $response = $this->generalServiceType->openBatch($request);
84
85
        return $this->processResponse($response, $this->generalServiceType);
86
    }
87
88
    /**
89
     * @param $batchId
90
     * @param null $targetWebShop
91
     *
92
     * @return CloseBatchResponse
93
     */
94
    public function closeBatch($batchId, $targetWebShop = null)
95
    {
96
        $request = new CloseBatchRequest(
97
            $this->securityService->getHash($batchId),
98
            $this->generalServiceType->getWebShopId(),
99
            $targetWebShop,
100
            $batchId
101
        );
102
103
        $response = $this->generalServiceType->closeBatch($request);
104
105
        return $this->processResponse($response, $this->generalServiceType);
106
    }
107
108
    /**
109
     * @param $batchId
110
     * @param null $targetWebShop
111
     *
112
     * @return BatchStatusResponse
113
     */
114
    public function getBatchStatus($batchId, $targetWebShop = null)
115
    {
116
        $request = new BatchStatusRequest(
117
            $this->securityService->getHash($batchId),
118
            $this->generalServiceType->getWebShopId(),
119
            $targetWebShop,
120
            $batchId
121
        );
122
123
        $response = $this->generalServiceType->batchStatus($request);
124
125
        return $this->processResponse($response, $this->generalServiceType);
126
    }
127
128
    /**
129
     * @param null $targetWebShop
130
     *
131
     * @return ListOpenBatchesResponse
132
     */
133 View Code Duplication
    public function listOpenBatches($targetWebShop = null)
134
    {
135
        $today = new DateTime();
136
        $request = new ListOpenBatchesRequest(
137
            $this->securityService->getHash($today->format('Ymd')),
138
            $this->generalServiceType->getWebShopId(),
139
            $targetWebShop
140
        );
141
142
        $response = $this->generalServiceType->listOpenBatches($request);
143
144
        return $this->processResponse($response, $this->generalServiceType);
145
    }
146
}
147