Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: Preliminary Checkout Generic API #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
"branch-alias": {
"dev-master": "1.0-dev"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
7 changes: 7 additions & 0 deletions src/ApiResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sylius\Api;

use Sylius\Api\Map\UriMapInterface;
use Sylius\Api\Checkout\GenericApi as CheckoutGenericApi;

/**
* @author Michał Marcinkowski <[email protected]>
Expand All @@ -38,6 +39,12 @@ public function __construct(ClientInterface $client, UriMapInterface $uriMap)
*/
public function getApi($resource)
{
if (strpos($resource, 'checkout') === 0) {
return new CheckoutGenericApi(
$this->client,
$this->uriMap->getUri($resource)
);
}
return new GenericApi($this->client, $this->uriMap->getUri($resource));
}
}
40 changes: 40 additions & 0 deletions src/Checkout/GenericApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Sylius\Api\Checkout;

use Sylius\Api\GenericApi as BaseApi;

class GenericApi extends BaseApi
{
public function create(array $body, array $uriParameters = [], array $files = [])
{
$response = $this
->getClient()
->put(
$this->getUri(
$uriParameters,
$body,
$files
),
$body
)
;

return $response->getStatusCode() === 204;
}

public function update($id, array $body, array $uriParameters = [], array $files = [])
{
$uri = sprintf('%s%s', $this->getUri($uriParameters), $id);

$response = $this
->getClient()
->put(
$uri,
$body
)
;

return $response->getStatusCode() === 204;
}
}
9 changes: 7 additions & 2 deletions src/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private function setUri($uri)
* @param array $uriParameters
* @return string
*/
private function getUri(array $uriParameters = [])
protected function getUri(array $uriParameters = [])
{
$uri = $this->uri;
foreach ($uriParameters as $uriParameterKey => $uriParameterValue) {
Expand Down Expand Up @@ -167,7 +167,7 @@ public function delete($id, array $uriParameters = [])
return (204 === $response->getStatusCode());
}

private function responseToArray(ResponseInterface $response)
protected function responseToArray(ResponseInterface $response)
{
$responseType = $response->getHeader('Content-Type');
if ((false === strpos($responseType, 'application/json')) && (false === strpos($responseType, 'application/xml'))) {
Expand All @@ -176,4 +176,9 @@ private function responseToArray(ResponseInterface $response)

return (strpos($responseType, 'application/json') !== false) ? $response->json() : $response->xml();
}

protected function getClient()
{
return $this->client;
}
}