Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 8de3b30

Browse files
author
Jens Schulze
committed
feat(Customer): add get customer by email token request
1 parent f146aa7 commit 8de3b30

File tree

5 files changed

+144
-27
lines changed

5 files changed

+144
-27
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
* @created: 12.02.15, 10:35
5+
*/
6+
7+
namespace Commercetools\Core\Request;
8+
9+
use Commercetools\Core\Client\JsonEndpoint;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Commercetools\Core\Client\HttpMethod;
12+
use Commercetools\Core\Client\HttpRequest;
13+
use Commercetools\Core\Model\Common\Context;
14+
use Commercetools\Core\Response\ResourceResponse;
15+
16+
abstract class AbstractByTokenGetRequest extends AbstractApiRequest
17+
{
18+
const TOKEN = 'token';
19+
const TOKEN_NAME = 'token';
20+
21+
protected $token;
22+
23+
/**
24+
* @param string $token
25+
* @param Context $context
26+
*/
27+
public function __construct(JsonEndpoint $endpoint, $token, Context $context = null)
28+
{
29+
parent::__construct($endpoint, $context);
30+
$this->setToken($token);
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getToken()
37+
{
38+
return $this->token;
39+
}
40+
41+
/**
42+
* @param string $token
43+
* @return $this
44+
*/
45+
public function setToken($token)
46+
{
47+
$this->token = $token;
48+
49+
return $this;
50+
}
51+
52+
/**
53+
* @return string
54+
* @internal
55+
*/
56+
protected function getPath()
57+
{
58+
return (string)$this->getEndpoint() . '/' . static::TOKEN_NAME . '=' . $this->getToken() .
59+
$this->getParamString();
60+
}
61+
62+
/**
63+
* @return HttpRequest
64+
* @internal
65+
*/
66+
public function httpRequest()
67+
{
68+
return new HttpRequest(HttpMethod::GET, $this->getPath());
69+
}
70+
71+
/**
72+
* @param ResponseInterface $response
73+
* @return ResourceResponse
74+
* @internal
75+
*/
76+
public function buildResponse(ResponseInterface $response)
77+
{
78+
return new ResourceResponse($response, $this, $this->getContext());
79+
}
80+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
* @created: 12.02.15, 10:35
5+
*/
6+
7+
namespace Commercetools\Core\Request\Customers;
8+
9+
use Commercetools\Core\Request\AbstractByTokenGetRequest;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Commercetools\Core\Client\HttpMethod;
12+
use Commercetools\Core\Client\HttpRequest;
13+
use Commercetools\Core\Model\Common\Context;
14+
use Commercetools\Core\Request\AbstractApiRequest;
15+
use Commercetools\Core\Response\ResourceResponse;
16+
use Commercetools\Core\Model\Customer\Customer;
17+
use Commercetools\Core\Response\ApiResponseInterface;
18+
use Commercetools\Core\Model\MapperInterface;
19+
20+
/**
21+
* @package Commercetools\Core\Request\Customers
22+
* @link https://dev.commercetools.com/http-api-projects-customers.html#get-customer-by-email-token
23+
* @method Customer mapResponse(ApiResponseInterface $response)
24+
* @method Customer mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
25+
*/
26+
class CustomerByEmailTokenGetRequest extends AbstractByTokenGetRequest
27+
{
28+
const TOKEN_NAME = 'email-token';
29+
30+
protected $resultClass = Customer::class;
31+
/**
32+
* @param string $token
33+
* @param Context $context
34+
*/
35+
public function __construct($token, Context $context = null)
36+
{
37+
parent::__construct(CustomersEndpoint::endpoint(), $token, $context);
38+
}
39+
40+
/**
41+
* @param string $token
42+
* @param Context $context
43+
* @return static
44+
*/
45+
public static function ofToken($token, Context $context = null)
46+
{
47+
return new static($token, $context);
48+
}
49+
}

src/Core/Request/Customers/CustomerByTokenGetRequest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Commercetools\Core\Request\Customers;
88

9+
use Commercetools\Core\Request\AbstractByTokenGetRequest;
910
use Psr\Http\Message\ResponseInterface;
1011
use Commercetools\Core\Client\HttpMethod;
1112
use Commercetools\Core\Client\HttpRequest;
@@ -22,20 +23,18 @@
2223
* @method Customer mapResponse(ApiResponseInterface $response)
2324
* @method Customer mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
2425
*/
25-
class CustomerByTokenGetRequest extends AbstractApiRequest
26+
class CustomerByTokenGetRequest extends AbstractByTokenGetRequest
2627
{
27-
const TOKEN = 'token';
28+
const TOKEN_NAME = 'password-token';
2829

2930
protected $resultClass = Customer::class;
30-
3131
/**
3232
* @param string $token
3333
* @param Context $context
3434
*/
3535
public function __construct($token, Context $context = null)
3636
{
37-
parent::__construct(CustomersEndpoint::endpoint(), $context);
38-
$this->addParam(static::TOKEN, $token);
37+
parent::__construct(CustomersEndpoint::endpoint(), $token, $context);
3938
}
4039

4140
/**
@@ -47,23 +46,4 @@ public static function ofToken($token, Context $context = null)
4746
{
4847
return new static($token, $context);
4948
}
50-
51-
/**
52-
* @return HttpRequest
53-
* @internal
54-
*/
55-
public function httpRequest()
56-
{
57-
return new HttpRequest(HttpMethod::GET, $this->getPath());
58-
}
59-
60-
/**
61-
* @param ResponseInterface $response
62-
* @return ResourceResponse
63-
* @internal
64-
*/
65-
public function buildResponse(ResponseInterface $response)
66-
{
67-
return new ResourceResponse($response, $this, $this->getContext());
68-
}
6949
}

tests/integration/Customer/CustomerLoginRequestTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Commercetools\Core\Request\Carts\CartByIdGetRequest;
1818
use Commercetools\Core\Request\Carts\CartCreateRequest;
1919
use Commercetools\Core\Request\Carts\CartDeleteRequest;
20+
use Commercetools\Core\Request\Customers\CustomerByEmailTokenGetRequest;
2021
use Commercetools\Core\Request\Customers\CustomerByTokenGetRequest;
2122
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
2223
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
@@ -251,6 +252,13 @@ public function testVerifyEmail()
251252
$token = $result->getValue();
252253
$this->assertNotEmpty($token);
253254

255+
$request = CustomerByEmailTokenGetRequest::ofToken(
256+
$token
257+
);
258+
$response = $request->executeWithClient($this->getClient());
259+
$result = $request->mapResponse($response);
260+
$this->assertSame($customer->getId(), $result->getId());
261+
254262
$request = CustomerEmailConfirmRequest::ofToken(
255263
$token
256264
);
@@ -278,10 +286,10 @@ public function testAuthLogin()
278286
{
279287
$draft = $this->getDraft('email');
280288
$this->createCustomer($draft);
281-
289+
282290
$email = $draft->getEmail();
283291
$password = $draft->getPassword();
284-
292+
285293
$config = $this->getClientConfig('view_products');
286294
$config->setGrantType(Config::GRANT_TYPE_PASSWORD)->setUsername($email)->setPassword($password);
287295

tests/unit/Request/Customers/CustomerByTokenGetRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testHttpRequestPath()
2626
$request = CustomerByTokenGetRequest::ofToken('myToken');
2727
$httpRequest = $request->httpRequest();
2828

29-
$this->assertSame('customers?token=myToken', (string)$httpRequest->getUri());
29+
$this->assertSame('customers/password-token=myToken', (string)$httpRequest->getUri());
3030
}
3131

3232
public function testHttpRequestObject()

0 commit comments

Comments
 (0)