Skip to content

Commit 6bb9024

Browse files
authored
Merge pull request #70 from silverDuy/remove-last-slashes-for-endpoint
Added EndPointTrait for supporting to remove the unnecessary last slashes of an endpoint
2 parents ba845a1 + 2c26313 commit 6bb9024

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
### 2.0.0
88
- Updated the entity-schema for Shopware version 6.5
9+
- Added EndPointTrait for supporting to remove the unnecessary last slashes of an endpoint
10+
- Updated `AdminAuthenticator` and `Context` to remove the unnecessary last slashes of the provided endpoint
11+
- [Fix Call to a member function getSource() on null](https://github.com/vienthuong/shopware-php-sdk/issues/65)
912

1013
### 1.7.3
1114
- [Fix Schema caching](https://github.com/vienthuong/shopware-php-sdk/pull/62)

src/Client/AdminAuthenticator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use Vin\ShopwareSdk\Client\GrantType\PasswordGrantType;
1111
use Vin\ShopwareSdk\Client\GrantType\RefreshTokenGrantType;
1212
use Vin\ShopwareSdk\Data\AccessToken;
13+
use Vin\ShopwareSdk\Data\EndPointTrait;
1314
use Vin\ShopwareSdk\Exception\AuthorizationFailedException;
1415

1516
class AdminAuthenticator
1617
{
1718
use CreateClientTrait;
19+
use EndPointTrait;
1820

1921
public const OAUTH_TOKEN_ENDPOINT = '/api/oauth/token';
2022

@@ -43,7 +45,7 @@ public function __construct(GrantType $grantType, string $endpoint, array $confi
4345

4446
$this->httpClient = $this->httpClient ?? $this->createHttpClient($this->config);
4547
$this->grantType = $grantType;
46-
$this->endpoint = $endpoint;
48+
$this->endpoint = $this->removeLastSlashes($endpoint);
4749
}
4850

4951
public function fetchAccessToken(): AccessToken

src/Data/Context.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Context
66
{
7+
use EndPointTrait;
8+
79
public string $languageId = Defaults::LANGUAGE_SYSTEM;
810

911
public string $currencyId = Defaults::CURRENCY;
@@ -36,7 +38,7 @@ public function __construct(
3638
$this->compatibility = $compatibility;
3739
$this->inheritance = $inheritance;
3840
$this->accessToken = $accessToken;
39-
$this->apiEndpoint = $apiEndpoint;
41+
$this->apiEndpoint = $this->removeLastSlashes($apiEndpoint);
4042
$this->additionalHeaders = $additionalHeaders;
4143
}
4244
}

src/Data/EndPointTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Vin\ShopwareSdk\Data;
4+
5+
trait EndPointTrait
6+
{
7+
private function removeLastSlashes(string $endpoint): string
8+
{
9+
while (substr($endpoint, -1) === '/') {
10+
$endpoint = rtrim($endpoint, '/');
11+
}
12+
13+
return $endpoint;
14+
}
15+
}

tests/AdminAuthenticatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,17 @@ public function testTokenExpired(): void
102102

103103
static::assertFalse($accessToken->isExpired());
104104
}
105+
106+
public function testRemoveLastSlashesFromEndPoint(): void
107+
{
108+
$grantType = GrantType::createFromConfig([
109+
'grant_type' => GrantType::PASSWORD,
110+
'username' => 'admin',
111+
'password' => 'shopware'
112+
]);
113+
114+
$this->authenticator = new AdminAuthenticator($grantType, 'http://test.com///////');
115+
116+
static::assertEquals('http://test.com', $this->authenticator->getEndpoint());
117+
}
105118
}

0 commit comments

Comments
 (0)