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

Commit

Permalink
feat(Types): add request to get type by key
Browse files Browse the repository at this point in the history
Closes #169
  • Loading branch information
Jens Schulze committed Feb 15, 2016
1 parent e66bd4b commit 2b34ae9
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
6 changes: 6 additions & 0 deletions features/request/Type/TypeDelete.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ Feature: I want to delete a type
And i want to delete a "type"
Then the path should be "types/id?version=1"
And the method should be "DELETE"

Scenario: Delete type
Given a "type" is identified by key "typeKey" and version 1
And i want to delete a "type"
Then the path should be "types/key=typeKey?version=1"
And the method should be "DELETE"
88 changes: 88 additions & 0 deletions src/Request/Types/TypeByKeyGetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Types;

use Commercetools\Core\Client\HttpMethod;
use Commercetools\Core\Client\HttpRequest;
use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractApiRequest;
use Commercetools\Core\Model\Type\Type;
use Commercetools\Core\Request\ExpandTrait;
use Commercetools\Core\Request\PageTrait;
use Commercetools\Core\Request\QueryTrait;
use Commercetools\Core\Request\StagedTrait;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Response\ResourceResponse;
use Psr\Http\Message\ResponseInterface;

/**
* @package Commercetools\Core\Request\Types
* @method Type mapResponse(ApiResponseInterface $response)
*/
class TypeByKeyGetRequest extends AbstractApiRequest
{
protected $resultClass = '\Commercetools\Core\Model\Type\Type';

use QueryTrait;
use StagedTrait;
use PageTrait;
use ExpandTrait;

/**
* @param string $key
* @param Context $context
*/
public function __construct($key, Context $context = null)
{
parent::__construct(TypesEndpoint::endpoint(), $context);
if (!is_null($key)) {
$this->where(sprintf('key="%1$s"', $key));
}
$this->limit(1);
}

/**
* @param string $key
* @param Context $context
* @return static
*/
public static function ofKey($key, Context $context = null)
{
return new static($key, $context);
}

/**
* @return HttpRequest
* @internal
*/
public function httpRequest()
{
return new HttpRequest(HttpMethod::GET, $this->getPath());
}

/**
* @param ResponseInterface $response
* @return ResourceResponse
*/
public function buildResponse(ResponseInterface $response)
{
return new ResourceResponse($response, $this, $this->getContext());
}

/**
* @param array $result
* @param Context $context
* @return Type|null
*/
public function mapResult(array $result, Context $context = null)
{
if (!empty($result['results'])) {
$data = current($result['results']);
return Type::fromArray($data, $context);
}
return null;
}
}
43 changes: 43 additions & 0 deletions src/Request/Types/TypeDeleteByKeyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Types;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
use Commercetools\Core\Request\AbstractDeleteRequest;
use Commercetools\Core\Model\Type\Type;
use Commercetools\Core\Response\ApiResponseInterface;

/**
* @package Commercetools\Core\Request\Types
* @link https://dev.commercetools.com/http-api-projects-types.html#delete-type-by-key
* @method Type mapResponse(ApiResponseInterface $response)
*/
class TypeDeleteByKeyRequest extends AbstractDeleteByKeyRequest
{
protected $resultClass = '\Commercetools\Core\Model\Type\Type';

/**
* @param string $id
* @param int $version
* @param Context $context
*/
public function __construct($id, $version, Context $context = null)
{
parent::__construct(TypesEndpoint::endpoint(), $id, $version, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, $context);
}
}

0 comments on commit 2b34ae9

Please # to comment.