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

Commit 93b62de

Browse files
committed
feat(api-clients): add support for managing api clients
1 parent d25e3ed commit 93b62de

File tree

12 files changed

+474
-0
lines changed

12 files changed

+474
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
// phpcs:disable Generic.Files.LineLength
3+
namespace Commercetools\Core\Builder\Request;
4+
5+
use Commercetools\Core\Request\ApiClients\ApiClientByIdGetRequest;
6+
use Commercetools\Core\Request\ApiClients\ApiClientCreateRequest;
7+
use Commercetools\Core\Model\ApiClient\ApiClientDraft;
8+
use Commercetools\Core\Request\ApiClients\ApiClientDeleteRequest;
9+
use Commercetools\Core\Model\ApiClient\ApiClient;
10+
use Commercetools\Core\Request\ApiClients\ApiClientQueryRequest;
11+
12+
class ApiClientRequestBuilder
13+
{
14+
15+
/**
16+
*
17+
* @param string $id
18+
* @return ApiClientByIdGetRequest
19+
*/
20+
public function getById($id)
21+
{
22+
$request = ApiClientByIdGetRequest::ofId($id);
23+
return $request;
24+
}
25+
26+
/**
27+
*
28+
* @param ApiClientDraft $apiClientDraft
29+
* @return ApiClientCreateRequest
30+
*/
31+
public function create(ApiClientDraft $apiClientDraft)
32+
{
33+
$request = ApiClientCreateRequest::ofDraft($apiClientDraft);
34+
return $request;
35+
}
36+
37+
/**
38+
*
39+
* @param ApiClient $apiClient
40+
* @return ApiClientDeleteRequest
41+
*/
42+
public function delete(ApiClient $apiClient)
43+
{
44+
$request = ApiClientDeleteRequest::ofIdAndVersion($apiClient->getId(), $apiClient->getVersion());
45+
return $request;
46+
}
47+
48+
/**
49+
*
50+
*
51+
* @return ApiClientQueryRequest
52+
*/
53+
public function query()
54+
{
55+
$request = ApiClientQueryRequest::of();
56+
return $request;
57+
}
58+
59+
/**
60+
* @return ApiClientRequestBuilder
61+
*/
62+
public function of()
63+
{
64+
return new self();
65+
}
66+
}

src/Core/Builder/Request/RequestBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
class RequestBuilder
99
{
10+
/**
11+
* @return ApiClientRequestBuilder
12+
*/
13+
public function apiClients()
14+
{
15+
return new ApiClientRequestBuilder();
16+
}
17+
1018
/**
1119
* @return CartDiscountRequestBuilder
1220
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Model\ApiClient;
6+
7+
use Commercetools\Core\Model\Common\DateDecorator;
8+
use Commercetools\Core\Model\Common\DateTimeDecorator;
9+
use Commercetools\Core\Model\Common\JsonObject;
10+
use DateTime;
11+
12+
/**
13+
* @package Commercetools\Core\Model\ApiClient
14+
*
15+
* @method string getId()
16+
* @method ApiClient setId(string $id = null)
17+
* @method string getName()
18+
* @method ApiClient setName(string $name = null)
19+
* @method string getScope()
20+
* @method ApiClient setScope(string $scope = null)
21+
* @method DateTimeDecorator getCreatedAt()
22+
* @method ApiClient setCreatedAt(DateTime $createdAt = null)
23+
* @method DateDecorator getLastUsedAt()
24+
* @method ApiClient setLastUsedAt(DateTime $lastUsedAt = null)
25+
* @method string getSecret()
26+
* @method ApiClient setSecret(string $secret = null)
27+
*/
28+
class ApiClient extends JsonObject
29+
{
30+
public function fieldDefinitions()
31+
{
32+
return [
33+
'id' => [static::TYPE => 'string'],
34+
'name' => [static::TYPE => 'string'],
35+
'scope' => [static::TYPE => 'string'],
36+
'createdAt' => [
37+
static::TYPE => DateTime::class,
38+
static::DECORATOR => DateTimeDecorator::class
39+
],
40+
'lastUsedAt' => [
41+
static::TYPE => DateTime::class,
42+
static::DECORATOR => DateDecorator::class
43+
],
44+
'secret' => [static::TYPE => 'string']
45+
];
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Model\ApiClient;
6+
7+
use Commercetools\Core\Model\Common\Collection;
8+
9+
/**
10+
* @package Commercetools\Core\Model\ApiClient
11+
* @method ApiClientCollection add(ApiClient $element)
12+
* @method ApiClient current()
13+
* @method ApiClient getAt($offset)
14+
* @method ApiClient getById($offset)
15+
*/
16+
class ApiClientCollection extends Collection
17+
{
18+
protected $type = ApiClient::class;
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Model\ApiClient;
6+
7+
use Commercetools\Core\Model\Common\JsonObject;
8+
9+
/**
10+
* @package Commercetools\Core\Model\ApiClient
11+
*
12+
* @method string getName()
13+
* @method ApiClientDraft setName(string $name = null)
14+
* @method string getScope()
15+
* @method ApiClientDraft setScope(string $scope = null)
16+
*/
17+
class ApiClientDraft extends JsonObject
18+
{
19+
public function fieldDefinitions()
20+
{
21+
return [
22+
'name' => [static::TYPE => 'string'],
23+
'scope' => [static::TYPE => 'string']
24+
];
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ApiClients;
6+
7+
use Commercetools\Core\Model\ApiClient\ApiClient;
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractByIdGetRequest;
10+
use Commercetools\Core\Response\ApiResponseInterface;
11+
use Commercetools\Core\Model\MapperInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\ApiClients
15+
* @method ApiClient mapResponse(ApiResponseInterface $response)
16+
* @method ApiClient mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
17+
*/
18+
class ApiClientByIdGetRequest extends AbstractByIdGetRequest
19+
{
20+
protected $resultClass = ApiClient::class;
21+
22+
/**
23+
* @param string $id
24+
* @param Context $context
25+
*/
26+
public function __construct($id, Context $context = null)
27+
{
28+
parent::__construct(ApiClientsEndpoint::endpoint(), $id, $context);
29+
}
30+
31+
/**
32+
* @param string $id
33+
* @param Context $context
34+
* @return static
35+
*/
36+
public static function ofId($id, Context $context = null)
37+
{
38+
return new static($id, $context);
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ApiClients;
6+
7+
use Commercetools\Core\Model\ApiClient\ApiClient;
8+
use Commercetools\Core\Model\ApiClient\ApiClientDraft;
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractCreateRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\MapperInterface;
13+
14+
/**
15+
* @package Commercetools\Core\Request\ApiClients
16+
* @method ApiClient mapResponse(ApiResponseInterface $response)
17+
* @method ApiClient mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ApiClientCreateRequest extends AbstractCreateRequest
20+
{
21+
protected $resultClass = ApiClient::class;
22+
23+
/**
24+
* @param ApiClientDraft $apiClientDraft
25+
* @param Context $context
26+
*/
27+
public function __construct(ApiClientDraft $apiClientDraft, Context $context = null)
28+
{
29+
parent::__construct(ApiClientsEndpoint::endpoint(), $apiClientDraft, $context);
30+
}
31+
32+
/**
33+
* @param ApiClientDraft $apiClientDraft
34+
* @param Context $context
35+
* @return static
36+
*/
37+
public static function ofDraft(ApiClientDraft $apiClientDraft, Context $context = null)
38+
{
39+
return new static($apiClientDraft, $context);
40+
}
41+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ApiClients;
6+
7+
use Commercetools\Core\Client\HttpMethod;
8+
use Commercetools\Core\Client\HttpRequest;
9+
use Commercetools\Core\Model\ApiClient\ApiClient;
10+
use Commercetools\Core\Model\Common\Context;
11+
use Commercetools\Core\Request\AbstractApiRequest;
12+
use Commercetools\Core\Response\ResourceResponse;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Commercetools\Core\Response\ApiResponseInterface;
15+
use Commercetools\Core\Model\MapperInterface;
16+
17+
/**
18+
* @package Commercetools\Core\Request\ApiClients
19+
* @method ApiClient mapResponse(ApiResponseInterface $response)
20+
* @method ApiClient mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
21+
*/
22+
class ApiClientDeleteRequest extends AbstractApiRequest
23+
{
24+
protected $resultClass = ApiClient::class;
25+
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $id;
31+
32+
/**
33+
* @param string $id
34+
* @param Context $context
35+
*/
36+
public function __construct($id, Context $context = null)
37+
{
38+
parent::__construct(ApiClientsEndpoint::endpoint(), $context);
39+
$this->setId($id);
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getId()
46+
{
47+
return $this->id;
48+
}
49+
50+
/**
51+
* @param $id
52+
* @return $this
53+
*/
54+
public function setId($id)
55+
{
56+
$this->id = $id;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @return string
63+
* @internal
64+
*/
65+
protected function getPath()
66+
{
67+
return (string)$this->getEndpoint() . '/' . $this->getId();
68+
}
69+
70+
/**
71+
* @return HttpRequest
72+
* @internal
73+
*/
74+
public function httpRequest()
75+
{
76+
return new HttpRequest(HttpMethod::DELETE, $this->getPath());
77+
}
78+
79+
/**
80+
* @param ResponseInterface $response
81+
* @return ResourceResponse
82+
* @internal
83+
*/
84+
public function buildResponse(ResponseInterface $response)
85+
{
86+
return new ResourceResponse($response, $this, $this->getContext());
87+
}
88+
89+
/**
90+
* @param string $id
91+
* @param Context $context
92+
* @return static
93+
*/
94+
public static function ofId($id, Context $context = null)
95+
{
96+
return new static($id, $context);
97+
}
98+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ApiClients;
6+
7+
use Commercetools\Core\Model\ApiClient\ApiClientCollection;
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractQueryRequest;
10+
use Commercetools\Core\Response\ApiResponseInterface;
11+
use Commercetools\Core\Model\MapperInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\ApiClients
15+
* @method ApiClientCollection mapResponse(ApiResponseInterface $response)
16+
* @method ApiClientCollection mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
17+
*/
18+
class ApiClientQueryRequest extends AbstractQueryRequest
19+
{
20+
protected $resultClass = ApiClientCollection::class;
21+
22+
/**
23+
* @param Context $context
24+
*/
25+
public function __construct(Context $context = null)
26+
{
27+
parent::__construct(ApiClientsEndpoint::endpoint(), $context);
28+
}
29+
30+
/**
31+
* @param Context $context
32+
* @return static
33+
*/
34+
public static function of(Context $context = null)
35+
{
36+
return new static($context);
37+
}
38+
}

0 commit comments

Comments
 (0)