From f96dc8bef997394712cc8d66447472c8c1fb70d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Tue, 20 Dec 2016 15:06:15 +0100 Subject: [PATCH] Split api into manager and client --- app/config/services.yml | 19 +++- .../{ => Api}/Client/Brickset/Brickset.php | 96 +++++++------------ .../Brickset/Entity/AdditionalImage.php | 2 +- .../Client/Brickset/Entity/Instructions.php | 2 +- .../Client/Brickset/Entity/Review.php | 2 +- .../{ => Api}/Client/Brickset/Entity/Set.php | 2 +- .../Client/Brickset/Entity/Subtheme.php | 2 +- .../Client/Brickset/Entity/Theme.php | 2 +- .../{ => Api}/Client/Brickset/Entity/Year.php | 2 +- .../Converter/PartPropertyNameConverter.php | 2 +- .../Client/Rebrickable/Entity/Color.php | 2 +- .../Client/Rebrickable/Entity/Part.php | 2 +- .../Client/Rebrickable/Entity/Set.php | 2 +- .../Client/Rebrickable/Rebrickable.php | 10 +- src/AppBundle/Api/Manager/BricksetManager.php | 43 +++++++++ .../Api/Manager/RebrickableManager.php | 33 +++++++ src/AppBundle/Controller/SetsController.php | 7 +- src/AppBundle/Form/FilterSetType.php | 22 ++--- src/AppBundle/Service/CollectionService.php | 58 ++++------- 19 files changed, 172 insertions(+), 138 deletions(-) rename src/AppBundle/{ => Api}/Client/Brickset/Brickset.php (65%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/AdditionalImage.php (96%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Instructions.php (94%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Review.php (98%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Set.php (99%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Subtheme.php (97%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Theme.php (97%) rename src/AppBundle/{ => Api}/Client/Brickset/Entity/Year.php (94%) rename src/AppBundle/{ => Api}/Client/Rebrickable/Converter/PartPropertyNameConverter.php (90%) rename src/AppBundle/{ => Api}/Client/Rebrickable/Entity/Color.php (98%) rename src/AppBundle/{ => Api}/Client/Rebrickable/Entity/Part.php (98%) rename src/AppBundle/{ => Api}/Client/Rebrickable/Entity/Set.php (95%) rename src/AppBundle/{ => Api}/Client/Rebrickable/Rebrickable.php (95%) create mode 100644 src/AppBundle/Api/Manager/BricksetManager.php create mode 100644 src/AppBundle/Api/Manager/RebrickableManager.php diff --git a/app/config/services.yml b/app/config/services.yml index d536658..8cd9ac7 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -1,15 +1,26 @@ services: client.brickset: - class: AppBundle\Client\Brickset\Brickset + class: AppBundle\Api\Client\Brickset\Brickset arguments: ['%brickset_apikey%'] client.rebrickable: - class: AppBundle\Client\Rebrickable\Rebrickable + class: AppBundle\Api\Client\Rebrickable\Rebrickable arguments: ['%rebrickable_apikey%'] + + manager.brickset: + class: AppBundle\Api\Manager\BricksetManager + arguments: ['@client.brickset'] + manager.rebrickable: + class: AppBundle\Api\Manager\RebrickableManager + arguments: ['@client.rebrickable'] + app.collection_service: class: AppBundle\Service\CollectionService - arguments: ['@client.brickset','@client.rebrickable'] + arguments: ['@doctrine.orm.entity_manager', '@manager.brickset','@manager.rebrickable'] + app.model_loader_service: + class: AppBundle\Service\ModelLoaderService + arguments: ['@doctrine.orm.entity_manager','%kernel.root_dir%/../var/data/LDrawLibrary'] app.form.filter_set: class: AppBundle\Form\FilterSetType - arguments: ['@app.collection_service'] + arguments: ['@manager.brickset'] tags: - { name: form.type } \ No newline at end of file diff --git a/src/AppBundle/Client/Brickset/Brickset.php b/src/AppBundle/Api/Client/Brickset/Brickset.php similarity index 65% rename from src/AppBundle/Client/Brickset/Brickset.php rename to src/AppBundle/Api/Client/Brickset/Brickset.php index 045e4ce..7ac2831 100644 --- a/src/AppBundle/Client/Brickset/Brickset.php +++ b/src/AppBundle/Api/Client/Brickset/Brickset.php @@ -1,14 +1,14 @@ apiKey = $apiKey; } - private function call($method, $parameters) + public function call($method, $parameters) { $parameters['apiKey'] = $this->apiKey; try { - return $this->__soapCall($method, [$parameters]); + return $this->__soapCall($method, [$parameters])->{$method.'Result'}; } catch (\SoapFault $e) { //TODO throw new LogicException($e->getCode().' - '.$e->getMessage()); + } catch (ContextErrorException $e) { + //TODO empty resposne + throw new LogicException($method.' - '.$e->getMessage()); } } @@ -94,13 +98,9 @@ class Brickset extends \SoapClient } } - try { - $response = $this->call('getSets', $parameters)->getSetsResult->sets; + $response = $this->call('getSets', $parameters)->sets; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -112,11 +112,7 @@ class Brickset extends \SoapClient { $parameters = ['userHash' => $this->userHash, 'SetID' => $SetID]; - try { - return $this->call('getSet', $parameters)->getSetResult->sets; - } catch (ContextErrorException $e) { - return null; - } + return $this->call('getSet', $parameters)->sets; } /** @@ -130,13 +126,9 @@ class Brickset extends \SoapClient { $parameters = ['minutesAgo' => $minutesAgo]; - try { - $response = $this->call('getRecentlyUpdatedSets', $parameters)->getRecentlyUpdatedSetsResult->sets; + $response = $this->call('getRecentlyUpdatedSets', $parameters)->sets; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -150,13 +142,9 @@ class Brickset extends \SoapClient { $parameters = ['setID' => $setID]; - try { - $response = $this->call('getAdditionalImages', $parameters)->getAdditionalImagesResult->additionalImages; + $response = $this->call('getAdditionalImages', $parameters)->additionalImages; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -170,13 +158,9 @@ class Brickset extends \SoapClient { $parameters = ['setID' => $setID]; - try { - $response = $this->call('getReviews', $parameters)->getReviewsResult->reviews; + $response = $this->call('getReviews', $parameters)->reviews; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -190,13 +174,9 @@ class Brickset extends \SoapClient { $parameters = ['setID' => $setID]; - try { - $response = $this->call('getInstructions', $parameters)->getInstructionsResult->instructions; + $response = $this->call('getInstructions', $parameters)->instructions; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return null; - } + return is_array($response) ? $response : [$response]; } /** @@ -206,11 +186,7 @@ class Brickset extends \SoapClient */ public function getThemes() { - try { - return $this->call('getThemes', [])->getThemesResult->themes; - } catch (ContextErrorException $e) { - return []; - } + return $this->call('getThemes', [])->themes; } /** @@ -224,13 +200,9 @@ class Brickset extends \SoapClient { $parameters = ['theme' => $theme]; - try { - $response = $this->call('getSubthemes', $parameters)->getSubthemesResult->subthemes; + $response = $this->call('getSubthemes', $parameters)->subthemes; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -244,13 +216,9 @@ class Brickset extends \SoapClient { $parameters = ['theme' => $theme]; - try { - $response = $this->call('getYears', $parameters)->getYearsResult->years; + $response = $this->call('getYears', $parameters)->years; - return is_array($response) ? $response : [$response]; - } catch (ContextErrorException $e) { - return []; - } + return is_array($response) ? $response : [$response]; } /** @@ -265,7 +233,7 @@ class Brickset extends \SoapClient { $parameters = ['username' => $username, 'password' => $password]; - $response = $this->call('login', $parameters)->loginResult; + $response = $this->call('login', $parameters); if ($response == 'INVALIDKEY') { return false; } elseif (strpos($response, 'ERROR:') === 0) { @@ -284,6 +252,6 @@ class Brickset extends \SoapClient */ public function checkKey() { - return ($this->call('checkKey', [])->checkKeyResult) == 'OK' ? true : false; + return ($this->call('checkKey', [])) == 'OK' ? true : false; } } diff --git a/src/AppBundle/Client/Brickset/Entity/AdditionalImage.php b/src/AppBundle/Api/Client/Brickset/Entity/AdditionalImage.php similarity index 96% rename from src/AppBundle/Client/Brickset/Entity/AdditionalImage.php rename to src/AppBundle/Api/Client/Brickset/Entity/AdditionalImage.php index cc11372..be95c32 100644 --- a/src/AppBundle/Client/Brickset/Entity/AdditionalImage.php +++ b/src/AppBundle/Api/Client/Brickset/Entity/AdditionalImage.php @@ -1,6 +1,6 @@ bricksetClient = $bricksetClient; + } + + public function getThemes() + { + return $this->bricksetClient->getThemes(); + } + + public function getSubthemesByTheme($theme) + { + return $this->bricksetClient->getSubthemes($theme); + } + + public function getYearsByTheme($theme) + { + return $this->bricksetClient->getYears($theme); + } + + public function getSetById($id) + { + return $this->bricksetClient->getSet($id); + } +} diff --git a/src/AppBundle/Api/Manager/RebrickableManager.php b/src/AppBundle/Api/Manager/RebrickableManager.php new file mode 100644 index 0000000..df94f08 --- /dev/null +++ b/src/AppBundle/Api/Manager/RebrickableManager.php @@ -0,0 +1,33 @@ +rebrickableClient = $rebrickableClient; + } + + public function getSetParts($setNumber) + { + return $this->rebrickableClient->getSetParts($setNumber); + } + + public function getPartById($id) + { + return $this->rebrickableClient->getPart($id); + } +} diff --git a/src/AppBundle/Controller/SetsController.php b/src/AppBundle/Controller/SetsController.php index 41c0470..d4109e2 100644 --- a/src/AppBundle/Controller/SetsController.php +++ b/src/AppBundle/Controller/SetsController.php @@ -8,7 +8,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; /** - * @Route("/sets") + * @Route("/brickset/sets") */ class SetsController extends Controller { @@ -43,12 +43,13 @@ class SetsController extends Controller */ public function detailAction(Request $request, $id, $name = null) { - $collectionService = $this->get('app.collection_service'); + $set = $this->get('manager.brickset')->getSetById($id);; - $set = $collectionService->getSetById($id); + $parts = $this->get('app.collection_service')->getSet($set->getNumber().'-'.$set->getNumberVariant())->getParts(); return $this->render('sets/detail.html.twig', [ 'set' => $set, + 'parts' => $parts, ]); } } diff --git a/src/AppBundle/Form/FilterSetType.php b/src/AppBundle/Form/FilterSetType.php index 6461b65..1e68eaa 100644 --- a/src/AppBundle/Form/FilterSetType.php +++ b/src/AppBundle/Form/FilterSetType.php @@ -2,10 +2,10 @@ namespace AppBundle\Form; -use AppBundle\Client\Brickset\Entity\Subtheme; -use AppBundle\Client\Brickset\Entity\Theme; -use AppBundle\Client\Brickset\Entity\Year; -use AppBundle\Service\CollectionService; +use AppBundle\Api\Client\Brickset\Entity\Subtheme; +use AppBundle\Api\Client\Brickset\Entity\Theme; +use AppBundle\Api\Client\Brickset\Entity\Year; +use AppBundle\Api\Manager\BricksetManager; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -16,18 +16,18 @@ use Symfony\Component\Form\FormEvents; class FilterSetType extends AbstractType { - private $collectionService; + private $bricksetManager; - public function __construct(CollectionService $collectionService) + public function __construct(BricksetManager $bricksetManager) { - $this->collectionService = $collectionService; + $this->bricksetManager = $bricksetManager; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add("theme", ChoiceType::class, [ - 'choices' => $this->collectionService->getThemes(), + 'choices' => $this->bricksetManager->getThemes(), 'choice_label' => function(Theme $theme = null) { return $theme ? $theme->getTheme().' ('.$theme->getSetCount().')' : ''; }, @@ -37,8 +37,8 @@ class FilterSetType extends AbstractType ]); $formModifier = function (Form $form, Theme $theme = null) { - $subthemes = null === $theme ? [] : $this->collectionService->getSubthemesByTheme($theme); - $years = null === $theme ? [] : $this->collectionService->getYearsByTheme($theme); + $subthemes = null === $theme ? [] : $this->bricksetManager->getSubthemesByTheme($theme); + $years = null === $theme ? [] : $this->bricksetManager->getYearsByTheme($theme); $form->add("subtheme", ChoiceType::class, [ 'choices' => $subthemes, @@ -63,7 +63,7 @@ class FilterSetType extends AbstractType FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) { $data = $event->getData(); - $theme = $data === null ? null : $this->collectionService->getThemes()[$data['theme']]; + $theme = $data === null ? null : $this->bricksetManager->getThemes()[$data['theme']]; $formModifier($event->getForm(), $theme); } ); diff --git a/src/AppBundle/Service/CollectionService.php b/src/AppBundle/Service/CollectionService.php index bf5acd5..8cdd0e3 100644 --- a/src/AppBundle/Service/CollectionService.php +++ b/src/AppBundle/Service/CollectionService.php @@ -2,60 +2,38 @@ namespace AppBundle\Service; -use AppBundle\Client\Brickset\Brickset; -use AppBundle\Client\Rebrickable\Rebrickable; +use AppBundle\Api\Client\Rebrickable\Rebrickable; +use AppBundle\Api\Manager\BricksetManager; +use Doctrine\ORM\EntityManager; class CollectionService { /** - * @var Brickset client + * @var BricksetManager */ - protected $bricksetClient; + protected $bricksetManager; /** * @var Rebrickable client */ - protected $rebrickableClient; + protected $rebrickableManager; + + /** + * @var EntityManager + */ + private $em; /** * CollectionService constructor. * - * @param $bricksetClient - * @param $rebrickableClient + * @param $em + * @param $bricksetManager + * @param $rebrickableManager */ - public function __construct($bricksetClient, $rebrickableClient) + public function __construct($em, $bricksetManager, $rebrickableManager) { - $this->bricksetClient = $bricksetClient; - $this->rebrickableClient = $rebrickableClient; - } - - public function getThemes() - { - return $this->bricksetClient->getThemes(); - } - - public function getSubthemesByTheme($theme) - { - return $this->bricksetClient->getSubthemes($theme); - } - - public function getYearsByTheme($theme) - { - return $this->bricksetClient->getYears($theme); - } - - public function getSetById($id) - { - return $this->bricksetClient->getSet($id); - } - - public function getSetParts($setNumber) - { - return $this->rebrickableClient->getSetParts($setNumber); - } - - public function getPartById($id) - { - return $this->rebrickableClient->getPart($id); + $this->em = $em; + $this->bricksetManager = $bricksetManager; + $this->rebrickableManager = $rebrickableManager; } }