Passed
Push — master ( 0d508a...6e2a06 )
by Jens
16:22 queued 10s
created

OrderEditApplyRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Commercetools\Core\Request\OrderEdits;
7
8
use Commercetools\Core\Client\HttpMethod;
9
use Commercetools\Core\Client\JsonRequest;
10
use Commercetools\Core\Model\Common\Context;
11
use Commercetools\Core\Model\OrderEdit\OrderEdit;
12
use Commercetools\Core\Request\AbstractApiRequest;
13
use Commercetools\Core\Response\ResourceResponse;
14
use Psr\Http\Message\ResponseInterface;
15
use Commercetools\Core\Response\ApiResponseInterface;
16
use Commercetools\Core\Model\MapperInterface;
17
18
/**
19
 * @package Commercetools\Core\Request\OrderEdits
20
 *
21
 * @method OrderEdit mapResponse(ApiResponseInterface $response)
22
 * @method OrderEdit mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
23
 */
24
class OrderEditApplyRequest extends AbstractApiRequest
25
{
26
    protected $resultClass = OrderEdit::class;
27
28
    const EDIT_VERSION = 'editVersion';
29
    const RESOURCE_VERSION = 'resourceVersion';
30
31
    /**
32
     * @var string
33
     */
34
    protected $id;
35
36
    /**
37
     * @var int
38
     */
39
    protected $version;
40
    /**
41
     * @var int
42
     */
43
    protected $resourceVersion;
44
45
    /**
46
     * OrderEditApplyRequest constructor.
47
     * @param $id
48
     * @param $version
49
     * @param $resourceVersion
50
     * @param Context|null $context
51
     */
52 8
    public function __construct($id, $version, $resourceVersion, Context $context = null)
53
    {
54 8
        parent::__construct(OrderEditsEndpoint::endpoint(), $context);
55 8
        $this->id = $id;
56 8
        $this->version = $version;
57 8
        $this->resourceVersion = $resourceVersion;
58 8
    }
59
60
    /**
61
     * @param string $id
62
     * @param int $version
63
     * @param $resourceVersion
64
     * @param Context $context
65
     * @return static
66
     */
67 8
    public static function ofIdVersionAndResourceVersion($id, $version, $resourceVersion, Context $context = null)
68
    {
69 8
        return new static($id, $version, $resourceVersion, $context);
70
    }
71
72
    /**
73
     * @return string
74
     * @internal
75
     */
76 5
    protected function getPath()
77
    {
78 5
        return (string)$this->getEndpoint() . '/' .  $this->id . '/apply' . $this->getParamString();
79
    }
80
81
    /**
82
     * @return JsonRequest
83
     * @internal
84
     */
85 5
    public function httpRequest()
86
    {
87
        $payload = [
88 5
            static::EDIT_VERSION => (int)$this->version,
89 5
            static::RESOURCE_VERSION => (int)$this->resourceVersion,
90
        ];
91 5
        return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
92
    }
93
94
    /**
95
     * @param ResponseInterface $response
96
     * @return ResourceResponse
97
     * @internal
98
     */
99 3
    public function buildResponse(ResponseInterface $response)
100
    {
101 3
        return new ResourceResponse($response, $this, $this->getContext());
102
    }
103
}
104