1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-18 21:20:09 -07:00

Split api into manager and client

This commit is contained in:
David Hübner 2016-12-20 15:06:15 +01:00
parent c9cc467b84
commit f96dc8bef9
19 changed files with 172 additions and 138 deletions

View File

@ -1,15 +1,26 @@
services: services:
client.brickset: client.brickset:
class: AppBundle\Client\Brickset\Brickset class: AppBundle\Api\Client\Brickset\Brickset
arguments: ['%brickset_apikey%'] arguments: ['%brickset_apikey%']
client.rebrickable: client.rebrickable:
class: AppBundle\Client\Rebrickable\Rebrickable class: AppBundle\Api\Client\Rebrickable\Rebrickable
arguments: ['%rebrickable_apikey%'] 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: app.collection_service:
class: AppBundle\Service\CollectionService 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: app.form.filter_set:
class: AppBundle\Form\FilterSetType class: AppBundle\Form\FilterSetType
arguments: ['@app.collection_service'] arguments: ['@manager.brickset']
tags: tags:
- { name: form.type } - { name: form.type }

View File

@ -1,14 +1,14 @@
<?php <?php
namespace AppBundle\Client\Brickset; namespace AppBundle\Api\Client\Brickset;
use AppBundle\Client\Brickset\Entity\AdditionalImage; use AppBundle\Api\Client\Brickset\Entity\AdditionalImage;
use AppBundle\Client\Brickset\Entity\Instructions; use AppBundle\Api\Client\Brickset\Entity\Instructions;
use AppBundle\Client\Brickset\Entity\Review; use AppBundle\Api\Client\Brickset\Entity\Review;
use AppBundle\Client\Brickset\Entity\Set; use AppBundle\Api\Client\Brickset\Entity\Set;
use AppBundle\Client\Brickset\Entity\Subtheme; use AppBundle\Api\Client\Brickset\Entity\Subtheme;
use AppBundle\Client\Brickset\Entity\Theme; use AppBundle\Api\Client\Brickset\Entity\Theme;
use AppBundle\Client\Brickset\Entity\Year; use AppBundle\Api\Client\Brickset\Entity\Year;
use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Debug\Exception\ContextErrorException; use Symfony\Component\Debug\Exception\ContextErrorException;
@ -17,6 +17,7 @@ class Brickset extends \SoapClient
const WSDL = 'http://brickset.com/api/v2.asmx?WSDL'; const WSDL = 'http://brickset.com/api/v2.asmx?WSDL';
private $apiKey = ''; private $apiKey = '';
private $userHash = ''; private $userHash = '';
/** /**
@ -60,15 +61,18 @@ class Brickset extends \SoapClient
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
} }
private function call($method, $parameters) public function call($method, $parameters)
{ {
$parameters['apiKey'] = $this->apiKey; $parameters['apiKey'] = $this->apiKey;
try { try {
return $this->__soapCall($method, [$parameters]); return $this->__soapCall($method, [$parameters])->{$method.'Result'};
} catch (\SoapFault $e) { } catch (\SoapFault $e) {
//TODO //TODO
throw new LogicException($e->getCode().' - '.$e->getMessage()); 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)->sets;
$response = $this->call('getSets', $parameters)->getSetsResult->sets;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -112,11 +112,7 @@ class Brickset extends \SoapClient
{ {
$parameters = ['userHash' => $this->userHash, 'SetID' => $SetID]; $parameters = ['userHash' => $this->userHash, 'SetID' => $SetID];
try { return $this->call('getSet', $parameters)->sets;
return $this->call('getSet', $parameters)->getSetResult->sets;
} catch (ContextErrorException $e) {
return null;
}
} }
/** /**
@ -130,13 +126,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['minutesAgo' => $minutesAgo]; $parameters = ['minutesAgo' => $minutesAgo];
try { $response = $this->call('getRecentlyUpdatedSets', $parameters)->sets;
$response = $this->call('getRecentlyUpdatedSets', $parameters)->getRecentlyUpdatedSetsResult->sets;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -150,13 +142,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['setID' => $setID]; $parameters = ['setID' => $setID];
try { $response = $this->call('getAdditionalImages', $parameters)->additionalImages;
$response = $this->call('getAdditionalImages', $parameters)->getAdditionalImagesResult->additionalImages;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -170,13 +158,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['setID' => $setID]; $parameters = ['setID' => $setID];
try { $response = $this->call('getReviews', $parameters)->reviews;
$response = $this->call('getReviews', $parameters)->getReviewsResult->reviews;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -190,13 +174,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['setID' => $setID]; $parameters = ['setID' => $setID];
try { $response = $this->call('getInstructions', $parameters)->instructions;
$response = $this->call('getInstructions', $parameters)->getInstructionsResult->instructions;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return null;
}
} }
/** /**
@ -206,11 +186,7 @@ class Brickset extends \SoapClient
*/ */
public function getThemes() public function getThemes()
{ {
try { return $this->call('getThemes', [])->themes;
return $this->call('getThemes', [])->getThemesResult->themes;
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -224,13 +200,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['theme' => $theme]; $parameters = ['theme' => $theme];
try { $response = $this->call('getSubthemes', $parameters)->subthemes;
$response = $this->call('getSubthemes', $parameters)->getSubthemesResult->subthemes;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -244,13 +216,9 @@ class Brickset extends \SoapClient
{ {
$parameters = ['theme' => $theme]; $parameters = ['theme' => $theme];
try { $response = $this->call('getYears', $parameters)->years;
$response = $this->call('getYears', $parameters)->getYearsResult->years;
return is_array($response) ? $response : [$response]; return is_array($response) ? $response : [$response];
} catch (ContextErrorException $e) {
return [];
}
} }
/** /**
@ -265,7 +233,7 @@ class Brickset extends \SoapClient
{ {
$parameters = ['username' => $username, 'password' => $password]; $parameters = ['username' => $username, 'password' => $password];
$response = $this->call('login', $parameters)->loginResult; $response = $this->call('login', $parameters);
if ($response == 'INVALIDKEY') { if ($response == 'INVALIDKEY') {
return false; return false;
} elseif (strpos($response, 'ERROR:') === 0) { } elseif (strpos($response, 'ERROR:') === 0) {
@ -284,6 +252,6 @@ class Brickset extends \SoapClient
*/ */
public function checkKey() public function checkKey()
{ {
return ($this->call('checkKey', [])->checkKeyResult) == 'OK' ? true : false; return ($this->call('checkKey', [])) == 'OK' ? true : false;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class AdditionalImage class AdditionalImage
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Instructions class Instructions
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Review class Review
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Set class Set
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Subtheme class Subtheme
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Theme class Theme
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Brickset\Entity; namespace AppBundle\Api\Client\Brickset\Entity;
class Year class Year
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Rebrickable\Converter; namespace AppBundle\Api\Client\Rebrickable\Converter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Rebrickable\Entity; namespace AppBundle\Api\Client\Rebrickable\Entity;
class Color class Color
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Rebrickable\Entity; namespace AppBundle\Api\Client\Rebrickable\Entity;
class Part class Part
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace AppBundle\Client\Rebrickable\Entity; namespace AppBundle\Api\Client\Rebrickable\Entity;
class Set class Set
{ {

View File

@ -1,11 +1,11 @@
<?php <?php
namespace AppBundle\Client\Rebrickable; namespace AppBundle\Api\Client\Rebrickable;
use AppBundle\Client\Rebrickable\Entity\Color; use AppBundle\Api\Client\Rebrickable\Entity\Color;
use AppBundle\Client\Rebrickable\Entity\Part; use AppBundle\Api\Client\Rebrickable\Entity\Part;
use AppBundle\Client\Rebrickable\Entity\Set; use AppBundle\Api\Client\Rebrickable\Entity\Set;
use AppBundle\Client\Rebrickable\Converter\PartPropertyNameConverter; use AppBundle\Api\Client\Rebrickable\Converter\PartPropertyNameConverter;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;

View File

@ -0,0 +1,43 @@
<?php
namespace AppBundle\Api\Manager;
use AppBundle\Api\Client\Brickset\Brickset;
class BricksetManager
{
/**
* @var Brickset
*/
private $bricksetClient;
/**
* BricksetManager constructor.
*
* @param Brickset $bricksetClient
*/
public function __construct(Brickset $bricksetClient)
{
$this->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);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace AppBundle\Api\Manager;
use AppBundle\Api\Client\Rebrickable\Rebrickable;
class RebrickableManager
{
/**
* @var Rebrickable
*/
private $rebrickableClient;
/**
* RebrickableManager constructor.
*
* @param Rebrickable $rebrickableClient
*/
public function __construct(Rebrickable $rebrickableClient)
{
$this->rebrickableClient = $rebrickableClient;
}
public function getSetParts($setNumber)
{
return $this->rebrickableClient->getSetParts($setNumber);
}
public function getPartById($id)
{
return $this->rebrickableClient->getPart($id);
}
}

View File

@ -8,7 +8,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
* @Route("/sets") * @Route("/brickset/sets")
*/ */
class SetsController extends Controller class SetsController extends Controller
{ {
@ -43,12 +43,13 @@ class SetsController extends Controller
*/ */
public function detailAction(Request $request, $id, $name = null) 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', [ return $this->render('sets/detail.html.twig', [
'set' => $set, 'set' => $set,
'parts' => $parts,
]); ]);
} }
} }

View File

@ -2,10 +2,10 @@
namespace AppBundle\Form; namespace AppBundle\Form;
use AppBundle\Client\Brickset\Entity\Subtheme; use AppBundle\Api\Client\Brickset\Entity\Subtheme;
use AppBundle\Client\Brickset\Entity\Theme; use AppBundle\Api\Client\Brickset\Entity\Theme;
use AppBundle\Client\Brickset\Entity\Year; use AppBundle\Api\Client\Brickset\Entity\Year;
use AppBundle\Service\CollectionService; use AppBundle\Api\Manager\BricksetManager;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -16,18 +16,18 @@ use Symfony\Component\Form\FormEvents;
class FilterSetType extends AbstractType 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) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add("theme", ChoiceType::class, [ ->add("theme", ChoiceType::class, [
'choices' => $this->collectionService->getThemes(), 'choices' => $this->bricksetManager->getThemes(),
'choice_label' => function(Theme $theme = null) { 'choice_label' => function(Theme $theme = null) {
return $theme ? $theme->getTheme().' ('.$theme->getSetCount().')' : ''; return $theme ? $theme->getTheme().' ('.$theme->getSetCount().')' : '';
}, },
@ -37,8 +37,8 @@ class FilterSetType extends AbstractType
]); ]);
$formModifier = function (Form $form, Theme $theme = null) { $formModifier = function (Form $form, Theme $theme = null) {
$subthemes = null === $theme ? [] : $this->collectionService->getSubthemesByTheme($theme); $subthemes = null === $theme ? [] : $this->bricksetManager->getSubthemesByTheme($theme);
$years = null === $theme ? [] : $this->collectionService->getYearsByTheme($theme); $years = null === $theme ? [] : $this->bricksetManager->getYearsByTheme($theme);
$form->add("subtheme", ChoiceType::class, [ $form->add("subtheme", ChoiceType::class, [
'choices' => $subthemes, 'choices' => $subthemes,
@ -63,7 +63,7 @@ class FilterSetType extends AbstractType
FormEvents::PRE_SET_DATA, FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) { function (FormEvent $event) use ($formModifier) {
$data = $event->getData(); $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); $formModifier($event->getForm(), $theme);
} }
); );

View File

@ -2,60 +2,38 @@
namespace AppBundle\Service; namespace AppBundle\Service;
use AppBundle\Client\Brickset\Brickset; use AppBundle\Api\Client\Rebrickable\Rebrickable;
use AppBundle\Client\Rebrickable\Rebrickable; use AppBundle\Api\Manager\BricksetManager;
use Doctrine\ORM\EntityManager;
class CollectionService class CollectionService
{ {
/** /**
* @var Brickset client * @var BricksetManager
*/ */
protected $bricksetClient; protected $bricksetManager;
/** /**
* @var Rebrickable client * @var Rebrickable client
*/ */
protected $rebrickableClient; protected $rebrickableManager;
/**
* @var EntityManager
*/
private $em;
/** /**
* CollectionService constructor. * CollectionService constructor.
* *
* @param $bricksetClient * @param $em
* @param $rebrickableClient * @param $bricksetManager
* @param $rebrickableManager
*/ */
public function __construct($bricksetClient, $rebrickableClient) public function __construct($em, $bricksetManager, $rebrickableManager)
{ {
$this->bricksetClient = $bricksetClient; $this->em = $em;
$this->rebrickableClient = $rebrickableClient; $this->bricksetManager = $bricksetManager;
} $this->rebrickableManager = $rebrickableManager;
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);
} }
} }