From 88d8c37f3c624c436ba3fd9b480a2af1e1f1c926 Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 7 Jul 2020 21:35:05 +0100 Subject: [PATCH 1/2] sdk-improvements began working on improvements --- composer.json | 8 +- src/App/App.php | 19 + src/App/AppService.php | 20 + src/Booking/Booking.php | 29 ++ src/Booking/BookingService.php | 20 + src/Client.php | 150 ------- src/Cohort/Cohort.php | 20 + src/Cohort/CohortService.php | 20 + src/Competition/Competition.php | 26 ++ src/Competition/CompetitionClient.php | 14 - src/Competition/CompetitionService.php | 19 + src/Currency/Currency.php | 23 ++ src/Currency/CurrencyService.php | 20 + src/Device/Device.php | 34 ++ src/Device/DeviceService.php | 20 + src/DevicePreference/DevicePreference.php | 25 ++ .../DevicePreferenceService.php | 20 + src/DeviceToken/DeviceToken.php | 24 ++ src/DeviceToken/DeviceTokenService.php | 20 + src/Event/Event.php | 44 +++ src/Event/EventClient.php | 14 - src/Event/EventService.php | 26 ++ src/EventActivity/EventActivity.php | 27 ++ src/EventActivity/EventActivityService.php | 20 + src/EventCategory/EventCategory.php | 32 ++ src/EventCategory/EventCategoryClient.php | 14 - src/EventCategory/EventCategoryService.php | 19 + src/EventDate/EventDate.php | 23 ++ src/EventDate/EventDateService.php | 20 + src/EventIntegration/EventIntegration.php | 29 ++ .../EventIntegrationService.php | 20 + src/EventStage/EventStage.php | 30 ++ src/EventStage/EventStageService.php | 18 + src/EventStages/EventStagesClient.php | 14 - src/EventTranslation/EventTranslation.php | 25 ++ .../EventTranslationService.php | 20 + src/EventUserAction/EventUserAction.php | 26 ++ .../EventUserActionService.php | 20 + .../ConfigParameterRequiredException.php | 17 + src/Exception/InvalidConfigValueException.php | 17 + src/Exception/ServiceNotFoundException.php | 9 + .../UnsupportedConfigParameterException.php | 17 + src/Fixture/Fixture.php | 39 ++ src/Fixture/FixtureClient.php | 14 - src/Fixture/FixtureService.php | 19 + src/Form/Form.php | 28 ++ src/Form/FormService.php | 20 + src/FormField/FormField.php | 24 ++ src/FormField/FormFieldService.php | 20 + src/FulfilmentPoint/FulfilmentPoint.php | 36 ++ .../FulfilmentPointService.php | 20 + .../FulfilmentPointCategory.php | 29 ++ .../FulfilmentPointCategoryService.php | 20 + src/Integration/Integration.php | 25 ++ src/Integration/IntegrationService.php | 20 + src/Item.php | 14 + src/LeagueTable/LeagueTable.php | 39 ++ src/LeagueTable/LeagueTableService.php | 20 + src/LeagueTableGroup/LeagueTableGroup.php | 23 ++ .../LeagueTableGroupService.php | 20 + src/LoyaltyGroup/LoyaltyGroup.php | 16 + .../LoyaltyGroupsService.php} | 13 +- src/MagicFields/MagicField.php | 25 ++ ...ieldsClient.php => MagicFieldsService.php} | 9 +- src/MerchantCustomer/MerchantCustomer.php | 22 ++ .../MerchantCustomerService.php | 20 + src/News/News.php | 32 ++ src/News/NewsClient.php | 14 - src/News/NewsService.php | 19 + src/Order/Order.php | 31 ++ src/Order/OrderService.php | 20 + src/Product/Product.php | 34 ++ src/Product/ProductService.php | 20 + src/ProductCategory/ProductCategory.php | 28 ++ .../ProductCategoryService.php | 20 + .../ProductModifierItem.php | 20 + .../ProductModifierItemService.php | 21 + .../ProductModifierList.php | 28 ++ .../ProductModifierListService.php | 20 + .../ProductModifierListTranslation.php | 21 + .../ProductModifierListTranslationService.php | 20 + src/ProductVariant/ProductVariant.php | 26 ++ src/ProductVariant/ProductVariantService.php | 20 + .../ProductVariantStock.php | 25 ++ .../ProductVariantStockService.php | 20 + src/PushApp/PushApp.php | 23 ++ src/PushApp/PushAppService.php | 20 + src/PushBroadcast/PushBroadcast.php | 25 ++ src/PushBroadcast/PushBroadcastService.php | 20 + src/PushConsent/PushConsent.php | 27 ++ src/PushConsent/PushConsentService.php | 20 + src/RealifeResult.php | 40 ++ src/RealifeServiceResolver.php | 165 ++++++++ src/Season/Season.php | 24 ++ src/Season/SeasonClient.php | 14 - src/Season/SeasonService.php | 19 + src/ServiceClient.php | 365 ++++++++++++++++++ src/ServiceMap.php | 196 ++++++++++ src/SocialMedia/SocialMedia.php | 26 ++ src/SocialMedia/SocialMediaService.php | 21 + .../SocialMediaTranslation.php | 23 ++ .../SocialMediaTranslationService.php | 20 + src/SportVenue/SportVenue.php | 22 ++ src/SportVenue/SportVenueClient.php | 14 - src/SportVenue/SportVenueService.php | 19 + src/Team/Team.php | 25 ++ src/Team/TeamClient.php | 14 - src/Team/TeamService.php | 19 + src/Ticket/Ticket.php | 51 +++ src/Ticket/TicketService.php | 20 + src/TicketAuth/TicketAuth.php | 26 ++ src/TicketAuth/TicketAuthService.php | 20 + src/TicketIntegration/TicketIntegration.php | 31 ++ .../TicketIntegrationService.php | 20 + src/UsefulInfo/UsefulInfo.php | 26 ++ src/UsefulInfo/UsefulInfoService.php | 20 + .../UsefulInfoTranslation.php | 26 ++ .../UsefulInfoTranslationService.php | 20 + src/User/User.php | 34 ++ src/User/UserSso.php | 26 ++ src/User/UserSsosService.php | 19 + .../UsersClient.php => User/UsersService.php} | 11 +- src/UserEmail/UserEmail.php | 18 + src/UserEmail/UserEmailService.php | 21 + src/UserInfo/UserInfo.php | 27 ++ src/UserInfo/UserInfoService.php | 20 + src/Users/UserSsosClient.php | 14 - src/Venue/Venue.php | 37 ++ src/Venue/VenueClient.php | 14 - src/Venue/VenueService.php | 19 + src/Workflow/Workflow.php | 30 ++ src/Workflow/WorkflowService.php | 21 + src/WorkflowAction/WorkflowAction.php | 28 ++ src/WorkflowAction/WorkflowActionService.php | 21 + src/WorkflowTrigger/WorkflowTrigger.php | 30 ++ .../WorkflowTriggerService.php | 21 + 136 files changed, 3523 insertions(+), 315 deletions(-) create mode 100644 src/App/App.php create mode 100644 src/App/AppService.php create mode 100644 src/Booking/Booking.php create mode 100644 src/Booking/BookingService.php delete mode 100644 src/Client.php create mode 100644 src/Cohort/Cohort.php create mode 100644 src/Cohort/CohortService.php create mode 100644 src/Competition/Competition.php delete mode 100644 src/Competition/CompetitionClient.php create mode 100644 src/Competition/CompetitionService.php create mode 100644 src/Currency/Currency.php create mode 100644 src/Currency/CurrencyService.php create mode 100644 src/Device/Device.php create mode 100644 src/Device/DeviceService.php create mode 100644 src/DevicePreference/DevicePreference.php create mode 100644 src/DevicePreference/DevicePreferenceService.php create mode 100644 src/DeviceToken/DeviceToken.php create mode 100644 src/DeviceToken/DeviceTokenService.php create mode 100644 src/Event/Event.php delete mode 100644 src/Event/EventClient.php create mode 100644 src/Event/EventService.php create mode 100644 src/EventActivity/EventActivity.php create mode 100644 src/EventActivity/EventActivityService.php create mode 100644 src/EventCategory/EventCategory.php delete mode 100644 src/EventCategory/EventCategoryClient.php create mode 100644 src/EventCategory/EventCategoryService.php create mode 100644 src/EventDate/EventDate.php create mode 100644 src/EventDate/EventDateService.php create mode 100644 src/EventIntegration/EventIntegration.php create mode 100644 src/EventIntegration/EventIntegrationService.php create mode 100644 src/EventStage/EventStage.php create mode 100644 src/EventStage/EventStageService.php delete mode 100644 src/EventStages/EventStagesClient.php create mode 100644 src/EventTranslation/EventTranslation.php create mode 100644 src/EventTranslation/EventTranslationService.php create mode 100644 src/EventUserAction/EventUserAction.php create mode 100644 src/EventUserAction/EventUserActionService.php create mode 100644 src/Exception/ConfigParameterRequiredException.php create mode 100644 src/Exception/InvalidConfigValueException.php create mode 100644 src/Exception/ServiceNotFoundException.php create mode 100644 src/Exception/UnsupportedConfigParameterException.php create mode 100644 src/Fixture/Fixture.php delete mode 100644 src/Fixture/FixtureClient.php create mode 100644 src/Fixture/FixtureService.php create mode 100644 src/Form/Form.php create mode 100644 src/Form/FormService.php create mode 100644 src/FormField/FormField.php create mode 100644 src/FormField/FormFieldService.php create mode 100644 src/FulfilmentPoint/FulfilmentPoint.php create mode 100644 src/FulfilmentPoint/FulfilmentPointService.php create mode 100644 src/FulfilmentPointCategory/FulfilmentPointCategory.php create mode 100644 src/FulfilmentPointCategory/FulfilmentPointCategoryService.php create mode 100644 src/Integration/Integration.php create mode 100644 src/Integration/IntegrationService.php create mode 100644 src/Item.php create mode 100644 src/LeagueTable/LeagueTable.php create mode 100644 src/LeagueTable/LeagueTableService.php create mode 100644 src/LeagueTableGroup/LeagueTableGroup.php create mode 100644 src/LeagueTableGroup/LeagueTableGroupService.php create mode 100644 src/LoyaltyGroup/LoyaltyGroup.php rename src/{LoyaltyGroups/LoyaltyGroupsClient.php => LoyaltyGroup/LoyaltyGroupsService.php} (78%) create mode 100644 src/MagicFields/MagicField.php rename src/MagicFields/{MagicFieldsClient.php => MagicFieldsService.php} (86%) create mode 100644 src/MerchantCustomer/MerchantCustomer.php create mode 100644 src/MerchantCustomer/MerchantCustomerService.php create mode 100644 src/News/News.php delete mode 100644 src/News/NewsClient.php create mode 100644 src/News/NewsService.php create mode 100644 src/Order/Order.php create mode 100644 src/Order/OrderService.php create mode 100644 src/Product/Product.php create mode 100644 src/Product/ProductService.php create mode 100644 src/ProductCategory/ProductCategory.php create mode 100644 src/ProductCategory/ProductCategoryService.php create mode 100644 src/ProductModifierItem/ProductModifierItem.php create mode 100644 src/ProductModifierItem/ProductModifierItemService.php create mode 100644 src/ProductModifierList/ProductModifierList.php create mode 100644 src/ProductModifierList/ProductModifierListService.php create mode 100644 src/ProductModifierListTranslation/ProductModifierListTranslation.php create mode 100644 src/ProductModifierListTranslation/ProductModifierListTranslationService.php create mode 100644 src/ProductVariant/ProductVariant.php create mode 100644 src/ProductVariant/ProductVariantService.php create mode 100644 src/ProductVariantStock/ProductVariantStock.php create mode 100644 src/ProductVariantStock/ProductVariantStockService.php create mode 100644 src/PushApp/PushApp.php create mode 100644 src/PushApp/PushAppService.php create mode 100644 src/PushBroadcast/PushBroadcast.php create mode 100644 src/PushBroadcast/PushBroadcastService.php create mode 100644 src/PushConsent/PushConsent.php create mode 100644 src/PushConsent/PushConsentService.php create mode 100644 src/RealifeResult.php create mode 100644 src/RealifeServiceResolver.php create mode 100644 src/Season/Season.php delete mode 100644 src/Season/SeasonClient.php create mode 100644 src/Season/SeasonService.php create mode 100644 src/ServiceClient.php create mode 100644 src/ServiceMap.php create mode 100644 src/SocialMedia/SocialMedia.php create mode 100644 src/SocialMedia/SocialMediaService.php create mode 100644 src/SocialMediaTranslation/SocialMediaTranslation.php create mode 100644 src/SocialMediaTranslation/SocialMediaTranslationService.php create mode 100644 src/SportVenue/SportVenue.php delete mode 100644 src/SportVenue/SportVenueClient.php create mode 100644 src/SportVenue/SportVenueService.php create mode 100644 src/Team/Team.php delete mode 100644 src/Team/TeamClient.php create mode 100644 src/Team/TeamService.php create mode 100644 src/Ticket/Ticket.php create mode 100644 src/Ticket/TicketService.php create mode 100644 src/TicketAuth/TicketAuth.php create mode 100644 src/TicketAuth/TicketAuthService.php create mode 100644 src/TicketIntegration/TicketIntegration.php create mode 100644 src/TicketIntegration/TicketIntegrationService.php create mode 100644 src/UsefulInfo/UsefulInfo.php create mode 100644 src/UsefulInfo/UsefulInfoService.php create mode 100644 src/UsefulInfoTranslation/UsefulInfoTranslation.php create mode 100644 src/UsefulInfoTranslation/UsefulInfoTranslationService.php create mode 100644 src/User/User.php create mode 100644 src/User/UserSso.php create mode 100644 src/User/UserSsosService.php rename src/{Users/UsersClient.php => User/UsersService.php} (87%) create mode 100644 src/UserEmail/UserEmail.php create mode 100644 src/UserEmail/UserEmailService.php create mode 100644 src/UserInfo/UserInfo.php create mode 100644 src/UserInfo/UserInfoService.php delete mode 100644 src/Users/UserSsosClient.php create mode 100644 src/Venue/Venue.php delete mode 100644 src/Venue/VenueClient.php create mode 100644 src/Venue/VenueService.php create mode 100644 src/Workflow/Workflow.php create mode 100644 src/Workflow/WorkflowService.php create mode 100644 src/WorkflowAction/WorkflowAction.php create mode 100644 src/WorkflowAction/WorkflowActionService.php create mode 100644 src/WorkflowTrigger/WorkflowTrigger.php create mode 100644 src/WorkflowTrigger/WorkflowTriggerService.php diff --git a/composer.json b/composer.json index ab8fc18..7a1cf70 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,14 @@ } ], "require": { - "php": ">=7.0", + "php": ">=7.1", "guzzlehttp/guzzle": "^5.3.3|^6.2.1|^7.0", "guzzlehttp/psr7": "^1.4.1", - "guzzlehttp/promises": "^1.0" + "guzzlehttp/promises": "^1.0", + "ext-json": "*" + }, + "require-dev": { + "symfony/var-dumper": "^5.1" }, "autoload": { "psr-4": { diff --git a/src/App/App.php b/src/App/App.php new file mode 100644 index 0000000..8900d4b --- /dev/null +++ b/src/App/App.php @@ -0,0 +1,19 @@ +domain = $this->addScheme($domain, $scheme); - $this->credentials = $credentials; - $this->httpClient = new \GuzzleHttp\Client([ - 'base_uri' => rtrim($this->domain, '/'), - 'timeout' => $timeout - ]); - } - - function addScheme($domain, $scheme) - { - return parse_url($domain, PHP_URL_SCHEME) === null ? - $scheme . $domain : $domain; - } - - /** - * @param $data - * @return array - * @throws EntityCreationException - */ - public function create($data) - { - try { - $response = $this->httpClient->post($this->getPath(), [ - 'body' => json_encode($data), - 'headers' => $this->getHeaders(), - ]); - } catch (BadResponseException $e) { - throw new EntityCreationException($e->getMessage(), $e->getCode(), $e); - } - - return json_decode($response->getBody() - ->getContents(), true); - } - - /** - * @param $id - * @param $data - * @return array - * @throws EntityCreationException - */ - public function update($id, $data) - { - try { - $response = $this->httpClient->patch($this->getPathWithId($id), [ - 'body' => json_encode($data), - 'headers' => $this->getHeaders(true), - ]); - } catch (BadResponseException $e) { - throw new EntityCreationException($e->getMessage(), $e->getCode(), $e); - } - - return json_decode($response->getBody() - ->getContents(), true); - } - - /** - * @param $id - * @param array $filters - * @return mixed - * @throws EntityFetchException - */ - public function find($id, $filters = []) - { - try { - $response = $this->httpClient->get($this->getPathWithId($id), [ - 'headers' => $this->getHeaders(), - 'query' => $filters - ]); - } catch (BadResponseException $e) { - throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); - } - - return json_decode($response->getBody() - ->getContents(), true); - } - - /** - * @param array $filters - * @param int $pageSize - * @param int $page - * @return array - * @throws EntityFetchException - */ - public function findAll($filters = [], $pageSize = 10, $page = 1) - { - try { - $response = $this->httpClient->get($this->getPath(), [ - 'headers' => $this->getHeaders(), - 'query' => array_merge(compact('pageSize', 'page'), $filters) - ]); - } catch (BadResponseException $e) { - throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); - } - - return json_decode($response->getBody() - ->getContents(), true); - } - - /** - * @param bool $patch - * @return array - */ - protected function getHeaders($patch = false): array - { - $headers = [ - 'x-api-key' => $this->credentials['api_key'], - 'content-type' => 'application/json' - ]; - - if ($patch) { - $headers['content-type'] = 'application/merge-patch+json'; - } - - return $headers; - } - - /** - * @param $id - * @return string - */ - protected function getPathWithId($id): string - { - return $this->getPath() . '/' . $id; - } - - abstract protected function getPath(); -} diff --git a/src/Cohort/Cohort.php b/src/Cohort/Cohort.php new file mode 100644 index 0000000..849bfcb --- /dev/null +++ b/src/Cohort/Cohort.php @@ -0,0 +1,20 @@ +getBody() ->getContents()); } -} \ No newline at end of file + + public static function getModel() + { + return new LoyaltyGroup(); + } +} diff --git a/src/MagicFields/MagicField.php b/src/MagicFields/MagicField.php new file mode 100644 index 0000000..ba6f124 --- /dev/null +++ b/src/MagicFields/MagicField.php @@ -0,0 +1,25 @@ +data = $data; + $this->total = $total; + } + + public function getIterator() + { + return new \ArrayIterator($this->data); + } + + public function getGenerator() + { + foreach ($this->data as $item) { + yield $item; + } + } + + public function __toString() + { + return json_encode($this->data, JSON_PRETTY_PRINT); + } +} diff --git a/src/RealifeServiceResolver.php b/src/RealifeServiceResolver.php new file mode 100644 index 0000000..63ff67d --- /dev/null +++ b/src/RealifeServiceResolver.php @@ -0,0 +1,165 @@ +validateConfig($config); + $this->setRegion($config[self::CONFIG_REGION]); + $this->setEnvironment($config[self::CONFIG_ENVIRONMENT]); + $this->setVpc($config[self::CONFIG_VPC]); + $this->setAuthType($config[self::CONFIG_AUTH_TYPE]); + $this->credentials = $config[self::CONFIG_CREDENTIALS]; + $this->endpoint = $config[self::CONFIG_ENDPOINT]; + } + + /** + * @param string $serviceIdentifier + * @return ServiceClient + * @throws Exception\ServiceNotFoundException + */ + public function getService(string $serviceIdentifier): ServiceClient + { + $service = (new ServiceMap())->resolve($serviceIdentifier); + + return new $service($this->endpoint, [$this->authType => $this->credentials]); + } + + /** + * @param $config + * @return bool + * @throws UnsupportedConfigParameterException + * @throws ConfigParameterRequiredException + */ + private function validateConfig($config) + { + $configKeys = array_keys($config); + $schemaKeys = self::CONFIG_SCHEMA; + $res = array_merge(array_diff($configKeys, $schemaKeys), array_diff($schemaKeys, $configKeys)); + + if (empty($res)) { + return true; + } + + $field = $res[0]; + + if (in_array($field, self::CONFIG_SCHEMA)) { + throw new ConfigParameterRequiredException($field); + } else { + throw new UnsupportedConfigParameterException($field); + } + } + + /** + * @param $environment + * @throws InvalidConfigValueException + */ + private function setEnvironment($environment) + { + $this->validateSupported($environment, self::CONFIG_ENVIRONMENT, self::SUPPORTED_ENVIRONMENTS); + + $this->environment = $environment; + } + + /** + * @param $region + * @throws InvalidConfigValueException + */ + private function setRegion($region) + { + $this->validateSupported($region, self::CONFIG_REGION, self::SUPPORTED_REGIONS); + + $this->region = $region; + } + + /** + * @param $value + * @param $configKey + * @param $configArray + * @throws InvalidConfigValueException + */ + private function validateSupported($value, $configKey, $configArray) + { + if (!in_array($value, $configArray)) { + throw new InvalidConfigValueException([$value, $configKey]); + } + } + + /** + * @param $vpc + * @throws InvalidConfigValueException + */ + private function setVpc($vpc) + { + if (!is_bool($vpc)) { + throw new InvalidConfigValueException([$vpc, self::CONFIG_VPC]); + } + + $this->vpc = $vpc; + } + + /** + * @param $authType + * @throws InvalidConfigValueException + */ + private function setAuthType($authType) + { + $this->validateSupported($authType, self::CONFIG_AUTH_TYPE, self::SUPPORTED_AUTH_TYPES); + + $this->authType = $authType; + } +} diff --git a/src/Season/Season.php b/src/Season/Season.php new file mode 100644 index 0000000..8d01183 --- /dev/null +++ b/src/Season/Season.php @@ -0,0 +1,24 @@ +domain = $this->addScheme($domain, $scheme); + $this->credentials = $credentials; + $this->httpClient = new \GuzzleHttp\Client([ + 'base_uri' => rtrim($this->domain, '/'), + 'timeout' => $timeout + ]); + } + + abstract protected function getPath(); + + abstract public static function getModel(); + + function addScheme($domain, $scheme) + { + return parse_url($domain, PHP_URL_SCHEME) === null ? + $scheme . $domain : $domain; + } + + /** + * @param $data + * @return array + * @throws EntityCreationException + */ + public function create($data) + { + try { + $response = $this->httpClient->post($this->getPath(), [ + 'body' => json_encode($data), + 'headers' => $this->getHeaders(), + ]); + } catch (BadResponseException $e) { + throw new EntityCreationException($e->getMessage(), $e->getCode(), $e); + } + + return json_decode($response->getBody() + ->getContents(), true); + } + + /** + * @param $id + * @param $data + * @return array + * @throws EntityCreationException + */ + public function update($id, $data) + { + try { + $response = $this->httpClient->patch($this->getPathWithId($id), [ + 'body' => json_encode($data), + 'headers' => $this->getHeaders(true), + ]); + } catch (BadResponseException $e) { + throw new EntityCreationException($e->getMessage(), $e->getCode(), $e); + } + + return json_decode($response->getBody() + ->getContents(), true); + } + + /** + * @param $id + * @param array $filters + * @return mixed + * @throws EntityFetchException + */ + public function find($id, $filters = []) + { + try { + $response = $this->get($this->getPathWithId($id), $filters); + } catch (BadResponseException $e) { + throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); + } + + return json_decode($response->getBody() + ->getContents(), true); + } + + /** + * @param array $filters + * @param int $pageSize + * @param int $page + * @return array + * @throws EntityFetchException + */ + public function findAll($filters = [], $pageSize = 10, $page = 1) + { + try { + $response = $this->get($this->getPath(), array_merge(compact('pageSize', 'page'), $filters)); + } catch (BadResponseException $e) { + throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); + } + + return json_decode($response->getBody() + ->getContents(), true); + } + + /** + * @param bool $patch + * @return array + */ + protected function getHeaders($patch = false): array + { + $headers = [ + 'x-api-key' => $this->credentials['api_key'], + 'content-type' => 'application/json' + ]; + + if ($patch) { + $headers['content-type'] = 'application/merge-patch+json'; + } + + return $headers; + } + + /** + * @param $id + * @return string + */ + protected function getPathWithId($id): string + { + return $this->getPath() . '/' . $id; + } + + /** + * @param array $filters + * @param int $pageSize + * @param int $page + * @return RealifeResult + * @throws EntityFetchException + */ + public function getCollection($filters = [], $pageSize = 2, $page = 1): RealifeResult + { + try { + $response = $this->get($this->getPath(), array_merge(compact('pageSize', 'page'), $filters)); + } catch (BadResponseException $e) { + throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); + } + + return $this->getRealifeResult($response); + } + + public function getUniqueItem($resource, $params) + { + //todo need to implement + } + + /** + * @param $id + * @param array $filters + * @return Item + * @throws EntityFetchException + */ + public function getItem($id, $filters = []) + { + try { + $response = $this->get($this->getPathWithId($id), $filters); + } catch (BadResponseException $e) { + throw new EntityFetchException($e->getMessage(), $e->getCode(), $e); + } + + return $this->getItemResponse($response); + } + + public function postCollection($resource, $data) + { + //todo need to implement + } + + public function putItem($resource, $id) + { + //todo need to implement + } + + public function patchItem($resource, $id) + { + //todo need to implement + } + + public function deleteItem($resource, $id) + { + //todo need to implement + } + + public function collectionOperation($resource, $method, $data) + { + //todo need to implement + } + + public function itemOperation($resource, $action, $id) + { + //todo need to implement + } + + /** + * @param \Psr\Http\Message\ResponseInterface $response + * @return RealifeResult + */ + private function getRealifeResult(\Psr\Http\Message\ResponseInterface $response): RealifeResult + { + $response = $this->decodeResponse($response); + $data = $response['hydra:member'] ?? []; + $total = $response['hydra:totalItems'] ?? 0; + + $data = array_map(function ($item) { + if (!$this->isResource($item)) { + return null; + } + + return $this->hydrateItem($item['@id'], $item['@type'], $item); + }, $data); + + return new RealifeResult($data, $total); + } + + private function hydrateItem(string $iri, string $type, array $responseItem): ?Item + { + $object = $this->resolveModel($type); + $object->iri = $iri; + $object->_type = $type; + + foreach ($responseItem as $property => $value) { + if (is_array($value)) { + $object->$property = $this->hydrateSubProperty($value); + } else { + $object->$property = $value; + } + } + + return $object; + } + + /** + * @param array $subProperty + * @return Item + */ + private function hydrateSubProperty(array $subProperty) + { + if (isset($subProperty[0])) { + $property = $this->processArrayProperty($subProperty); + } else { + $property = $this->processProperty($subProperty); + } + + return $property; + } + + private function convertToSnakeCase(string $type) + { + return strtolower(preg_replace('/(?convertToSnakeCase($type); + + if (isset(ServiceMap::SERVICE_MAP[$type])) { + /** + * @var $clientMapping self + */ + $clientMapping = ServiceMap::SERVICE_MAP[$type]; + + return $clientMapping::getModel(); + } else { + return new Item(); + } + } + + /** + * @param \Psr\Http\Message\ResponseInterface $response + * @return Item + */ + private function getItemResponse(\Psr\Http\Message\ResponseInterface $response): Item + { + $response = $this->decodeResponse($response); + + return $this->hydrateItem($response['@id'], $response['@type'], $response); + } + + /** + * @param \Psr\Http\Message\ResponseInterface $response + * @return mixed + */ + private function decodeResponse(\Psr\Http\Message\ResponseInterface $response) + { + return json_decode($response->getBody()->getContents(), true); + } + + /** + * @param $path + * @param array $filters + * @param array $headers + * @return \Psr\Http\Message\ResponseInterface + */ + private function get($path, array $filters, $headers = []): \Psr\Http\Message\ResponseInterface + { + return $this->httpClient->get($path, [ + 'headers' => array_merge($this->getHeaders(), $headers), + 'query' => $filters + ]); + } + + private function isResource($item): bool + { + return isset($item['@id']); + } + + /** + * @param array $arrayProperty + * @return array + */ + private function processArrayProperty(array $arrayProperty): array + { + $property = []; + + foreach ($arrayProperty as $item) { + if (!$this->isResource($item)) { + $property[] = $item; + } else { + $property[] = $this->hydrateItem($item['@id'], $item['@type'], $item); + } + } + + return $property; + } + + /** + * @param array $subProperty + * @return Item|null + */ + private function processProperty(array $subProperty) + { + if (!$this->isResource($subProperty)) { + return null; + } + + return $this->hydrateItem($subProperty['@id'], $subProperty['@type'], $subProperty); + } +} diff --git a/src/ServiceMap.php b/src/ServiceMap.php new file mode 100644 index 0000000..d32c077 --- /dev/null +++ b/src/ServiceMap.php @@ -0,0 +1,196 @@ + AppService::class, + self::BOOKING => BookingService::class, + self::COHORT => CohortService::class, + self::COMPETITION => CompetitionService::class, + self::CURRENCY => CurrencyService::class, + self::DEVICE => DeviceService::class, + self::DEVICE_PREFERENCE => DevicePreferenceService::class, + self::DEVICE_TOKEN => DeviceTokenService::class, + self::EVENT => EventService::class, + self::EVENT_ACTIVITY => EventActivityService::class, + self::EVENT_CATEGORY => EventCategoryService::class, + self::EVENT_DATE => EventDateService::class, + self::EVENT_INTEGRATION => EventIntegrationService::class, + self::EVENT_STAGE => EventStageService::class, + self::EVENT_TRANSLATION => EventTranslationService::class, + self::EVENT_USER_ACTION => EventUserActionService::class, + self::FIXTURE => FixtureService::class, + self::FORM => FormService::class, + self::FORM_FIELD => FormFieldService::class, + self::FULFILMENT_POINT => FulfilmentPointService::class, + self::FULFILMENT_POINT_CATEGORY => FulfilmentPointCategoryService::class, + self::INTEGRATION => IntegrationService::class, + self::LEAGUE_TABLE => LeagueTableService::class, + self::LEAGUE_TABLE_GROUP => LeagueTableGroupService::class, + self::LOYALTY_GROUP => LoyaltyGroupsService::class, + self::MAGIC_FIELD => MagicFieldsService::class, + self::MERCHANT_CUSTOMER => MerchantCustomerService::class, + self::NEWS => NewsService::class, + self::ORDER => OrderService::class, + self::PRODUCT => ProductService::class, + self::PRODUCT_CATEGORY => ProductCategoryService::class, + self::PRODUCT_MODIFIER_ITEM => ProductModifierItemService::class, + self::PRODUCT_MODIFIER_LIST => ProductModifierListService::class, + self::PRODUCT_MODIFIER_LIST_TRANSLATION => ProductModifierListTranslationService::class, + self::PRODUCT_VARIANT => ProductVariantService::class, + self::PRODUCT_VARIANT_STOCK => ProductVariantStockService::class, + self::PUSH_APP => PushAppService::class, + self::PUSH_BROADCAST => PushBroadcastService::class, + self::PUSH_CONSENT => PushConsentService::class, + self::SEASON => SeasonService::class, + self::SOCIAL_MEDIA => SocialMediaService::class, + self::SOCIAL_MEDIA_TRANSLATION => SocialMediaTranslationService::class, + self::SPORT_VENUE => SportVenueService::class, + self::TEAM => TeamService::class, + self::TICKET => TicketService::class, + self::TICKET_AUTH => TicketAuthService::class, + self::TICKET_INTEGRATION => TicketIntegration::class, + self::USEFUL_INFO => UsefulInfoService::class, + self::USEFUL_INFO_TRANSLATION => UsefulInfoTranslation::class, + self::USER => UsersService::class, + self::USER_EMAIL => UserEmailService::class, + self::USER_INFO => UserInfoService::class, + self::VENUE => VenueService::class, + self::WORKFLOW => WorkflowService::class, + self::WORKFLOW_ACTION => WorkflowActionService::class, + self::WORKFLOW_TRIGGER => WorkflowTriggerService::class, + ]; + + /** + * @param string $service + * @return string + * @throws ServiceNotFoundException + */ + public function resolve(string $service): string + { + if (!array_key_exists($service, self::SERVICE_MAP)) { + throw new ServiceNotFoundException("No such service {$service}"); + } + + return self::SERVICE_MAP[$service]; + } +} diff --git a/src/SocialMedia/SocialMedia.php b/src/SocialMedia/SocialMedia.php new file mode 100644 index 0000000..4d5ec75 --- /dev/null +++ b/src/SocialMedia/SocialMedia.php @@ -0,0 +1,26 @@ +getBody() ->getContents(), true); } + + public static function getModel() + { + return new User(); + } } diff --git a/src/UserEmail/UserEmail.php b/src/UserEmail/UserEmail.php new file mode 100644 index 0000000..612bc04 --- /dev/null +++ b/src/UserEmail/UserEmail.php @@ -0,0 +1,18 @@ + Date: Tue, 7 Jul 2020 21:40:05 +0100 Subject: [PATCH 2/2] sdk-improvements fixed broken service reference --- src/ServiceMap.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ServiceMap.php b/src/ServiceMap.php index d32c077..149d3c6 100644 --- a/src/ServiceMap.php +++ b/src/ServiceMap.php @@ -51,9 +51,9 @@ use LiveStyled\Team\TeamService; use LiveStyled\Ticket\TicketService; use LiveStyled\TicketAuth\TicketAuthService; -use LiveStyled\TicketIntegration\TicketIntegration; +use LiveStyled\TicketIntegration\TicketIntegrationService; use LiveStyled\UsefulInfo\UsefulInfoService; -use LiveStyled\UsefulInfoTranslation\UsefulInfoTranslation; +use LiveStyled\UsefulInfoTranslation\UsefulInfoTranslationService; use LiveStyled\User\UsersService; use LiveStyled\UserEmail\UserEmailService; use LiveStyled\UserInfo\UserInfoService; @@ -168,9 +168,9 @@ class ServiceMap self::TEAM => TeamService::class, self::TICKET => TicketService::class, self::TICKET_AUTH => TicketAuthService::class, - self::TICKET_INTEGRATION => TicketIntegration::class, + self::TICKET_INTEGRATION => TicketIntegrationService::class, self::USEFUL_INFO => UsefulInfoService::class, - self::USEFUL_INFO_TRANSLATION => UsefulInfoTranslation::class, + self::USEFUL_INFO_TRANSLATION => UsefulInfoTranslationService::class, self::USER => UsersService::class, self::USER_EMAIL => UserEmailService::class, self::USER_INFO => UserInfoService::class,