CustomerLoginRequest::httpRequest()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.5555
c 1
b 0
f 1
cc 5
nc 16
nop 0
crap 30
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 * @created: 11.02.15, 15:53
5
 */
6
7
namespace Commercetools\Core\Request\Customers;
8
9
use Commercetools\Core\Model\Cart\Cart;
10
use Commercetools\Core\Model\Cart\CartReference;
11
use Commercetools\Core\Model\Common\ResourceIdentifier;
12
use Commercetools\Core\Model\Customer\CustomerDraft;
13
use Psr\Http\Message\ResponseInterface;
14
use Commercetools\Core\Client\HttpMethod;
15
use Commercetools\Core\Client\JsonRequest;
16
use Commercetools\Core\Model\Common\Context;
17
use Commercetools\Core\Request\AbstractApiRequest;
18
use Commercetools\Core\Response\ResourceResponse;
19
use Commercetools\Core\Model\Customer\CustomerSigninResult;
20
use Commercetools\Core\Response\ApiResponseInterface;
21
use Commercetools\Core\Model\MapperInterface;
22
23
/**
24
 * @package Commercetools\Core\Request\Customers
25
 * @link https://docs.commercetools.com/http-api-projects-customers.html#authenticate-customer-sign-in
26
 * @method CustomerSigninResult mapResponse(ApiResponseInterface $response)
27
 * @method CustomerSigninResult mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
28
 */
29
class CustomerLoginRequest extends AbstractApiRequest
30
{
31
    const EMAIL = 'email';
32
    const PASSWORD = 'password';
33
    const ANONYMOUS_CART = 'anonymousCart';
34
    const ANONYMOUS_CART_ID = 'anonymousCartId';
35
    const ANONYMOUS_CART_SIGN_IN_MODE = 'anonymousCartSignInMode';
36
    const SIGN_IN_MODE_MERGE = 'MergeWithExistingCustomerCart';
37
    const SIGN_IN_MODE_NEW = 'UseAsNewActiveCustomerCart';
38
    const UPDATE_PRODUCT_DATA = 'updateProductData';
39
40
    /**
41
     * @var string
42
     */
43
    protected $email;
44
45
    /**
46
     * @var string
47
     */
48
    protected $password;
49
50
    /**
51
     * @var CartReference
52
     */
53
    protected $anonymousCart;
54
55
    /**
56
     * @deprecated use $anonymousCart instead
57
     * @var string
58
     */
59
    protected $anonymousCartId;
60
61
    protected $anonymousCartSignInMode;
62
63
    /**
64
     * @var bool
65 19
     */
66
    protected $updateProductData;
67 19
68 19
    protected $resultClass = CustomerSigninResult::class;
69 19
70 19
    /**
71 19
     * @param string $email
72
     * @param string $password
73
     * @param CartReference|string|null $anonymousCart
74
     * @param Context $context
75
     */
76
    public function __construct($email, $password, $anonymousCart = null, Context $context = null)
77
    {
78
        parent::__construct(LoginEndpoint::endpoint(), $context);
79
        $this->email = $email;
80
        $this->password = $password;
81
        if ($anonymousCart instanceof CartReference) {
82
            $this->anonymousCart = $anonymousCart;
83
        } elseif ($anonymousCart !== null) {
84
            $this->anonymousCart = CartReference::ofId($anonymousCart);
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getEmail()
92
    {
93
        return $this->email;
94
    }
95
96
    /**
97
     * @param string $email
98
     * @return $this
99
     */
100
    public function setEmail($email)
101
    {
102
        $this->email = $email;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getPassword()
111
    {
112
        return $this->password;
113
    }
114
115
    /**
116
     * @param string $password
117
     * @return $this
118
     */
119
    public function setPassword($password)
120
    {
121
        $this->password = $password;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @deprecated use $getAnonymousCart instead
128
     * @return string
129
     */
130
    public function getAnonymousCartId()
131
    {
132
        if ($this->anonymousCart == null) {
133
            return null;
134
        }
135
        return $this->anonymousCart->getId();
136
    }
137
138
    /**
139
     * @deprecated use $setAnonymousCart instead
140
     * @param string $anonymousCartId
141
     * @return $this
142 3
     */
143
    public function setAnonymousCartId($anonymousCartId)
144 3
    {
145
        $this->anonymousCart = CartReference::ofId($anonymousCartId);
146 3
147
        return $this;
148
    }
149
150
    /**
151
     * @return mixed
152
     */
153
    public function getAnonymousCartSignInMode()
154
    {
155
        return $this->anonymousCartSignInMode;
156
    }
157
158
    /**
159
     * @param mixed $anonymousCartSignInMode
160
     * @return $this
161 12
     */
162
    public function setAnonymousCartSignInMode($anonymousCartSignInMode)
163 12
    {
164 12
        $this->anonymousCartSignInMode = $anonymousCartSignInMode;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    public function getUpdateProductData()
173
    {
174 7
        return $this->updateProductData;
175
    }
176 7
177
    /**
178
     * @param bool $updateProductData
179
     * @return $this
180
     */
181
    public function setUpdateProductData($updateProductData)
182
    {
183
        $this->updateProductData = $updateProductData;
184
        return $this;
185
    }
186
187 12
    /**
188
     * @return CartReference
189
     */
190
    public function getAnonymousCart()
191
    {
192
        return $this->anonymousCart;
193
    }
194 12
195 12
    /**
196
     * @param CartReference $anonymousCart
197 12
     * @return $this
198
     */
199
    public function setAnonymousCart($anonymousCart)
200
    {
201
        $this->anonymousCart = $anonymousCart;
202
203
        return $this;
204 16
    }
205
206
    /**
207 16
     * @param string $email
208 16
     * @param string $password
209
     * @param CartReference|string $anonymousCart
210 16
     * @param Context $context
211 5
     * @return static
212
     */
213 16
    public static function ofEmailAndPassword($email, $password, $anonymousCart = null, Context $context = null)
214 3
    {
215
        return new static($email, $password, $anonymousCart, $context);
216 16
    }
217 12
218
    /**
219 16
     * @param string $email
220
     * @param string $password
221
     * @param bool $updateProductData
222
     * @param CartReference|string $anonymousCart
223
     * @param Context $context
224
     * @return static
225
     */
226
    public static function ofEmailPasswordAndUpdateProductData(
227 10
        $email,
228
        $password,
229 10
        $updateProductData,
230
        $anonymousCart = null,
231
        Context $context = null
232
    ) {
233
        $request = new static($email, $password, $anonymousCart, $context);
234
        $request->setUpdateProductData($updateProductData);
235
236
        return $request;
237
    }
238
239
    /**
240
     * @return JsonRequest
241
     * @internal
242
     */
243
    public function httpRequest()
244
    {
245
        $payload = [
246
            static::EMAIL => $this->email,
247
            static::PASSWORD => $this->password,
248
        ];
249
        if (!is_null($this->anonymousCartId)) {
0 ignored issues
show
introduced by
The condition is_null($this->anonymousCartId) is always false.
Loading history...
Deprecated Code introduced by
The property Commercetools\Core\Reque...quest::$anonymousCartId has been deprecated: use $anonymousCart instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

249
        if (!is_null(/** @scrutinizer ignore-deprecated */ $this->anonymousCartId)) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
250
            $payload[static::ANONYMOUS_CART_ID] = $this->anonymousCartId;
0 ignored issues
show
Deprecated Code introduced by
The property Commercetools\Core\Reque...quest::$anonymousCartId has been deprecated: use $anonymousCart instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

250
            $payload[static::ANONYMOUS_CART_ID] = /** @scrutinizer ignore-deprecated */ $this->anonymousCartId;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
251
        }
252
        if (!is_null($this->anonymousCart)) {
253
            $payload[static::ANONYMOUS_CART] = $this->anonymousCart;
254
        }
255
        if (!is_null($this->anonymousCartSignInMode)) {
256
            $payload[static::ANONYMOUS_CART_SIGN_IN_MODE] = $this->anonymousCartSignInMode;
257
        }
258
        if (!is_null($this->updateProductData)) {
0 ignored issues
show
introduced by
The condition is_null($this->updateProductData) is always false.
Loading history...
259
            $payload[static::UPDATE_PRODUCT_DATA] = $this->updateProductData;
260
        }
261
        return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
262
    }
263
264
    /**
265
     * @param ResponseInterface $response
266
     * @return ResourceResponse
267
     * @internal
268
     */
269
    public function buildResponse(ResponseInterface $response)
270
    {
271
        return new ResourceResponse($response, $this, $this->getContext());
272
    }
273
}
274