| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the sauls/object-registry-bundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author    Saulius Vaičeliūnas <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link      http://saulius.vaiceliunas.lt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2018 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Sauls\Bundle\ObjectRegistryBundle\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Exception\UnsupportedManagerClassException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Manager\ConcreteManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Manager\ManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Manager\NamedManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sauls\Component\Collection\ArrayCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class ObjectManagerCollection extends ArrayCollection implements ObjectManagerCollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public const LOOSE_MANAGERS_KEY = 'loose'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public const NAMED_MANAGERS_KEY = 'named'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public const CONCRETE_MANAGERS_KEY = 'concrete'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 4 |  |     public function set($key, $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |         $this->setConcreteManager($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |         $this->setNamedManager($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 4 |  |         $this->setLooseManager($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 4 |  |         $this->throwUnsupportedManagerClassException($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |     private function setConcreteManager(object $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |         if (\is_subclass_of($value, ConcreteManagerInterface::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |             parent::set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |                 $this->glueKeyNameWithValue(self::CONCRETE_MANAGERS_KEY, $value->getObjectClass()), $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 4 |  |     private function glueKeyNameWithValue(string $keyName, string $value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 4 |  |         return \sprintf('%s.%s', $keyName, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |     private function setNamedManager(object $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |         if (\is_subclass_of($value, NamedManagerInterface::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |             parent::set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |                 $this->glueKeyNameWithValue(self::NAMED_MANAGERS_KEY, $value->getName()), $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |     private function setLooseManager($key, object $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |         if (\is_subclass_of($value, ManagerInterface::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |             parent::set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |                 $this->glueKeyNameWithValue(self::LOOSE_MANAGERS_KEY, $key), $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |     private function throwUnsupportedManagerClassException(object $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |         if ($this->isNotSupportedObjectClass($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             throw new UnsupportedManagerClassException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                     'Given `%s` class should implement one of [%s] interfaces', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |                     \get_class($value), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |                     \implode(', ', $this->getSupportedManagerClasses()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |     private function isNotSupportedObjectClass(object $value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  |         return false === $this->isSupportedObjectClass($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  |     private function isSupportedObjectClass(object $value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  |         foreach ($this->getSupportedManagerClasses() as $supportedObjectClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |             if ($value instanceof $supportedObjectClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 4 |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 4 |  |     private function getSupportedManagerClasses(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |             ManagerInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ConcreteManagerInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             NamedManagerInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 2 |  |     public function get($key, $default = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 2 |  |         foreach ($this->getSupportedManagerGroups() as $managerGroup) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 2 |  |             $managerGroupKey = $this->glueKeyNameWithValue($managerGroup, $key); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 2 |  |             if ($this->keyExists($managerGroupKey)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 2 |  |                 return parent::get($managerGroupKey, $default); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 | 1 |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |     private function getSupportedManagerGroups(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |             self::CONCRETE_MANAGERS_KEY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 3 |  |             self::NAMED_MANAGERS_KEY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |             self::LOOSE_MANAGERS_KEY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 3 |  |     public function count(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 3 |  |         $total = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  |         foreach ($this->all() as $group => $managers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |             $total += \count($managers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 3 |  |         return $total; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 1 |  |     public function clear(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |         foreach ($this->getSupportedManagerGroups() as $group) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |             parent::set($group, []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  |