From f928d53fba0ef5b667e076c9d06943df44d10aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Wed, 14 Oct 2020 12:24:56 +0300 Subject: [PATCH] ability to provide custom state instead of generated one --- src/Builder/AuthorizationUriBuilder.php | 36 ++++------- src/Component/AuthorizationUri.php | 60 ------------------- src/Interfaces/TopClientInterface.php | 8 +-- src/TopClient/TopClient.php | 10 +--- .../Builder/AuthorizationUriBuilderTest.php | 8 +-- 5 files changed, 19 insertions(+), 103 deletions(-) delete mode 100644 src/Component/AuthorizationUri.php diff --git a/src/Builder/AuthorizationUriBuilder.php b/src/Builder/AuthorizationUriBuilder.php index 6696ee3..5fff728 100644 --- a/src/Builder/AuthorizationUriBuilder.php +++ b/src/Builder/AuthorizationUriBuilder.php @@ -41,58 +41,42 @@ class AuthorizationUriBuilder implements BuilderInterface private $redirectUri; /** - * @var bool $withState + * @var string $state */ - private $withState; + private $state; /** * AuthorizationUriBuilder constructor. * * @param string $appKey * @param string $redirectUri - * @param bool $withState Set to true if state should be present in the URI - * - * It doesn't violate SRP because this class doesn't do anything besides URI generation. - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * @param string $state */ - public function __construct(string $appKey, string $redirectUri, bool $withState = false) + public function __construct(string $appKey, string $redirectUri, string $state = '') { $this->appKey = $appKey; $this->redirectUri = $redirectUri; - $this->withState = $withState; + $this->state = $state; } /** * @inheritDoc */ - public function build(): AuthorizationUri - { - $state = $this->withState ? uniqid('aeauth', true) : null; - - return new AuthorizationUri( - self::AUTHORIZE_URI . '?' . http_build_query($this->getParams($state)), - $state - ); - } - - /** - * @param string|null $state - * - * @return array - */ - private function getParams(?string $state): array + public function build(): string { if (empty($this->redirectUri)) { throw new BadMethodCallException('Redirect URI should not be empty'); } - return array_filter([ + $address = array_filter([ 'client_id' => $this->appKey, 'response_type' => 'code', 'redirect_uri' => $this->redirectUri, 'sp' => 'ae', - 'state' => $state, + 'state' => $this->state, 'view' => 'web' ]); + + return self::AUTHORIZE_URI . '?' . http_build_query($address); } } diff --git a/src/Component/AuthorizationUri.php b/src/Component/AuthorizationUri.php deleted file mode 100644 index e9d5030..0000000 --- a/src/Component/AuthorizationUri.php +++ /dev/null @@ -1,60 +0,0 @@ - - * @license http://retailcrm.ru Proprietary - * @link http://retailcrm.ru - * @see http://help.retailcrm.ru - */ - -namespace RetailCrm\Component; - -/** - * Class AuthorizationUri - * - * @category AuthorizationUri - * @package RetailCrm\Component - * @author RetailDriver LLC - * @license https://retailcrm.ru Proprietary - * @link http://retailcrm.ru - * @see https://help.retailcrm.ru - */ -class AuthorizationUri -{ - /** @var string $address */ - private $address; - - /** @var string $state */ - private $state; - - /** - * AuthorizationUri constructor. - * - * @param string $address - * @param string|null $state - */ - public function __construct(string $address, ?string $state) - { - $this->address = $address; - $this->state = $state; - } - - /** - * @return string - */ - public function getAddress(): string - { - return $this->address; - } - - /** - * @return string - */ - public function getState(): string - { - return $this->state; - } -} diff --git a/src/Interfaces/TopClientInterface.php b/src/Interfaces/TopClientInterface.php index c166309..b6f4d5c 100644 --- a/src/Interfaces/TopClientInterface.php +++ b/src/Interfaces/TopClientInterface.php @@ -42,15 +42,11 @@ interface TopClientInterface public function getServiceLocator(): ServiceLocator; /** - * @param bool $withState + * @param string $state * * @return BuilderInterface - * - * $withState is passed to AuthorizationUriBuilder. - * @see AuthorizationUriBuilder::__construct - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ - public function getAuthorizationUriBuilder(bool $withState = false): BuilderInterface; + public function getAuthorizationUriBuilder(string $state = ''): BuilderInterface; /** * Send TOP request diff --git a/src/TopClient/TopClient.php b/src/TopClient/TopClient.php index 6f05a8d..34bcb93 100644 --- a/src/TopClient/TopClient.php +++ b/src/TopClient/TopClient.php @@ -202,17 +202,13 @@ class TopClient implements TopClientInterface } /** - * @param bool $withState + * @param string $state * * @return BuilderInterface - * - * $withState is passed to AuthorizationUriBuilder. - * @see AuthorizationUriBuilder::__construct - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ - public function getAuthorizationUriBuilder(bool $withState = false): BuilderInterface + public function getAuthorizationUriBuilder(string $state = ''): BuilderInterface { - return new AuthorizationUriBuilder($this->appData->getAppKey(), $this->appData->getAppSecret(), $withState); + return new AuthorizationUriBuilder($this->appData->getAppKey(), $this->appData->getAppSecret(), $state); } /** diff --git a/tests/RetailCrm/Tests/Builder/AuthorizationUriBuilderTest.php b/tests/RetailCrm/Tests/Builder/AuthorizationUriBuilderTest.php index 2720eb7..edd3b8e 100644 --- a/tests/RetailCrm/Tests/Builder/AuthorizationUriBuilderTest.php +++ b/tests/RetailCrm/Tests/Builder/AuthorizationUriBuilderTest.php @@ -30,11 +30,11 @@ class AuthorizationUriBuilderTest extends TestCase public function testBuild() { $appData = $this->getEnvAppData(); - $builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), true); + $builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), 'state'); $result = $builder->build(); - self::assertNotFalse(strpos($result->getAddress(), $appData->getAppKey())); - self::assertNotFalse(strpos($result->getAddress(), urlencode($appData->getRedirectUri()))); - self::assertNotEmpty($result->getState()); + self::assertNotFalse(strpos($result, $appData->getAppKey())); + self::assertNotFalse(strpos($result, urlencode($appData->getRedirectUri()))); + self::assertNotFalse(strpos($result, urlencode('state'))); } }