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

Commit d1813ec

Browse files
author
Jens Schulze
committed
Merge branch 'release/v1.0.0-RC12'
2 parents 4b89b99 + 543fcc5 commit d1813ec

File tree

110 files changed

+1608
-553
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1608
-553
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ before_script:
2222
- composer config -g github-oauth.github.com $GITHUB_OAUTH_TOKEN
2323
- sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ] && [ `php-config --vernum` -lt 70000 ] ; then echo 'extension = redis.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi"
2424
- sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ] && [ `php-config --vernum` -ge 50400 ] && [ `php-config --vernum` -lt 70000 ]; then printf "yes\n" | pecl install -f apcu-4.0.10 ; fi"
25-
- sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ] && [ `php-config --vernum` -ge 70000 ] ; then printf "yes\n" | pecl install -f apcu ; fi"
25+
- sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ] && [ `php-config --vernum` -ge 70000 ] ; then printf "yes\n" | pecl install -f apcu-5.1.3 ; fi"
2626
- sh -c "if [ $TRAVIS_PHP_VERSION != 'hhvm' ] ; then phpenv config-add ./tests/apc.ini; fi"
2727
- if [ $TRAVIS_PHP_VERSION == '5.4' ] ; then ./set_guzzle5.sh; fi
2828
- composer global require hirak/prestissimo

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
<a name="1.0.0-RC12"></a>
2+
# [1.0.0-RC12](https://github.com/sphereio/commercetools-php-sdk/compare/v1.0.0-RC11...v1.0.0-RC12) (2016-05-02)
3+
4+
5+
### Bug Fixes
6+
7+
* **Cart:** fix type of custom line-item slug ([34367d5](https://github.com/sphereio/commercetools-php-sdk/commit/34367d5))
8+
* **CustomLineItem:** fix type of slug in custom line-item ([d8d7d2a](https://github.com/sphereio/commercetools-php-sdk/commit/d8d7d2a))
9+
10+
### Features
11+
12+
* **Cart:** add helper to calculate line item count ([291bd05](https://github.com/sphereio/commercetools-php-sdk/commit/291bd05))
13+
* **Client:** add support for oauth password and refresh token flow ([fe23c8b](https://github.com/sphereio/commercetools-php-sdk/commit/fe23c8b)), closes [#191](https://github.com/sphereio/commercetools-php-sdk/issues/191)
14+
* **Error:** add oauth error classes ([92eec57](https://github.com/sphereio/commercetools-php-sdk/commit/92eec57))
15+
* **Product:** add support to use sku to identify a product variant in update action ([7f1979b](https://github.com/sphereio/commercetools-php-sdk/commit/7f1979b)), closes [#192](https://github.com/sphereio/commercetools-php-sdk/issues/192)
16+
* **Reference:** add constructor ofKey constructor to references ([e6fafc3](https://github.com/sphereio/commercetools-php-sdk/commit/e6fafc3))
17+
18+
19+
120
<a name="1.0.0-RC11"></a>
221
# [1.0.0-RC11](https://github.com/sphereio/commercetools-php-sdk/compare/v1.0.0-RC10...v1.0.0-RC11) (2016-04-06)
322

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# <img src="build/theme/resources/CT_cube_200px.png" width="40" align="center"></img> commercetools PHP SDK
22

3-
> STATUS: Release Candidate 11. We ask you to really use this API thoroughly now, especially the API design and object structure. Thank you very much!
4-
>
5-
> See the [Milestone Plan](https://github.com/sphereio/commercetools-php-sdk/milestones?direction=desc&sort=completeness&state=open) for details of what's planned in detail. We love feedback and [Issue reports](https://github.com/sphereio/commercetools-php-sdk/issues?q=is%3Aopen+is%3Aissue+sort%3Acreated-asc)!
6-
> Up-to-Date planning status can be found on this [Waffle Board](https://waffle.io/sphereio/commercetools-php-sdk)
7-
83
[![Build Status](https://img.shields.io/travis/sphereio/commercetools-php-sdk/master.svg?style=flat-square)](https://travis-ci.org/sphereio/commercetools-php-sdk) [![Scrutinizer](https://img.shields.io/scrutinizer/g/sphereio/commercetools-php-sdk.svg?style=flat-square)](https://scrutinizer-ci.com/g/sphereio/commercetools-php-sdk/) [![Scrutinizer](https://img.shields.io/scrutinizer/coverage/g/sphereio/commercetools-php-sdk.svg?style=flat-square)](https://scrutinizer-ci.com/g/sphereio/commercetools-php-sdk/) [![Packagist](https://img.shields.io/packagist/v/commercetools/php-sdk.svg?style=flat-square)](https://packagist.org/packages/commercetools/php-sdk) [![Packagist](https://img.shields.io/packagist/dm/commercetools/php-sdk.svg?style=flat-square)](https://packagist.org/packages/commercetools/php-sdk)
94

105
The PHP SDK allows developers to build applications on the commercetools platform (technically speaking against the SPHERE.IO REST API) using PHP native interfaces, models and helpers instead of manually using the HTTP and JSON API.
@@ -184,16 +179,25 @@ To enable code style checks directly in phpStorm you have to configure the path
184179
Now you can enable at Preferences > Editor > Inspections > PHP the "PHP code sniffer validation" with PSR-2 standard. Change the severity if needed.
185180

186181

187-
### Running tests using docker
182+
### Running integration tests
183+
184+
For running the integration tests you need an empty commercetools project and have to create an API client using the commercetools Admin Center with the scopes manage_project, view_orders and view_products.
185+
186+
#### Local environment
187+
188+
```sh
189+
composer update
190+
vendor/bin/phpunit
191+
```
192+
193+
#### Using docker
188194

189195
Running the test image:
190196

191197
```sh
192198
echo "COMMERCETOOLS_CLIENT_ID=YourClientID" > env.list
193199
echo "COMMERCETOOLS_CLIENT_SECRET=YourClientSecret" >> env.list
194200
echo "COMMERCETOOLS_PROJECT=YourProjectKey" >> env.list
195-
echo "COMMERCETOOLS_OAUTH_URL=https://auth.sphere.io/oauth/token" >> env.list
196-
echo "COMMERCETOOLS_API_URL=https://api.sphere.io" >> env.list
197201

198202
docker run --env-file env.list -v $PWD:/opt/app -w /opt/app --rm=true jaysde/php-test-base tools/docker-phpunit.sh
199203
```

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@
6363
"env-map": {
6464
"client_id": "COMMERCETOOLS_CLIENT_ID",
6565
"client_secret": "COMMERCETOOLS_CLIENT_SECRET",
66-
"view_products-client_id": "COMMERCETOOLS_CLIENT_ID_VIEW",
67-
"view_products-client_secret": "COMMERCETOOLS_CLIENT_SECRET_VIEW",
66+
"project": "COMMERCETOOLS_PROJECT"
67+
}
68+
},
69+
{
70+
"file": "tests/myapp.yml",
71+
"env-map": {
72+
"client_id": "COMMERCETOOLS_CLIENT_ID",
73+
"client_secret": "COMMERCETOOLS_CLIENT_SECRET",
6874
"project": "COMMERCETOOLS_PROJECT",
6975
"oauth_url": "COMMERCETOOLS_OAUTH_URL",
7076
"api_url": "COMMERCETOOLS_API_URL"

docroot/myapp.yml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
parameters:
22
client_id: Your Commercetools client ID
33
client_secret: Your Commercetools client secret
4-
view_products-client_id: Your Commercetools client ID
5-
view_products-client_secret: Your Commercetools client secret
64
project: Your Commercetools project key
7-
oauth_url: https://auth.sphere.io/oauth/token
8-
api_url: https://api.sphere.io

src/AbstractHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
abstract class AbstractHttpClient
1616
{
17-
const VERSION = '1.0.0-RC11';
17+
const VERSION = '1.0.0-RC12';
1818

1919
/**
2020
* @var AdapterInterface

src/Client/OAuth/Manager.php

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
namespace Commercetools\Core\Client\OAuth;
88

9+
use Commercetools\Core\Config;
910
use Commercetools\Core\Error\ApiException;
10-
use GuzzleHttp\Psr7\Request;
1111
use Psr\Http\Message\ResponseInterface;
1212
use Commercetools\Core\AbstractHttpClient;
1313
use Commercetools\Core\Cache\CacheAdapterFactory;
1414
use Commercetools\Core\Cache\CacheAdapterInterface;
15-
use Commercetools\Core\Client\HttpMethod;
1615
use Commercetools\Core\Error\InvalidClientCredentialsException;
17-
use Commercetools\Core\Error\Message;
1816

1917
/**
2018
* @package Commercetools\Core\OAuth
@@ -24,6 +22,7 @@ class Manager extends AbstractHttpClient
2422
{
2523
const TOKEN_CACHE_KEY = 'commercetools-io-access-token';
2624

25+
const REFRESH_TOKEN = 'refresh_token';
2726
const ACCESS_TOKEN = 'access_token';
2827
const EXPIRES_IN = 'expires_in';
2928
const ERROR = 'error';
@@ -87,70 +86,90 @@ public function getCacheAdapter()
8786
* @return Token
8887
* @throws InvalidClientCredentialsException
8988
*/
90-
public function getToken($scope = null)
89+
public function getToken()
9190
{
92-
if (is_null($scope)) {
93-
$scope = $this->getConfig()->getScope();
94-
}
95-
if ($token = $this->getCacheToken($scope)) {
91+
$scope = $this->getConfig()->getScope();
92+
if ($token = $this->getCacheToken()) {
9693
return new Token($token, null, $scope);
9794
}
9895

99-
return $this->refreshToken($scope);
96+
return $this->refreshToken();
10097
}
10198

10299
/**
103-
* @param string $scope
104100
* @return Token
105101
* @throws InvalidClientCredentialsException
106102
*/
107-
public function refreshToken($scope = null)
103+
public function refreshToken()
108104
{
109-
if (is_null($scope)) {
110-
$scope = $this->getConfig()->getScope();
105+
$scope = $this->getConfig()->getScope();
106+
$grantType = $this->getConfig()->getGrantType();
107+
$data = [Config::SCOPE => $scope, Config::GRANT_TYPE => $grantType];
108+
109+
if ($grantType === Config::GRANT_TYPE_PASSWORD) {
110+
$user = $this->getConfig()->getUsername();
111+
$password = $this->getConfig()->getPassword();
112+
$data[Config::USER_NAME] = $user;
113+
$data[Config::PASSWORD] = $password;
114+
} elseif ($grantType === Config::GRANT_TYPE_REFRESH) {
115+
$refreshToken = $this->getConfig()->getRefreshToken();
116+
$data[Config::REFRESH_TOKEN] = $refreshToken;
111117
}
112-
$token = $this->getBearerToken($scope);
118+
119+
$token = $this->getBearerToken($data);
120+
121+
if ($grantType === Config::GRANT_TYPE_PASSWORD) {
122+
$this->getConfig()->setGrantType(Config::GRANT_TYPE_REFRESH);
123+
$this->getConfig()->setRefreshToken($token->getRefreshToken());
124+
}
125+
113126
// ensure token to be invalidated in cache before TTL
114127
$ttl = max(1, floor($token->getTtl()/2));
115-
$this->getCacheAdapter()->store($this->getCacheKey($scope), $token->getToken(), $ttl);
128+
$this->getCacheAdapter()->store($this->getCacheKey(), $token->getToken(), $ttl);
116129

117130
return $token;
118131
}
119132

120-
protected function getCacheToken($scope)
133+
protected function getCacheToken()
121134
{
122-
return $this->getCacheAdapter()->fetch($this->getCacheKey($scope));
135+
return $this->getCacheAdapter()->fetch($this->getCacheKey());
123136
}
124137

125138
/**
126-
* @param $scope
127139
* @return string
128140
*/
129-
protected function getCacheKey($scope)
141+
protected function getCacheKey()
130142
{
131-
if (!isset($this->cacheKeys[$scope])) {
132-
$this->cacheKeys[$scope] = static::TOKEN_CACHE_KEY . '-' .
133-
sha1($scope);
143+
$scope = $this->getConfig()->getScope();
144+
$grantType = $this->getConfig()->getGrantType();
145+
$cacheScope = $scope . '-' . $grantType;
146+
147+
if ($grantType === Config::GRANT_TYPE_PASSWORD) {
148+
$user = $this->getConfig()->getUsername();
149+
$cacheScope .= '-' . $user;
150+
} elseif ($grantType === Config::GRANT_TYPE_REFRESH) {
151+
$token = $this->getConfig()->getRefreshToken();
152+
$cacheScope .= '-' . $token;
134153
}
135154

136-
return $this->cacheKeys[$scope];
155+
if (!isset($this->cacheKeys[$cacheScope])) {
156+
$this->cacheKeys[$cacheScope] = static::TOKEN_CACHE_KEY . '-' .
157+
sha1($cacheScope);
158+
}
159+
160+
return $this->cacheKeys[$cacheScope];
137161
}
138162

139163
/**
140-
* @param string $scope
164+
* @param array $data
141165
* @return Token
142166
* @throws ApiException
143167
* @throws \Commercetools\Core\Error\BadGatewayException
144168
* @throws \Commercetools\Core\Error\GatewayTimeoutException
145169
* @throws \Commercetools\Core\Error\ServiceUnavailableException
146170
*/
147-
protected function getBearerToken($scope)
171+
protected function getBearerToken(array $data)
148172
{
149-
$data = [
150-
'grant_type' => 'client_credentials',
151-
'scope' => $scope
152-
];
153-
154173
try {
155174
$response = $this->execute($data);
156175
} catch (ApiException $exception) {
@@ -161,6 +180,9 @@ protected function getBearerToken($scope)
161180

162181
$token = new Token($result[static::ACCESS_TOKEN], $result[static::EXPIRES_IN], $result[static::SCOPE]);
163182
$token->setValidTo(new \DateTime('now +' . $result[static::EXPIRES_IN] . ' seconds'));
183+
if (isset($result[static::REFRESH_TOKEN])) {
184+
$token->setRefreshToken($result[static::REFRESH_TOKEN]);
185+
}
164186

165187
return $token;
166188
}

src/Client/OAuth/Token.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Token
3131
*/
3232
protected $scope;
3333

34+
/**
35+
* @var string
36+
*/
37+
protected $refreshToken;
38+
3439
public function __construct($token = null, $ttl = null, $scope = null)
3540
{
3641
$this->setToken($token)->setTtl($ttl)->setScope($scope);
@@ -110,6 +115,24 @@ public function setScope($scope)
110115
$this->scope = $scope;
111116

112117
return $this;
118+
}
113119

120+
/**
121+
* @return string
122+
*/
123+
public function getRefreshToken()
124+
{
125+
return $this->refreshToken;
126+
}
127+
128+
/**
129+
* @param string $refreshToken
130+
* @return $this
131+
*/
132+
public function setRefreshToken($refreshToken)
133+
{
134+
$this->refreshToken = $refreshToken;
135+
136+
return $this;
114137
}
115138
}

0 commit comments

Comments
 (0)