diff --git a/CHANGELOG b/CHANGELOG deleted file mode 100644 index 2c93c39..0000000 --- a/CHANGELOG +++ /dev/null @@ -1,56 +0,0 @@ -4.0.0-beta - - Simplify code - - DI ready - - Improve PSRs support - -3.1.2 - Businesses service added - - SmartCampaigns support added - - SmartBanners support added - - checkDictionaries fix - -3.1.1 - Fix scrutinizer reflection issue - -3.1.0 - Improve params mapping - - NegativeKeywordSharedSets service added - - Creatives service added - - KeywordsResearch::deduplicate method added - - Clients::update method added - -3.0.1 - AgencyClients add and update methods - - Codestyle fixes - -3.0.0 - New parameters to Ads and Campaign services - -2.5.0 - Add TurboPages service mapping - -2.4.0 - Fix bc for report service - -2.3.1 - Fix report service - - Fix report call example - -2.3.0 - Report goals support added - -2.2.3 - KeywordBids setAuto method - - Update phpdoc codestyle - -2.2.2 - Remove lockfile - -2.2.1 - KeywordBids added - - Leads added - - Codestyle fixes - - Test improvements - - Update dependencies - -2.1.0 - Reports service - - Unify with Report request and response interfaces - - Add service name and method name metadata into Response - -2.0.1 - Update examples - -2.0.0 - Transport and ServiceFactory refactoring - - Improve Client class constructor - - Add transport and credentials setters to Client class - -1.1.0 - Add changelog - - Prepare transport for AgencyClients service - - AgencyClient service diff --git a/examples/agency_clients.php b/examples/agency_clients.php index 9d5a99f..b56aaac 100644 --- a/examples/agency_clients.php +++ b/examples/agency_clients.php @@ -2,10 +2,9 @@ require '../vendor/autoload.php'; -$credentials = \Gladyshev\Yandex\Direct\AgencyCredentials::buildForSandbox( +$credentials = \Gladyshev\Yandex\Direct\Credentials::agencySandbox( getenv('_TOKEN_'), - getenv('_MASTER_TOKEN_'), - getenv('_CLIENT_LOGIN_') + getenv('_MASTER_TOKEN_') ); $client = new \Gladyshev\Yandex\Direct\Client( @@ -14,7 +13,7 @@ ); $resp = $client->agencyClients->get( - ['Archived' => 'YES'], + ['Archived' => 'NO'], ['Login'] ); diff --git a/examples/reports.php b/examples/reports.php index 8744b76..f94f18e 100644 --- a/examples/reports.php +++ b/examples/reports.php @@ -2,7 +2,7 @@ require '../vendor/autoload.php'; -$credentials = \Gladyshev\Yandex\Direct\ClientCredentials::buildForSandbox( +$credentials = \Gladyshev\Yandex\Direct\Credentials::clientSandbox( getenv('_TOKEN_'), getenv('_MASTER_TOKEN_') ); diff --git a/examples/simple.php b/examples/simple.php index 5536599..0a9d3d9 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -2,7 +2,7 @@ require '../vendor/autoload.php'; -$credentials = \Gladyshev\Yandex\Direct\ClientCredentials::buildForSandbox( +$credentials = \Gladyshev\Yandex\Direct\Credentials::clientSandbox( getenv('_TOKEN_'), getenv('_MASTER_TOKEN_') ); diff --git a/phpcs.xml b/phpcs.xml index 78d2747..c30965f 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,7 +7,7 @@ - + @@ -15,7 +15,7 @@ - src + src/ */vendor/* diff --git a/src/AbstractService.php b/src/AbstractService.php index 2a72a64..a3f8d2e 100644 --- a/src/AbstractService.php +++ b/src/AbstractService.php @@ -1,4 +1,5 @@ httpClient = $httpClient; } - public function getName(): string - { - return $this->serviceName; - } - - public function getCredentials(): \Gladyshev\Yandex\Direct\CredentialsInterface - { - return $this->credentials; - } - public function call(array $params = []): array { $request = new \GuzzleHttp\Psr7\Request( @@ -54,12 +45,22 @@ public function call(array $params = []): array return $this->handleResponse($request, $response); } + protected function getServiceName(): string + { + return $this->serviceName; + } + + protected function getCredentials(): \Gladyshev\Yandex\Direct\CredentialsInterface + { + return $this->credentials; + } + /** * @return string */ - protected function getUri() : string + protected function getUri(): string { - return $this->getCredentials()->getBaseUrl() . '/json/v5/' . mb_strtolower($this->getName()); + return $this->getCredentials()->getBaseUrl() . '/json/v5/' . mb_strtolower($this->getServiceName()); } /** @@ -91,7 +92,7 @@ protected function getHeaders(): array protected function getBody(array $params): string { if (empty($params['params'])) { - $params = new \StdClass; + $params = new \StdClass(); } else { $params['params'] = array_filter($params['params']); } diff --git a/src/AgencyCredentials.php b/src/AgencyCredentials.php deleted file mode 100644 index b222042..0000000 --- a/src/AgencyCredentials.php +++ /dev/null @@ -1,101 +0,0 @@ -token = $token; - $this->masterToken = $masterToken; - $this->login = $clientLogin; - $this->useOperatorUnits = $useOperatorUnits; - $this->language = $language; - $this->baseUrl = $baseUrl; - } - - public static function buildForSandbox( - string $token, - ?string $masterToken = null, - ?string $login = null, - ?bool $useOperatorUnits = true, - string $language = self::LANGUAGE_RU, - string $baseUrl = self::DEFAULT_SANDBOX_BASE_URL - ): self { - return new self( - $token, - $masterToken, - $login, - $useOperatorUnits, - $language, - $baseUrl - ); - } - - /** - * @return string - */ - public function getToken(): string - { - return $this->token; - } - - /** - * @return string|null - */ - public function getMasterToken(): ?string - { - return $this->masterToken; - } - - /** - * @return string|null - */ - public function getClientLogin(): ?string - { - return $this->login; - } - - /** - * @return bool|null - */ - public function getUseOperatorUnits(): ?bool - { - return $this->useOperatorUnits; - } - - /** - * @return string - */ - public function getLanguage(): string - { - return $this->language; - } - - /** - * @return string - */ - public function getBaseUrl(): string - { - return $this->baseUrl; - } - - public function isAgency(): bool - { - return true; - } -} diff --git a/src/Client.php b/src/Client.php index 6763e66..4a33b50 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,4 +1,5 @@ token = $token; - $this->masterToken = $masterToken; - $this->language = $language; - $this->baseUrl = $baseUrl; - } - - public static function build( - string $token, - ?string $masterToken = null, - string $language = self::LANGUAGE_RU, - string $baseUrl = self::DEFAULT_BASE_URL - ): self { - return new self( - $token, - $masterToken, - $language, - $baseUrl - ); - } - - public static function buildForSandbox( - string $token, - ?string $masterToken = null, - string $language = self::LANGUAGE_RU, - string $baseUrl = self::DEFAULT_SANDBOX_BASE_URL - ): self { - return new self( - $token, - $masterToken, - $language, - $baseUrl - ); - } - - /** - * @return string - */ - public function getToken(): string - { - return $this->token; - } - - /** - * @return string|null - */ - public function getMasterToken(): ?string - { - return $this->masterToken; - } - - /** - * @return string - */ - public function getLanguage(): string - { - return $this->language; - } - - /** - * @return string - */ - public function getBaseUrl(): string - { - return $this->baseUrl; - } - - public function getClientLogin(): ?string - { - return null; - } - - public function getUseOperatorUnits(): ?bool - { - return null; - } - - public function isAgency(): bool - { - return false; - } -} diff --git a/src/Credentials.php b/src/Credentials.php new file mode 100644 index 0000000..52d1e47 --- /dev/null +++ b/src/Credentials.php @@ -0,0 +1,169 @@ +token = $token; + $this->masterToken = $masterToken; + $this->login = $clientLogin; + $this->useOperatorUnits = $useOperatorUnits; + $this->language = $language; + $this->baseUrl = $baseUrl; + $this->isAgency = $isAgency; + } + + public static function agency( + string $token, + ?string $masterToken = null, + ?string $login = null, + bool $useOperatorUnits = true, + string $language = self::LANGUAGE_RU + ): self { + return new self( + $token, + $masterToken, + $login, + $useOperatorUnits, + true, + $language, + self::DEFAULT_BASE_URL + ); + } + + public static function agencySandbox( + string $token, + ?string $masterToken = null, + ?string $login = null, + bool $useOperatorUnits = true, + string $language = self::LANGUAGE_RU + ): self { + return new self( + $token, + $masterToken, + $login, + $useOperatorUnits, + true, + $language, + self::DEFAULT_SANDBOX_BASE_URL + ); + } + + public static function client( + string $token, + ?string $masterToken = null, + string $language = self::LANGUAGE_RU + ): self { + return new self( + $token, + $masterToken, + null, + null, + false, + $language, + self::DEFAULT_BASE_URL + ); + } + + public static function clientSandbox( + string $token, + ?string $masterToken = null, + string $language = self::LANGUAGE_RU + ): self { + return new self( + $token, + $masterToken, + null, + null, + false, + $language, + self::DEFAULT_SANDBOX_BASE_URL + ); + } + + /** + * @return string + */ + public function getToken(): string + { + return $this->token; + } + + /** + * @return string|null + */ + public function getMasterToken(): ?string + { + return $this->masterToken; + } + + /** + * @return string|null + */ + public function getClientLogin(): ?string + { + return $this->login; + } + + /** + * @return bool|null + */ + public function getUseOperatorUnits(): ?bool + { + return $this->useOperatorUnits; + } + + /** + * @return string + */ + public function getLanguage(): string + { + return $this->language; + } + + /** + * @return string + */ + public function getBaseUrl(): string + { + return $this->baseUrl; + } + + /** + * @return bool + */ + public function isAgency(): bool + { + return $this->isAgency; + } +} diff --git a/src/Exception/ErrorResponseException.php b/src/Exception/ErrorResponseException.php index c73b37a..8f8466d 100644 --- a/src/Exception/ErrorResponseException.php +++ b/src/Exception/ErrorResponseException.php @@ -1,4 +1,5 @@ - * @date 26/08/2016 13:51 - */ namespace Gladyshev\Yandex\Direct\Service; use ReflectionException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/AgencyClients.php b/src/Service/AgencyClients.php index 4857d6a..76f7011 100644 --- a/src/Service/AgencyClients.php +++ b/src/Service/AgencyClients.php @@ -1,4 +1,5 @@ - * @created 08.12.16 16:07 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/BidModifiers.php b/src/Service/BidModifiers.php index 53f1938..34017fe 100644 --- a/src/Service/BidModifiers.php +++ b/src/Service/BidModifiers.php @@ -4,7 +4,6 @@ use Gladyshev\Yandex\Direct\Exception\ErrorResponseException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Bids.php b/src/Service/Bids.php index 308f7ea..29645e9 100644 --- a/src/Service/Bids.php +++ b/src/Service/Bids.php @@ -1,13 +1,7 @@ - * @date 29/08/2016 12:32 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Businesses.php b/src/Service/Businesses.php index 64a7d9c..9243706 100644 --- a/src/Service/Businesses.php +++ b/src/Service/Businesses.php @@ -1,12 +1,7 @@ - * @date 26/08/20120 21:16 - */ namespace Gladyshev\Yandex\Direct\Service; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Campaigns.php b/src/Service/Campaigns.php index 5043700..f5280dc 100644 --- a/src/Service/Campaigns.php +++ b/src/Service/Campaigns.php @@ -1,4 +1,5 @@ - * @date 29/08/2016 12:33 - */ namespace Gladyshev\Yandex\Direct\Service; use ReflectionException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Clients.php b/src/Service/Clients.php index 7c3db8c..8c37b3d 100644 --- a/src/Service/Clients.php +++ b/src/Service/Clients.php @@ -1,14 +1,7 @@ - * @created 29.11.16 14:09 - */ namespace Gladyshev\Yandex\Direct\Service; - - - /** * Class Clients * @package Gladyshev\Yandex\Direct\Service diff --git a/src/Service/Creatives.php b/src/Service/Creatives.php index 66f898e..f9ab7fb 100644 --- a/src/Service/Creatives.php +++ b/src/Service/Creatives.php @@ -1,7 +1,4 @@ - * @date 29/08/2016 12:33 - */ namespace Gladyshev\Yandex\Direct\Service; - - - /** * Class Dictionaries * @package Gladyshev\Yandex\Direct\Service diff --git a/src/Service/DynamicTextAdTargets.php b/src/Service/DynamicTextAdTargets.php index e6f9d67..488a737 100644 --- a/src/Service/DynamicTextAdTargets.php +++ b/src/Service/DynamicTextAdTargets.php @@ -1,13 +1,7 @@ - * @date 29/08/2016 12:34 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Feeds.php b/src/Service/Feeds.php index af4fe0f..e1b83f5 100644 --- a/src/Service/Feeds.php +++ b/src/Service/Feeds.php @@ -1,15 +1,10 @@ - * @date 26/08/20120 21:34 - */ namespace Gladyshev\Yandex\Direct\Service; use ReflectionException; use Gladyshev\Yandex\Direct\Exception\ErrorResponseException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/KeywordBids.php b/src/Service/KeywordBids.php index b529ce1..9972d18 100644 --- a/src/Service/KeywordBids.php +++ b/src/Service/KeywordBids.php @@ -1,13 +1,7 @@ - * @date 23/04/2018 09:50 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Keywords.php b/src/Service/Keywords.php index 603050b..fde7e91 100644 --- a/src/Service/Keywords.php +++ b/src/Service/Keywords.php @@ -1,13 +1,7 @@ - * @date 29/08/2016 12:34 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/KeywordsResearch.php b/src/Service/KeywordsResearch.php index 9b93b96..65a3983 100644 --- a/src/Service/KeywordsResearch.php +++ b/src/Service/KeywordsResearch.php @@ -1,14 +1,7 @@ - * @date 03.05.17 7:46 - */ namespace Gladyshev\Yandex\Direct\Service; - - - final class KeywordsResearch extends \Gladyshev\Yandex\Direct\AbstractService { /** diff --git a/src/Service/Leads.php b/src/Service/Leads.php index e305d81..9e65115 100644 --- a/src/Service/Leads.php +++ b/src/Service/Leads.php @@ -1,14 +1,9 @@ - * @date 23/04/2018 10:12 - */ namespace Gladyshev\Yandex\Direct\Service; use ReflectionException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/NegativeKeywordSharedSets.php b/src/Service/NegativeKeywordSharedSets.php index 8606aa6..a8e6091 100644 --- a/src/Service/NegativeKeywordSharedSets.php +++ b/src/Service/NegativeKeywordSharedSets.php @@ -1,11 +1,7 @@ current($response->getHeader('RequestId')) ]; - if ($response->getStatusCode() == 201 + if ( + $response->getStatusCode() == 201 || $response->getStatusCode() == 202 ) { $result['retryIn'] = $response->getHeaders()['retryIn']; diff --git a/src/Service/RetargetingLists.php b/src/Service/RetargetingLists.php index 27cb146..245f410 100644 --- a/src/Service/RetargetingLists.php +++ b/src/Service/RetargetingLists.php @@ -1,13 +1,7 @@ - * @created 03.12.16 15:26 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/Sitelinks.php b/src/Service/Sitelinks.php index c8247db..ead759d 100644 --- a/src/Service/Sitelinks.php +++ b/src/Service/Sitelinks.php @@ -1,13 +1,7 @@ - * @date 29/08/2016 12:34 - */ namespace Gladyshev\Yandex\Direct\Service; - - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/SmartAdTargets.php b/src/Service/SmartAdTargets.php index 9148fdb..633b30d 100644 --- a/src/Service/SmartAdTargets.php +++ b/src/Service/SmartAdTargets.php @@ -1,15 +1,10 @@ - * @date 26/08/20120 21:34 - */ namespace Gladyshev\Yandex\Direct\Service; use ReflectionException; use Gladyshev\Yandex\Direct\Exception\ErrorResponseException; - use function Gladyshev\Yandex\Direct\get_param_names; /** diff --git a/src/Service/TurboPages.php b/src/Service/TurboPages.php index 5f3d582..e286a19 100644 --- a/src/Service/TurboPages.php +++ b/src/Service/TurboPages.php @@ -1,11 +1,7 @@ client = new Client($credentials, $httpClient); + $this->client = new \Gladyshev\Yandex\Direct\Client( + new \Gladyshev\Yandex\Direct\Tests\Mocks\Credentials, + new \GuzzleHttp\Client + ); } /** @@ -33,6 +29,16 @@ public function testCreateService(string $serviceName): void $this->assertInstanceOf(ServiceInterface::class, $instance); } + /** + * @dataProvider validServicesDataProvider + */ + public function testMagicCreateService(string $serviceName): void + { + $instance = $this->client->{$serviceName}; + + $this->assertInstanceOf(ServiceInterface::class, $instance); + } + /** * @dataProvider invalidServicesDataProvider */ @@ -42,43 +48,38 @@ public function testExceptionOnInvalidServiceName(string $serviceName): void $this->client->createService($serviceName); } + /** + * @dataProvider invalidServicesDataProvider + */ + public function testExceptionOnInvalidServiceNameWithMagicCreate(string $serviceName): void + { + $this->expectException(ServiceNotFoundException::class); + $this->client->{$serviceName}; + } + public function invalidServicesDataProvider(): array { return [ - ['fuck'], - ['nop'], - ['piu'] + ['foo'], + ['bar'], + ['buzz'] ]; } public function validServicesDataProvider(): array { - return [ - ['AdExtensions'], - ['AdGroups'], - ['AdImages'], - ['Ads'], - ['AgencyClients'], - ['AudienceTargets'], - ['BidModifiers'], - ['Bids'], - ['Businesses'], - ['Campaigns'], - ['Changes'], - ['Clients'], - ['Creatives'], - ['Dictionaries'], - ['DynamicTextAdTargets'], - ['KeywordBids'], - ['Keywords'], - ['KeywordsResearch'], - ['Leads'], - ['NegativeKeywordSharedSets'], - ['Reports'], - ['RetargetingLists'], - ['Sitelinks'], - ['TurboPages'], - ['VCards'], - ]; + $dir = new \FilesystemIterator( + dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Service' + ); + + $services = []; + + foreach ($dir as $fileInfo) { + if ($fileInfo->isFile()) { + $services[] = [$fileInfo->getBasename('.php')]; + } + } + + return $services; } } diff --git a/tests/CredentialsTest.php b/tests/CredentialsTest.php new file mode 100644 index 0000000..332cd1d --- /dev/null +++ b/tests/CredentialsTest.php @@ -0,0 +1,137 @@ +credentials = new Credentials( + 'token', + 'masterToken', + 'clientLogin', + true, + true, + 'language', + 'baseUrl' + ); + } + + public function testGetToken() + { + $this->assertEquals('token', $this->credentials->getToken()); + } + + public function testGetUseOperatorUnits() + { + $this->assertEquals(true, $this->credentials->getUseOperatorUnits()); + } + + public function testGetClientLogin() + { + $this->assertEquals('clientLogin', $this->credentials->getClientLogin()); + } + + public function testGetMasterToken() + { + $this->assertEquals('masterToken', $this->credentials->getMasterToken()); + } + + public function testIsAgency() + { + $this->assertEquals(true, $this->credentials->isAgency()); + } + + public function testGetLanguage() + { + $this->assertEquals('language', $this->credentials->getLanguage()); + } + + public function testGetBaseUrl() + { + $this->assertEquals('baseUrl', $this->credentials->getBaseUrl()); + } + + public function testAgency() + { + $credentials = \Gladyshev\Yandex\Direct\Credentials::agency( + 'token', + 'masterToken', + 'login', + true, + 'language' + ); + + $this->assertInstanceOf(CredentialsInterface::class, $credentials); + $this->assertEquals(true, $credentials->isAgency()); + $this->assertEquals(CredentialsInterface::DEFAULT_BASE_URL, $credentials->getBaseUrl()); + $this->assertEquals('token', $credentials->getToken()); + $this->assertEquals('masterToken', $credentials->getMasterToken()); + $this->assertEquals('login', $credentials->getClientLogin()); + $this->assertEquals('language', $credentials->getLanguage()); + } + + public function testAgencySandbox() + { + $credentials = \Gladyshev\Yandex\Direct\Credentials::agencySandbox( + 'token', + 'masterToken', + 'login', + true, + 'language' + ); + + $this->assertInstanceOf(CredentialsInterface::class, $credentials); + $this->assertEquals(true, $credentials->isAgency()); + $this->assertEquals(CredentialsInterface::DEFAULT_SANDBOX_BASE_URL, $credentials->getBaseUrl()); + $this->assertEquals('token', $credentials->getToken()); + $this->assertEquals('masterToken', $credentials->getMasterToken()); + $this->assertEquals('login', $credentials->getClientLogin()); + $this->assertEquals('language', $credentials->getLanguage()); + } + + public function testClient() + { + $credentials = \Gladyshev\Yandex\Direct\Credentials::client( + 'token', + 'masterToken', + 'language' + ); + + $this->assertInstanceOf(CredentialsInterface::class, $credentials); + $this->assertEquals(false, $credentials->isAgency()); + $this->assertEquals(CredentialsInterface::DEFAULT_BASE_URL, $credentials->getBaseUrl()); + $this->assertEquals('token', $credentials->getToken()); + $this->assertEquals('masterToken', $credentials->getMasterToken()); + $this->assertEquals(null, $credentials->getClientLogin()); + $this->assertEquals(null, $credentials->getUseOperatorUnits()); + $this->assertEquals('language', $credentials->getLanguage()); + } + + public function testClientSandbox() + { + $credentials = Credentials::clientSandbox( + 'token', + 'masterToken', + 'language' + ); + + $this->assertInstanceOf(CredentialsInterface::class, $credentials); + $this->assertEquals(false, $credentials->isAgency()); + $this->assertEquals(CredentialsInterface::DEFAULT_SANDBOX_BASE_URL, $credentials->getBaseUrl()); + $this->assertEquals('token', $credentials->getToken()); + $this->assertEquals('masterToken', $credentials->getMasterToken()); + $this->assertEquals(null, $credentials->getClientLogin()); + $this->assertEquals(null, $credentials->getUseOperatorUnits()); + $this->assertEquals('language', $credentials->getLanguage()); + } +}