Skip to content

Commit

Permalink
Merge pull request #221 from plentymarkets/OverallEnhancements
Browse files Browse the repository at this point in the history
Overall enhancements
  • Loading branch information
jochenmanz authored Sep 17, 2017
2 parents 10077c0 + df18a21 commit 0380953
Show file tree
Hide file tree
Showing 292 changed files with 5,998 additions and 9,283 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php

php:
- 7.0
- 7.1

sudo: false

Expand All @@ -12,11 +12,11 @@ services:

env:
matrix:
- SHOPWARE_VERSION="5.2"
- SHOPWARE_VERSION="5.3"
global:
- PLUGIN_NAME=PlentyConnector
- SHOPWARE_DIRECTORY=${HOME}/shopware
- PLUGIN_DIRECTORY=${SHOPWARE_DIRECTORY}/custom/plugins
- SHOPWARE_DIRECTORY="${HOME}/shopware"
- PLUGIN_DIRECTORY="${SHOPWARE_DIRECTORY}/custom/plugins"

cache:
directories:
Expand Down
58 changes: 27 additions & 31 deletions Adapter/PlentymarketsAdapter/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace PlentymarketsAdapter\Client;

use Assert\Assertion;
use GuzzleHttp\Client as GuzzleClient;
use Closure;
use Exception;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use PlentyConnector\Connector\ConfigService\ConfigServiceInterface;
use PlentymarketsAdapter\Client\Exception\InvalidCredentialsException;
Expand Down Expand Up @@ -43,11 +43,11 @@ class Client implements ClientInterface
/**
* Client constructor.
*
* @param GuzzleClient $connection
* @param GuzzleClientInterface $connection
* @param ConfigServiceInterface $config
*/
public function __construct(
GuzzleClient $connection,
GuzzleClientInterface $connection,
ConfigServiceInterface $config
) {
$this->connection = $connection;
Expand All @@ -57,9 +57,9 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getIterator($path, array $criteria = [])
public function getIterator($path, array $criteria = [], Closure $prepareFunction = null)
{
return new Iterator($path, $this, $criteria);
return new Iterator($path, $this, $criteria, $prepareFunction);
}

/**
Expand All @@ -85,8 +85,6 @@ public function getTotal($path, array $criteria = [])
}

/**
* TODO: simplify login handling.
*
* {@inheritdoc}
*/
public function request($method, $path, array $params = [], $limit = null, $offset = null, array $options = [])
Expand Down Expand Up @@ -116,10 +114,14 @@ public function request($method, $path, array $params = [], $limit = null, $offs
try {
$response = $this->connection->send($request);

if (null === $response) {
throw InvalidResponseException::fromParams($method, $path, $options);
}

$body = $response->getBody();

if (null === $body) {
// throw
throw InvalidResponseException::fromParams($method, $path, $options);
}

$result = json_decode($body->getContents(), true);
Expand All @@ -143,7 +145,7 @@ public function request($method, $path, array $params = [], $limit = null, $offs

return $result;
} catch (ClientException $exception) {
if ($exception->hasResponse() && $exception->getResponse()->getStatusCode() === 401 && !$this->isLoginRequired($path) && $this->accessToken != null) {
if ($this->accessToken !== null && !$this->isLoginRequired($path) && null !== $exception->getResponse() && $exception->getResponse()->getStatusCode() === 401) {
// retry with fresh accessToken
$this->accessToken = null;

Expand All @@ -159,19 +161,9 @@ public function request($method, $path, array $params = [], $limit = null, $offs
);
}

throw $exception;
} catch (InvalidResponseException $exception) {
if ($retries < 3) {
sleep(10);

++$retries;

return $this->request($method, $path, $params, $limit, $offset);
}

throw $exception;
} catch (ServerException $exception) {
if ($exception->hasResponse() && $exception->getResponse()->getStatusCode() === 503 && $retries < 3) {
if ($retries < 3 && $exception->hasResponse() && $exception->getResponse()->getStatusCode() === 503) {
sleep(10);

++$retries;
Expand All @@ -189,12 +181,16 @@ public function request($method, $path, array $params = [], $limit = null, $offs
}

throw $exception;
} catch (ConnectException $exception) {
sleep(10);
} catch (Exception $exception) {
if ($retries < 3) {
sleep(10);

++$retries;
++$retries;

return $this->request($method, $path, $params, $limit, $offset);
return $this->request($method, $path, $params, $limit, $offset);
}

throw $exception;
}
}

Expand Down Expand Up @@ -222,7 +218,7 @@ private function isLoginRequired($path)
private function login()
{
if (null === $this->config->get('rest_username') || null === $this->config->get('rest_password')) {
throw new InvalidCredentialsException();
throw new InvalidCredentialsException('invalid creddentials');
}

$login = $this->request('POST', 'login', [
Expand Down Expand Up @@ -266,13 +262,13 @@ private function getOptions($limit, $offset, array $options)
*/
private function getPage($limit, $offset)
{
$page = 1;
$page = 1.0;

if (null !== $offset) {
$page = (int) (floor($offset / $limit) + 1);
$page = (floor($offset / $limit) + 1);
}

return $page;
return (int) $page;
}

/**
Expand All @@ -296,7 +292,7 @@ private function getUrl($path, array $options = [])
}

/**
* @param $url
* @param string $url
*
* @throws InvalidCredentialsException
*
Expand All @@ -305,7 +301,7 @@ private function getUrl($path, array $options = [])
private function getBaseUri($url)
{
if (null === $url) {
throw new InvalidCredentialsException();
throw new InvalidCredentialsException('invalid creddentials');
}

$parts = parse_url($url);
Expand Down
8 changes: 5 additions & 3 deletions Adapter/PlentymarketsAdapter/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PlentymarketsAdapter\Client;

use Closure;
use PlentymarketsAdapter\Client\Iterator\Iterator;

/**
Expand All @@ -22,12 +23,13 @@ interface ClientInterface
public function request($method, $path, array $params = [], $limit = null, $offset = null, array $options = []);

/**
* @param string $path
* @param array $criteria
* @param string $path
* @param array $criteria
* @param null|Closure $prepareFunction
*
* @return Iterator
*/
public function getIterator($path, array $criteria = []);
public function getIterator($path, array $criteria = [], Closure $prepareFunction = null);

/**
* @param string $path
Expand Down
58 changes: 46 additions & 12 deletions Adapter/PlentymarketsAdapter/Client/Iterator/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace PlentymarketsAdapter\Client\Iterator;

use Assert\Assertion;
use Closure;
use Countable;
use Iterator as BaseIterator;
use PlentymarketsAdapter\Client\Client;

/**
* Class Iterator
*/
class Iterator implements BaseIterator
class Iterator implements BaseIterator, Countable
{
/**
* @var Client
Expand All @@ -34,40 +36,56 @@ class Iterator implements BaseIterator
/**
* @var array
*/
private $page = [];
private $data = [];

/**
* @var array
*/
private $criteria;

/**
* @var
* @var string
*/
private $path;

/**
* @var Closure
*/
private $prepareFunction;

/**
* @var bool
*/
private $isLastPage = false;

/**
* ResourceIterator constructor.
*
* @param string $path
* @param Client $client
* @param array $criteria
* @param string $path
* @param Client $client
* @param array $criteria
* @param null|Closure $prepareFunction
*/
public function __construct($path, Client $client, array $criteria = [])
public function __construct($path, Client $client, array $criteria = [], Closure $prepareFunction = null)
{
Assertion::string($path);

$this->client = $client;
$this->criteria = $criteria;
$this->path = $path;
$this->prepareFunction = $prepareFunction;
}

/**
* {@inheritdoc}
*/
public function current()
{
return $this->page[$this->index];
$element = $this->data[$this->index];

unset($this->data[$this->index]);

return $element;
}

/**
Expand All @@ -77,7 +95,7 @@ public function next()
{
++$this->index;

if (!$this->valid()) {
if (!$this->isLastPage && !$this->valid()) {
$this->offset += $this->limit;

$this->loadPage($this->criteria, $this->limit, $this->offset);
Expand All @@ -89,7 +107,7 @@ public function next()
*/
public function valid()
{
return array_key_exists($this->index, $this->page);
return array_key_exists($this->index, $this->data);
}

/**
Expand All @@ -105,12 +123,20 @@ public function key()
*/
public function rewind()
{
$this->loadPage($this->criteria, $this->limit, 0);
$this->loadPage($this->criteria, $this->limit);

$this->offset = 0;
$this->index = 0;
}

/**
* {@inheritdoc}
*/
public function count()
{
return $this->client->getTotal($this->path, $this->criteria);
}

/**
* @param array $criteria
* @param int $limit
Expand All @@ -120,8 +146,16 @@ private function loadPage(array $criteria = [], $limit = 0, $offset = 0)
{
$result = $this->client->request('GET', $this->path, $criteria, $limit, $offset);

if (null !== $this->prepareFunction) {
$result = call_user_func($this->prepareFunction, $result);
}

if (count($result) !== $this->limit) {
$this->isLastPage = true;
}

foreach ($result as $key => $item) {
$this->page[$this->index + $key] = $item;
$this->data[$this->index + $key] = $item;
}
}
}
Loading

0 comments on commit 0380953

Please sign in to comment.