From a7b68eb8d21e1f22ec2fcc960c305960f99e455d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Thu, 22 Dec 2016 15:08:56 +0100 Subject: [PATCH] Split Rebrickable to client and manager --- .../Api/Client/Rebrickable/Rebrickable.php | 154 +----------------- .../Api/Manager/RebrickableManager.php | 142 +++++++++++++++- 2 files changed, 147 insertions(+), 149 deletions(-) diff --git a/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php b/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php index 30c0828..f802e2c 100644 --- a/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php +++ b/src/AppBundle/Api/Client/Rebrickable/Rebrickable.php @@ -2,28 +2,26 @@ namespace AppBundle\Api\Client\Rebrickable; -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; use Symfony\Component\Asset\Exception\LogicException; -use Symfony\Component\Serializer\Encoder\JsonEncoder; -use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; -use Symfony\Component\Serializer\Serializer; class Rebrickable { const BASE_URI = 'https://rebrickable.com/api/'; const FORMAT = 'json'; - + + /** + * @var Client + */ private $client; + /** + * @var string + */ private $apiKey; - + /** * RebrickableAPI constructor. */ @@ -39,7 +37,7 @@ class Rebrickable * * @return null|string */ - private function call($method, $parameters = []) + public function call($method, $parameters = []) { $parameters['query']['key'] = $this->apiKey; $parameters['query']['format'] = self::FORMAT; @@ -69,138 +67,4 @@ class Rebrickable throw new LogicException($e->getMessage()); } } - - private function getSerializer() - { - $encoders = array(new JsonEncoder()); - $nameConverter = new PartPropertyNameConverter(); - $objectNormalizer = new ObjectNormalizer(null, $nameConverter); - $normalizers = array($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->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 - * - * @return Part|null - */ - public function getPart($partID) - { - $parameters = [ - 'query' => [ - 'part_id' => $partID, - 'inc_ext' => 1, - ], - ]; - - $data = $this->call('get_part', $parameters); - $serializer = $this->getSerializer(); - - return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null; - } - - /** - * Get associative array of colors used by all parts where key == rb_color_id. - * - * @return Color[]|null - */ - public function getColors() - { - $data = json_decode($this->call('get_colors'), true); - - $serializer = $this->getSerializer(); - - $colors = []; - - foreach ($data as $item) { - $color = $serializer->denormalize($item, Color::class, self::FORMAT); - $colors[$color->getRbColorId()] = $color; - } - - return $data ? $colors : null; - } - - /** - * Get associative array of themes used by all parts where key == part_type_id. - * - * @return string[] - */ - public function getPartTypes() - { - $data = json_decode($this->call('get_part_types'), true)['part_types']; - - $types = []; - foreach ($data as $item) { - $types[$item['part_type_id']] = $item['desc']; - } - - return $data ? $types : null; - } - - /** - * Get the list of sets that a specific part/color appears in. - * - * @param $partID - * @param $colorID - * - * @return Set[] - */ - public function getPartSets($partID, $colorID) - { - $parameters = [ - 'query' => [ - 'part_id' => $partID, - 'color_id' => $colorID, - ], - ]; - - $serializer = $this->getSerializer(); - - $data = json_decode($this->call('get_part_sets', $parameters), true)[0]['sets']; - - return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null; - } } diff --git a/src/AppBundle/Api/Manager/RebrickableManager.php b/src/AppBundle/Api/Manager/RebrickableManager.php index df94f08..11368a7 100644 --- a/src/AppBundle/Api/Manager/RebrickableManager.php +++ b/src/AppBundle/Api/Manager/RebrickableManager.php @@ -3,9 +3,19 @@ namespace AppBundle\Api\Manager; use AppBundle\Api\Client\Rebrickable\Rebrickable; +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 Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\Serializer; class RebrickableManager { + const FORMAT = 'json'; + /** * @var Rebrickable */ @@ -21,13 +31,137 @@ class RebrickableManager $this->rebrickableClient = $rebrickableClient; } - public function getSetParts($setNumber) + private function getSerializer() { - return $this->rebrickableClient->getSetParts($setNumber); + $encoders = array(new JsonEncoder()); + $nameConverter = new PartPropertyNameConverter(); + $objectNormalizer = new ObjectNormalizer(null, $nameConverter); + $normalizers = array($objectNormalizer, new ArrayDenormalizer()); + $serializer = new Serializer($normalizers, $encoders); + + return $serializer; } - public function getPartById($id) + /** + * 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) { - return $this->rebrickableClient->getPart($id); + $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 + * + * @return Part|null + */ + public function getPart($partID) + { + $parameters = [ + 'query' => [ + 'part_id' => $partID, + 'inc_ext' => 1, + ], + ]; + + $data = $this->rebrickableClient->call('get_part', $parameters); + $serializer = $this->getSerializer(); + + return $data ? $serializer->deserialize($data, Part::class, self::FORMAT) : null; + } + + /** + * Get associative array of colors used by all parts where key == rb_color_id. + * + * @return Color[]|null + */ + public function getColors() + { + $data = json_decode($this->rebrickableClient->call('get_colors'), true); + + $serializer = $this->getSerializer(); + + $colors = []; + + foreach ($data as $item) { + $color = $serializer->denormalize($item, Color::class, self::FORMAT); + $colors[$color->getRbColorId()] = $color; + } + + return $data ? $colors : []; + } + + /** + * Get associative array of themes used by all parts where key == part_type_id. + * + * @return string[] + */ + public function getPartTypes() + { + $data = json_decode($this->rebrickableClient->call('get_part_types'), true)['part_types']; + + $types = []; + foreach ($data as $item) { + $types[$item['part_type_id']] = $item['desc']; + } + + return $data ? $types : null; + } + + /** + * Get the list of sets that a specific part/color appears in. + * + * @param $partID + * @param $colorID + * + * @return Set[] + */ + public function getPartSets($partID, $colorID) + { + $parameters = [ + 'query' => [ + 'part_id' => $partID, + 'color_id' => $colorID, + ], + ]; + + $serializer = $this->getSerializer(); + + $data = json_decode($this->rebrickableClient->call('get_part_sets', $parameters), true)[0]['sets']; + + return $data ? $serializer->denormalize($data, Set::class.'[]', self::FORMAT) : null; } }