Passed
Push — master ( 3f8301...c7321f )
by Jens
42:49 queued 17:59
created

buildResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Commercetools\Core\Request\ShippingMethods;
4
5
use Commercetools\Core\Client\HttpMethod;
6
use Commercetools\Core\Client\HttpRequest;
7
use Commercetools\Core\Model\Common\Collection;
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\JsonObjectMapper;
10
use Commercetools\Core\Model\MapperInterface;
11
use Commercetools\Core\Model\ShippingMethod\ShippingMethodCollection;
12
use Commercetools\Core\Request\AbstractApiRequest;
13
use Commercetools\Core\Request\ExpandTrait;
14
use Commercetools\Core\Response\ApiResponseInterface;
15
use Commercetools\Core\Response\PagedQueryResponse;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @package Commercetools\Core\Request\ShippingMethods
20
 * @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#get-shippingmethods-for-an-orderedit
21
 * @method ShippingMethodCollection mapResponse(ApiResponseInterface $response)
22
 * @method ShippingMethodCollection mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
23
 */
24
class ShippingMethodByMatchingOrderEditGetRequest extends AbstractApiRequest
25
{
26
    use ExpandTrait;
27
28
    protected $resultClass = ShippingMethodCollection::class;
29
30
    /**
31
     * @var string
32
     */
33
    protected $country;
34
35
    /**
36
     * @var string
37
     */
38
    protected $orderEditId;
39
40
    /**
41
     * @var string
42
     */
43
    protected $state;
44
45
    /**
46
     * @param string $orderEditId
47
     * @param string $country
48
     * @param Context $context
49
     */
50 8
    public function __construct($orderEditId, $country, Context $context = null)
51
    {
52 8
        parent::__construct(ShippingMethodsEndpoint::endpoint(), $context);
53 8
        $this->withOrderEditId($orderEditId);
54 8
        $this->withCountry($country);
55 8
    }
56
57
    /**
58
     * @param string $country
59
     * @return $this
60
     */
61 8
    public function withCountry($country)
62
    {
63 8
        return $this->addParam('country', $country);
64
    }
65
66
    /**
67
     * @param string $orderEditId
68
     * @return $this
69
     */
70 8
    public function withOrderEditId($orderEditId)
71
    {
72 8
        return $this->addParam('orderEditId', $orderEditId);
73
    }
74
75
    /**
76
     * @param string $state
77
     * @return $this
78
     */
79 2
    public function withState($state)
80
    {
81 2
        return $this->addParam('state', $state);
82
    }
83
84
    /**
85
     * @param string $orderEditId
86
     * @param string $country
87
     * @param Context $context
88
     * @return static
89
     */
90 8
    public static function ofOrderEditAndCountry($orderEditId, $country, Context $context = null)
91
    {
92 8
        return new static($orderEditId, $country, $context);
93
    }
94
95
    /**
96
     * @return HttpRequest
97
     * @internal
98
     */
99 5
    public function httpRequest()
100
    {
101 5
        return new HttpRequest(HttpMethod::GET, $this->getPath());
102
    }
103
104
    /**
105
     * @param ResponseInterface $response
106
     * @return PagedQueryResponse
107
     */
108 2
    public function buildResponse(ResponseInterface $response)
109
    {
110 2
        return new PagedQueryResponse($response, $this, $this->getContext());
111
    }
112
113
    /**
114
     * @return string
115
     * @internal
116
     */
117 5
    protected function getPath()
118
    {
119 5
        return (string)$this->getEndpoint() . '/matching-orderedit' .  $this->getParamString();
120
    }
121
122
    /**
123
     * @param array $result
124
     * @param Context $context
125
     * @param MapperInterface $mapper
126
     * @return Collection
127
     */
128 3
    public function map(array $result, Context $context = null, MapperInterface $mapper = null)
129
    {
130 3
        $data = [];
131 3
        if (!empty($result['results'])) {
132 1
            $data = $result['results'];
133
        }
134 3
        if (is_null($mapper)) {
135 3
            $mapper = JsonObjectMapper::of($context);
136
        }
137 3
        return $mapper->map($data, $this->resultClass);
138
    }
139
}
140