From 83992cdcdd8f2d07081f91f3fae429d2bc94fd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Mon, 6 Feb 2017 01:33:46 +0100 Subject: [PATCH] migrate rebrickable api to v3 --- app/Resources/views/part/detail.html.twig | 2 +- app/config/services.yml | 2 +- .../Converter/PartPropertyNameConverter.php | 23 -- .../Converter/PropertyNameConverter.php | 27 ++ .../Api/Client/Rebrickable/Entity/Color.php | 211 +++------------- .../Api/Client/Rebrickable/Entity/Part.php | 237 +++++++----------- .../Rebrickable/Entity/PartCategory.php | 69 +++++ .../Api/Client/Rebrickable/Entity/Set.php | 119 +++++++-- .../Api/Client/Rebrickable/Entity/Theme.php | 75 ++++++ .../Api/Client/Rebrickable/Rebrickable.php | 69 ----- .../Api/Client/Rebrickable/Rebrickable_v3.php | 67 +++++ .../Api/Manager/RebrickableManager.php | 184 +++++++------- 12 files changed, 560 insertions(+), 525 deletions(-) delete mode 100644 src/AppBundle/Api/Client/Rebrickable/Converter/PartPropertyNameConverter.php create mode 100644 src/AppBundle/Api/Client/Rebrickable/Converter/PropertyNameConverter.php create mode 100644 src/AppBundle/Api/Client/Rebrickable/Entity/PartCategory.php create mode 100644 src/AppBundle/Api/Client/Rebrickable/Entity/Theme.php delete mode 100644 src/AppBundle/Api/Client/Rebrickable/Rebrickable.php create mode 100644 src/AppBundle/Api/Client/Rebrickable/Rebrickable_v3.php diff --git a/app/Resources/views/part/detail.html.twig b/app/Resources/views/part/detail.html.twig index b6461a2..c3a5f29 100644 --- a/app/Resources/views/part/detail.html.twig +++ b/app/Resources/views/part/detail.html.twig @@ -7,7 +7,7 @@ {{ dump(localPart) }}
- + {% endblock %} diff --git a/app/config/services.yml b/app/config/services.yml index 5814f07..21eb13b 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -3,7 +3,7 @@ services: class: AppBundle\Api\Client\Brickset\Brickset arguments: ['%brickset_apikey%'] client.rebrickable: - class: AppBundle\Api\Client\Rebrickable\Rebrickable + class: AppBundle\Api\Client\Rebrickable\Rebrickable_v3 arguments: ['%rebrickable_apikey%'] manager.brickset: diff --git a/src/AppBundle/Api/Client/Rebrickable/Converter/PartPropertyNameConverter.php b/src/AppBundle/Api/Client/Rebrickable/Converter/PartPropertyNameConverter.php deleted file mode 100644 index 7b79762..0000000 --- a/src/AppBundle/Api/Client/Rebrickable/Converter/PartPropertyNameConverter.php +++ /dev/null @@ -1,23 +0,0 @@ -rb_color_id; + return $this->id; } /** - * @param int $rb_color_id + * @param int $id */ - public function setRbColorId($rb_color_id) + public function setId($id) { - $this->rb_color_id = $rb_color_id; - } - - /** - * @return array - */ - public function getLdrawColorId() - { - return $this->ldraw_color_id; - } - - /** - * @param array $ldraw_color_id - */ - public function setLdrawColorId($ldraw_color_id) - { - $this->ldraw_color_id = $ldraw_color_id; - } - - /** - * @return array - */ - public function getBricklinkColorId() - { - return $this->bricklink_color_id; - } - - /** - * @param array $bricklink_color_id - */ - public function setBricklinkColorId($bricklink_color_id) - { - $this->bricklink_color_id = $bricklink_color_id; - } - - /** - * @return array - */ - public function getBrickowlColorId() - { - return $this->brickowl_color_id; - } - - /** - * @param array $brickowl_color_id - */ - public function setBrickowlColorId($brickowl_color_id) - { - $this->brickowl_color_id = $brickowl_color_id; + $this->id = $id; } /** * @return string */ - public function getColorName() + public function getName() { - return $this->color_name; + return $this->name; } /** - * @param string $color_name + * @param string $name */ - public function setColorName($color_name) + public function setName($name) { - $this->color_name = $color_name; - } - - /** - * @return int - */ - public function getNumParts() - { - return $this->num_parts; - } - - /** - * @param int $num_parts - */ - public function setNumParts($num_parts) - { - $this->num_parts = $num_parts; - } - - /** - * @return int - */ - public function getNumSets() - { - return $this->num_sets; - } - - /** - * @param int $num_sets - */ - public function setNumSets($num_sets) - { - $this->num_sets = $num_sets; - } - - /** - * @return int - */ - public function getYear1() - { - return $this->year1; - } - - /** - * @param int $year1 - */ - public function setYear1($year1) - { - $this->year1 = $year1; - } - - /** - * @return int - */ - public function getYear2() - { - return $this->year2; - } - - /** - * @param int $year2 - */ - public function setYear2($year2) - { - $this->year2 = $year2; + $this->name = $name; } /** @@ -225,10 +70,26 @@ class Color } /** - * @param string $rgbl + * @param string $rgb */ public function setRgb($rgb) { $this->rgb = $rgb; } + + /** + * @return bool + */ + public function isIsTrans() + { + return $this->isTrans; + } + + /** + * @param bool $isTrans + */ + public function setIsTrans($isTrans) + { + $this->isTrans = $isTrans; + } } diff --git a/src/AppBundle/Api/Client/Rebrickable/Entity/Part.php b/src/AppBundle/Api/Client/Rebrickable/Entity/Part.php index 000446b..103e624 100644 --- a/src/AppBundle/Api/Client/Rebrickable/Entity/Part.php +++ b/src/AppBundle/Api/Client/Rebrickable/Entity/Part.php @@ -5,17 +5,11 @@ namespace AppBundle\Api\Client\Rebrickable\Entity; class Part { /** - * Part ID. + * Part ID number. * * @var int */ - private $id; - /** - * Quantity of part in set returned from getSetParts. - * - * @var int - */ - private $qty; + private $number; /** * Part Name. * @@ -23,72 +17,89 @@ class Part */ private $name; /** - * Part type 1 = normal part, 2 = spare part. + * Part category id. * * @var int */ - private $type; + private $categoryId; + /** * Year the part first appeared in sets. * * @var int */ - private $year1; + private $yearFrom; /** * Year the part was last seen in sets. * * @var int */ - private $year2; - /** - * Part category/type description. - * - * @var string - */ - private $category; + private $yearTo; /** * Array of colors the part appears in. * * @var array */ - private $colors; - /** - * Array of related Part IDs used by external systems. - * - * @var array - */ - private $external_part_ids; + private $externalIds; /** * Rebrickable URL to the Part Details page. * * @var string */ - private $part_url; + private $url; /** * Rebrickable URL of the main part image (tries to use most common color). * * @var string */ - private $part_img_url; + private $imgUrl; - public function __construct() + /** + * @return int + */ + public function getPartNum() { + return $this->partNum; + } + + /** + * @param int $partNum + */ + public function setPartNum($partNum) + { + $this->partNum = $partNum; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; } /** * @return int */ - public function getId() + public function getCategoryId() { - return $this->id; + return $this->categoryId; } /** - * @param $part_id + * @param int $categoryId */ - public function setId($id) + public function setCategoryId($categoryId) { - $this->id = $id; + $this->categoryId = $categoryId; } /** @@ -110,144 +121,80 @@ class Part /** * @return int */ - public function getQty() + public function getYearFrom() { - return $this->qty; + return $this->yearFrom; } /** - * @param $qty + * @param int $yearFrom */ - public function setQty($qty) + public function setYearFrom($yearFrom) { - $this->qty = $qty; + $this->yearFrom = $yearFrom; + } + + /** + * @return int + */ + public function getYearTo() + { + return $this->yearTo; + } + + /** + * @param int $yearTo + */ + public function setYearTo($yearTo) + { + $this->yearTo = $yearTo; + } + + /** + * @return array + */ + public function getExternalIds() + { + return $this->externalIds; + } + + /** + * @param array $externalIds + */ + public function setExternalIds($externalIds) + { + $this->externalIds = $externalIds; } /** * @return string */ - public function getName() + public function getUrl() { - return $this->name; + return $this->url; } /** - * @param mixed $name + * @param string $url */ - public function setName($name) + public function setUrl($url) { - $this->name = $name; + $this->url = $url; } /** - * @return mixed + * @return string */ - public function getYear1() + public function getImgUrl() { - return $this->year1; + return $this->imgUrl; } /** - * @param mixed $year1 + * @param string $imgUrl */ - public function setYear1($year1) + public function setImgUrl($imgUrl) { - $this->year1 = $year1; - } - - /** - * @return mixed - */ - public function getYear2() - { - return $this->year2; - } - - /** - * @param mixed $year2 - */ - public function setYear2($year2) - { - $this->year2 = $year2; - } - - /** - * @return mixed - */ - public function getCategory() - { - return $this->category; - } - - /** - * @param mixed $category - */ - public function setCategory($category) - { - $this->category = $category; - } - - /** - * @return mixed - */ - public function getColors() - { - return $this->colors; - } - - /** - * @param mixed $colors - */ - public function setColors($colors) - { - $this->colors = $colors; - } - - /** - * @return mixed - */ - public function getExternalPartIds() - { - return $this->external_part_ids; - } - - /** - * @param mixed $external_part_ids - */ - public function setExternalPartIds($external_part_ids) - { - $this->external_part_ids = $external_part_ids; - } - - /** - * @return mixed - */ - public function getPartUrl() - { - return $this->part_url; - } - - /** - * @param mixed $part_url - */ - public function setPartUrl($part_url) - { - $this->part_url = $part_url; - } - - /** - * @return mixed - */ - public function getPartImgUrl() - { - return $this->part_img_url; - } - - /** - * @param $part_img_url - */ - public function setPartImgUrl($part_img_url) - { - $this->part_img_url = $part_img_url; + $this->imgUrl = $imgUrl; } } diff --git a/src/AppBundle/Api/Client/Rebrickable/Entity/PartCategory.php b/src/AppBundle/Api/Client/Rebrickable/Entity/PartCategory.php new file mode 100644 index 0000000..269b394 --- /dev/null +++ b/src/AppBundle/Api/Client/Rebrickable/Entity/PartCategory.php @@ -0,0 +1,69 @@ +id; + } + + /** + * @param int $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return int + */ + public function getPartCount() + { + return $this->partCount; + } + + /** + * @param int $partCount + */ + public function setPartCount($partCount) + { + $this->partCount = $partCount; + } +} diff --git a/src/AppBundle/Api/Client/Rebrickable/Entity/Set.php b/src/AppBundle/Api/Client/Rebrickable/Entity/Set.php index 0c71222..a02891b 100644 --- a/src/AppBundle/Api/Client/Rebrickable/Entity/Set.php +++ b/src/AppBundle/Api/Client/Rebrickable/Entity/Set.php @@ -9,41 +9,58 @@ class Set * * @var int */ - protected $set_id; + private $number; /** * Set description. * * @var string */ - protected $name; + private $name; /** - * Number of sets the part appears in. + * Year of set release. * * @var int */ - protected $num_parts; - + private $year; /** - * Set constructor. + * Count of parts in set. + * + * @var int */ - public function __construct() - { - } + private $numParts; + /** + * Rebrickable internal theme id. + * + * @var int + */ + private $themeId; + /** + * Rebrickable URL of the main set image. + * + * @var string + */ + private $imgUrl; + /** + * Rebrickable URL to the Set Details page. + * + * @var string + */ + private $url; /** * @return int */ - public function getSetId() + public function getNumber() { - return $this->set_id; + return $this->number; } /** - * @param int $set_id + * @param int $number */ - public function setSetId($set_id) + public function setNumber($number) { - $this->set_id = $set_id; + $this->number = $number; } /** @@ -65,16 +82,80 @@ class Set /** * @return int */ - public function getNumParts() + public function getYear() { - return $this->num_parts; + return $this->year; } /** - * @param int $num_parts + * @param int $year */ - public function setNumParts($num_parts) + public function setYear($year) { - $this->num_parts = $num_parts; + $this->year = $year; + } + + /** + * @return int + */ + public function getNumParts() + { + return $this->numParts; + } + + /** + * @param int $numParts + */ + public function setNumParts($numParts) + { + $this->numParts = $numParts; + } + + /** + * @return int + */ + public function getThemeId() + { + return $this->themeId; + } + + /** + * @param int $themeId + */ + public function setThemeId($themeId) + { + $this->themeId = $themeId; + } + + /** + * @return string + */ + public function getImgUrl() + { + return $this->imgUrl; + } + + /** + * @param string $imgUrl + */ + public function setImgUrl($imgUrl) + { + $this->imgUrl = $imgUrl; + } + + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @param string $url + */ + public function setUrl($url) + { + $this->url = $url; } } diff --git a/src/AppBundle/Api/Client/Rebrickable/Entity/Theme.php b/src/AppBundle/Api/Client/Rebrickable/Entity/Theme.php new file mode 100644 index 0000000..5bb9088 --- /dev/null +++ b/src/AppBundle/Api/Client/Rebrickable/Entity/Theme.php @@ -0,0 +1,75 @@ +id; + } + + /** + * @param int $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return int + */ + public function getParentId() + { + return $this->parentId; + } + + /** + * @param int $parentId + */ + public function setParentId($parentId) + { + $this->parentId = $parentId; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } +} diff --git a/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php b/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php deleted file mode 100644 index 03b412e..0000000 --- a/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php +++ /dev/null @@ -1,69 +0,0 @@ -client = new Client(['base_uri' => self::BASE_URI]); - $this->apiKey = $apiKey; - } - - /** - * @param $method - * @param $parameters - * - * @return null|string - */ - public function call($method, $parameters = []) - { - $parameters['query']['key'] = $this->apiKey; - $parameters['query']['format'] = self::FORMAT; - - try { - $response = $this->client->request('GET', $method, $parameters); - - if ($response->getStatusCode() === 200) { - $content = $response->getBody()->getContents(); - if ($content === 'INVALIDKEY') { - //TODO - throw new LogicException('Invalid API Key'); - } elseif ($content === 'NOSET' || $content === 'NOPART') { - return null; - } - - return $content; - } - //TODO - throw new LogicException($response->getStatusCode()); - } catch (ConnectException $e) { - //TODO - throw new LogicException($e->getMessage()); - } catch (ClientException $e) { - //TODO - throw new LogicException($e->getMessage()); - } - } -} diff --git a/src/AppBundle/Api/Client/Rebrickable/Rebrickable_v3.php b/src/AppBundle/Api/Client/Rebrickable/Rebrickable_v3.php new file mode 100644 index 0000000..d3e5eaf --- /dev/null +++ b/src/AppBundle/Api/Client/Rebrickable/Rebrickable_v3.php @@ -0,0 +1,67 @@ +client = new Client(['base_uri' => self::BASE_URI]); + $this->apiKey = $apiKey; + } + + /** + * @param $method + * @param $uri + * @param array $options + * + * @return string + */ + public function call($method, $uri, $options = []) + { + $options['query']['key'] = $this->apiKey; + $options['headers'] = [ + 'Accept' => 'application/json', + ]; + + try { + $response = $this->client->request($method, $uri, $options); + + $content = $response->getBody()->getContents(); + + return $content; + } catch (ConnectException $e) { + //TODO + throw new LogicException($e); + } catch (ClientException $e) { + //TODO + if ($e->getCode() == 404) { + throw new LogicException('Not Found'); + } + if ($e->getCode() == 500) { + throw new LogicException('Invalid token'); + } + throw new LogicException($e); + } + } +} diff --git a/src/AppBundle/Api/Manager/RebrickableManager.php b/src/AppBundle/Api/Manager/RebrickableManager.php index 40ef01e..d8997e8 100644 --- a/src/AppBundle/Api/Manager/RebrickableManager.php +++ b/src/AppBundle/Api/Manager/RebrickableManager.php @@ -2,11 +2,13 @@ namespace AppBundle\Api\Manager; -use AppBundle\Api\Client\Rebrickable\Converter\PartPropertyNameConverter; +use AppBundle\Api\Client\Rebrickable\Converter\PropertyNameConverter; use AppBundle\Api\Client\Rebrickable\Entity\Color; use AppBundle\Api\Client\Rebrickable\Entity\Part; +use AppBundle\Api\Client\Rebrickable\Entity\PartCategory; use AppBundle\Api\Client\Rebrickable\Entity\Set; -use AppBundle\Api\Client\Rebrickable\Rebrickable; +use AppBundle\Api\Client\Rebrickable\Entity\Theme; +use AppBundle\Api\Client\Rebrickable\Rebrickable_v3; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; @@ -17,151 +19,149 @@ class RebrickableManager const FORMAT = 'json'; /** - * @var Rebrickable + * @var Rebrickable_v3 */ private $rebrickableClient; + /** + * @var Serializer + */ + private $serializer; + /** * RebrickableManager constructor. * - * @param Rebrickable $rebrickableClient + * @param Rebrickable_v3 $rebrickableClient */ - public function __construct(Rebrickable $rebrickableClient) + public function __construct(Rebrickable_v3 $rebrickableClient) { $this->rebrickableClient = $rebrickableClient; + $this->serializer = $this->initSerializer(); } - private function getSerializer() + private function initSerializer() { $encoders = [new JsonEncoder()]; - $nameConverter = new PartPropertyNameConverter(); - $objectNormalizer = new ObjectNormalizer(null, $nameConverter); + $objectNormalizer = new ObjectNormalizer(null, new PropertyNameConverter()); $normalizers = [$objectNormalizer, new ArrayDenormalizer()]; $serializer = new Serializer($normalizers, $encoders); return $serializer; } - /** - * Get a list of all parts (normal + spare) used in a set. - * - * @param string $setName unique rebrickable set name - * - * @return Part[]|null - */ - public function getSetParts($setName) - { - $parameters = [ - 'query' => [ - 'set' => $setName, - ], - ]; - - $data = $this->rebrickableClient->call('get_set_parts', $parameters); - - $serializer = $this->getSerializer(); - $partsSTD = json_decode($data, true)[0]['parts']; - - if ($data) { - $parts = $serializer->denormalize($partsSTD, Part::class.'[]', self::FORMAT); - foreach ($parts as $key => &$part) { - $part->setCategory($this->getPartTypes()[$partsSTD[$key]['part_type_id']]); - $part->setColors([ - 0 => [ - 'color_name' => $partsSTD[$key]['color_name'], - 'rb_color_id' => $partsSTD[$key]['rb_color_id'], - 'ldraw_color_id' => $partsSTD[$key]['ldraw_color_id'], - ], - ]); - } - - return $data; - } - - return null; - } - /** * Get details about a specific part. * - * @param $partID + * @param $id * - * @return Part|null + * @return Part */ - public function getPart($partID) + public function getPart($id) { - $parameters = [ - 'query' => [ - 'part_id' => $partID, - 'inc_ext' => 1, - ], - ]; + $data = $this->rebrickableClient->call('GET', 'lego/parts/'.$id); - $data = $this->rebrickableClient->call('get_part', $parameters); - $serializer = $this->getSerializer(); - - return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null; + return $this->serializer->deserialize($data, Part::class, self::FORMAT); } /** - * Get associative array of colors used by all parts where key == rb_color_id. + * Get details about a specific Color. * - * @return Color[]|null + * @param $id + * + * @return Color */ - public function getColors() + public function getColor($id) { - $data = json_decode($this->rebrickableClient->call('get_colors'), true); + $data = $this->rebrickableClient->call('GET', 'lego/colors/'.$id); - $serializer = $this->getSerializer(); - - $colors = []; - - foreach ($data as $item) { - $color = $serializer->denormalize($item, Color::class, self::FORMAT); - $colors[$color->getRbColorId()] = $color; - } - - return $data ? $colors : []; + return $this->serializer->deserialize($data, Color::class, self::FORMAT); } /** - * Get associative array of themes used by all parts where key == part_type_id. + * Get details for a specific Set. * - * @return string[] + * @param $id + * + * @return Set */ - public function getPartTypes() + public function getSet($id) { - $data = json_decode($this->rebrickableClient->call('get_part_types'), true)['part_types']; + $data = $this->rebrickableClient->call('GET', 'lego/sets/'.$id); - $types = []; - foreach ($data as $item) { - $types[$item['part_type_id']] = $item['desc']; - } + return $this->serializer->deserialize($data, Set::class, self::FORMAT); + } - return $data ? $types : null; + /** + * Return details for a specific Theme. + * + * @param $id + * + * @return Theme + */ + public function getTheme($id) + { + $data = $this->rebrickableClient->call('GET', 'lego/themes/'.$id); + + return $this->serializer->deserialize($data, Theme::class, self::FORMAT); + } + + /** + * Return details for a specific PartCategory. + * + * @param $id + * + * @return PartCategory + */ + public function getPartCategory($id) + { + $data = $this->rebrickableClient->call('GET', 'lego/part_categories/'.$id); + + return $this->serializer->deserialize($data, PartCategory::class, self::FORMAT); } /** * Get the list of sets that a specific part/color appears in. * - * @param $partID - * @param $colorID + * @param $partId + * @param $colorId + * @param $page * * @return Set[] */ - public function getPartSets($partID, $colorID) + public function getPartSets($partId, $colorId, $page = null) { - $parameters = [ + $options = [ 'query' => [ - 'part_id' => $partID, - 'color_id' => $colorID, + 'page' => $page, ], ]; - $serializer = $this->getSerializer(); + $response = $this->rebrickableClient->call('GET', 'lego/parts/'.$partId.'/colors/'.$colorId.'/sets', $options); + $data = json_decode($response, true)['results']; - $data = json_decode($this->rebrickableClient->call('get_part_sets', $parameters), true)[0]['sets']; + return $this->serializer->denormalize($data, Set::class.'[]', self::FORMAT); + } - return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null; + /** + * Get a list of all parts (normal + spare) used in a set. + * + * @param $setId + * @param $page + * + * @return + */ + public function getSetParts($setId, $page = null) + { + $options = [ + 'query' => [ + 'page' => $page, + ], + ]; +// +// $response = $this->rebrickableClient->call('GET','lego/sets/'.$setId.'/parts', $options); +// +// $data = json_decode($response, true)['results']; +// +// return $this->serializer->denormalize($data, Part::class . '[]', self::FORMAT); } }