| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Victoire\Bundle\ViewReferenceBundle\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\PropertyAccess\PropertyAccessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Victoire\Bundle\BusinessPageBundle\Helper\BusinessPageHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Victoire\Bundle\CoreBundle\Entity\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Victoire\Bundle\CoreBundle\Entity\WebViewInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property BusinessPageHelper businessPageHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property BusinessPageBuilder businessPageBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * ref. victoire_view_reference.provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class ViewReferenceProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $businessPageHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $businessPageBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param BusinessPageHelper  $businessPageHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param BusinessPageBuilder $businessPageBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(BusinessPageHelper $businessPageHelper, BusinessPageBuilder $businessPageBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->businessPageHelper = $businessPageHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->businessPageBuilder = $businessPageBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param View[]        $views | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param EntityManager $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return WebViewInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function getReferencableViews($views, EntityManager $em) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $referencableViews = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (!$views instanceof \Traversable && !is_array($views)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $views = [$views]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $businessPages = $this->findBusinessPages($views); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         foreach ($views as $view) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             if ($view instanceof BusinessTemplate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $entities = $this->businessPageHelper->getEntitiesAllowed($view, $em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 // for each business entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 foreach ($entities as $k => $entity) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $currentTemplate = clone $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     $page = $this->businessPageBuilder->generateEntityPageFromTemplate($currentTemplate, $entity, $em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     $this->businessPageBuilder->updatePageParametersByEntity($page, $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     $entityId = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     if (method_exists($entity, 'getId')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         $entityId = $entity->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     } elseif ($page->getBusinessEntity()->getType() === APIBusinessEntity::TYPE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                         $accessor = new PropertyAccessor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                         $entityId = $accessor->getValue($entity, $page->getBusinessEntity()->getBusinessIdentifiers()->first()->getName()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     if (!array_key_exists(ViewReferenceHelper::generateViewReferenceId($page, $entityId), $businessPages)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         $referencableViews[] = ['view' => $page]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } elseif ($view instanceof WebViewInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $referencableView = ['view' => $view]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 if ($view->getChildren()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     $referencableView['children'] = $this->getReferencableViews($view->getChildren(), $em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $referencableViews[] = $referencableView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $referencableViews; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |     public function findBusinessPages($tree) | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         $businessPages = []; | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |         foreach ($tree as $view) { | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             if ($view instanceof BusinessPage) { | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                 $businessPages[ViewReferenceHelper::generateViewReferenceId($view, $view->getEntity())] = $view; | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             if ($view->hasChildren()) { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                 self::findBusinessPages($view->getChildren()); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         return $businessPages; | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                        
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.