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:
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 }

View File

@ -1,14 +1,14 @@
<?php
namespace AppBundle\Client\Brickset;
namespace AppBundle\Api\Client\Brickset;
use AppBundle\Client\Brickset\Entity\AdditionalImage;
use AppBundle\Client\Brickset\Entity\Instructions;
use AppBundle\Client\Brickset\Entity\Review;
use AppBundle\Client\Brickset\Entity\Set;
use AppBundle\Client\Brickset\Entity\Subtheme;
use AppBundle\Client\Brickset\Entity\Theme;
use AppBundle\Client\Brickset\Entity\Year;
use AppBundle\Api\Client\Brickset\Entity\AdditionalImage;
use AppBundle\Api\Client\Brickset\Entity\Instructions;
use AppBundle\Api\Client\Brickset\Entity\Review;
use AppBundle\Api\Client\Brickset\Entity\Set;
use AppBundle\Api\Client\Brickset\Entity\Subtheme;
use AppBundle\Api\Client\Brickset\Entity\Theme;
use AppBundle\Api\Client\Brickset\Entity\Year;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Debug\Exception\ContextErrorException;
@ -17,6 +17,7 @@ class Brickset extends \SoapClient
const WSDL = 'http://brickset.com/api/v2.asmx?WSDL';
private $apiKey = '';
private $userHash = '';
/**
@ -60,15 +61,18 @@ class Brickset extends \SoapClient
$this->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;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,11 @@
<?php
namespace AppBundle\Client\Rebrickable;
namespace AppBundle\Api\Client\Rebrickable;
use AppBundle\Client\Rebrickable\Entity\Color;
use AppBundle\Client\Rebrickable\Entity\Part;
use AppBundle\Client\Rebrickable\Entity\Set;
use AppBundle\Client\Rebrickable\Converter\PartPropertyNameConverter;
use AppBundle\Api\Client\Rebrickable\Entity\Color;
use AppBundle\Api\Client\Rebrickable\Entity\Part;
use AppBundle\Api\Client\Rebrickable\Entity\Set;
use AppBundle\Api\Client\Rebrickable\Converter\PartPropertyNameConverter;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
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;
/**
* @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,
]);
}
}

View File

@ -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);
}
);

View File

@ -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;
}
}