| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Wambo\Catalog\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Wambo\Catalog\Error\ContentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Wambo\Catalog\Model\Content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class ContentMapper creates \Wambo\Model\Content models from data bags with product content data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Wambo\Mapper | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class ContentMapper | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const FIELD_TITLE = "title"; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const FIELD_SUMMARY = "summary"; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const FIELD_DESCRIPTION = "description"; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var array $mandatoryFields A list of all mandatory fields of a Content | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     private $mandatoryFields = [self::FIELD_TITLE, self::FIELD_SUMMARY]; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * Creates a new ContentMapper instance | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 12 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 | 12 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Get a Content model from an array of unstructured product content data | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param array $contentData An array containing content attributes | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return Content | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @throws ContentException If a mandatory field is missing | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @throws ContentException If the no Content could be created from the given product data | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |     public function getContent(array $contentData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         // check if all mandatory fields are present | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |         foreach ($this->mandatoryFields as $mandatoryField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |             if (!array_key_exists($mandatoryField, $contentData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |                 throw new ContentException("The field '$mandatoryField' is missing in the given content data"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         // try to create a content model from the available data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             // title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |             $title = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 5 |  |             if (isset($contentData[self::FIELD_TITLE])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 5 |  |                 $title = $contentData[self::FIELD_TITLE]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             // summary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |             $summary = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |             if (isset($contentData[self::FIELD_SUMMARY])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 5 |  |                 $summary = $contentData[self::FIELD_SUMMARY]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             // description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 5 |  |             $description = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 5 |  |             if (isset($contentData[self::FIELD_DESCRIPTION])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 4 |  |                 $description = $contentData[self::FIELD_DESCRIPTION]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 5 |  |             $content = new Content($title, $summary, $description); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |             return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         } catch (\Exception $contentException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             throw new ContentException(sprintf("Failed to create a content model from the given data: %s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |                 $contentException->getMessage()), $contentException); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  | } |