| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sylius\ShopApiPlugin\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sylius\Component\Core\Model\ChannelInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sylius\Component\Core\Model\ProductImageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Sylius\Component\Core\Model\ProductInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sylius\Component\Core\Model\TaxonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Sylius\ShopApiPlugin\View\ProductView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sylius\ShopApiPlugin\View\TaxonView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | final class ProductViewFactory implements ProductViewFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var ImageViewFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $imageViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var ProductAttributeValuesViewFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $attributeValuesViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var TaxonViewFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $taxonViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $fallback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param ImageViewFactoryInterface $imageViewFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param TaxonViewFactoryInterface $taxonViewFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $fallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ImageViewFactoryInterface $imageViewFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         TaxonViewFactoryInterface $taxonViewFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $fallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->imageViewFactory = $imageViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->attributeValuesViewFactory = $attributeValuesViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->taxonViewFactory = $taxonViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->fallback = $fallback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function create(ProductInterface $product, ChannelInterface $channel, $locale) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $productView = new ProductView(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $productView->name = $product->getTranslation($locale)->getName(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $productView->code = $product->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $productView->slug = $product->getTranslation($locale)->getSlug(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         /** @var ProductImageInterface $image */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($product->getImages() as $image) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $imageView = $this->imageViewFactory->create($image); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $productView->images[] = $imageView; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         /** @var TaxonInterface $taxon */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         foreach ($product->getTaxons() as $taxon) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $productView->taxons[] = $this->getTaxonWithAncestors($taxon, $locale); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $productView->attributes = $this->attributeValuesViewFactory->create($product->getAttributesByLocale($locale, $this->fallback)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $productView; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param TaxonInterface $taxon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return TaxonView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     private function getTaxonWithAncestors(TaxonInterface $taxon, $locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $currentTaxonView = $this->taxonViewFactory->create($taxon, $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if (null === $taxon->getParent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return $currentTaxonView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $taxonView = $this->getTaxonWithAncestors($taxon->getParent(), $locale); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $taxonView->children[] = $currentTaxonView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $taxonView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: