| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Wambo\Catalog\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Wambo\Catalog\Error\CatalogException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Wambo\Catalog\Model\Catalog; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class CatalogMapper creates \Wambo\Model\Product models from catalog data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Wambo\Mapper | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class CatalogMapper | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @var ProductMapper | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $productMapper; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * Creates a new instance of the CatalogMapper class. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param ProductMapper $productMapper A ProductMapper instance for converting unstructured product data to Product | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      *                                     models | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 16 |  |     public function __construct(ProductMapper $productMapper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 16 |  |         $this->productMapper = $productMapper; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 | 16 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Get a Catalog model from the an array of catalog data | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param array $catalogData An array containing a product catalog | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return Catalog | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @throws CatalogException If the catalog cannot be created | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 12 |  |     public function getCatalog(array $catalogData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         /** @var array $skuIndex A list of all SKUs */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 12 |  |         $skuIndex = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         /** @var array $slugIndex A list of all product slugs */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 12 |  |         $slugIndex = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 12 |  |         $index = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 12 |  |         $products = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 12 |  |         foreach ($catalogData as $catalogItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 // convert the product data into a Product model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 10 |  |                 $product = $this->productMapper->getProduct($catalogItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 // check for duplicate SKUs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 8 |  |                 $sku = strtolower($product->getSku()->__toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 8 |  |                 if (array_key_exists($sku, $skuIndex)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |                     throw new CatalogException(sprintf("Cannot add a second product with the SKU '%s' to the catalog", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         $sku)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |                 $skuIndex[$sku] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 // check for duplicate Slugs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 8 |  |                 $slug = strtolower($product->getSlug()->__toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 8 |  |                 if (array_key_exists($slug, $slugIndex)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                     throw new CatalogException(sprintf("Cannot add a second product with the Slug '%s' to the catalog", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         $slug)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 8 |  |                 $slugIndex[$slug] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 // add the product to the catalog | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 8 |  |                 $products[] = $product; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |             } catch (\Exception $productException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |                 throw new CatalogException(sprintf("Cannot convert catalog item %s into a product: %s", $index, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 6 |  |                     $productException->getMessage()), $productException); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 8 |  |             $index++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 6 |  |         return new Catalog($products); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } |