| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AppBundle\Tenant\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ds\Component\Config\Service\ConfigService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ds\Component\Tenant\Entity\Tenant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Ds\Component\Tenant\Loader\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class ConfigLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ConfigLoader implements Loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var \Ds\Component\Config\Service\ConfigService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param \Ds\Component\Config\Service\ConfigService $configService | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct(ConfigService $configService) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $this->configService = $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function load(Tenant $tenant) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/tenant/configs.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // @todo Figure out how symfony does parameter binding and use the same technique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $yml = strtr($yml, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             '%config.app.spa.admin.value%' => $tenant->getData()['config']['app.spa.admin']['value'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             '%config.app.spa.portal.value%' => $tenant->getData()['config']['app.spa.portal']['value'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             '%business_unit.administration.uuid%' => $tenant->getData()['business_unit']['administration']['uuid'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             '%tenant.uuid%' => $tenant->getUuid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $configs = Yaml::parse($yml, YAML::PARSE_OBJECT_FOR_MAP); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $manager = $this->configService->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         foreach ($configs->objects as $object) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $object = (object) array_merge((array) $configs->prototype, (array) $object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $config = $this->configService->createInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ->setOwner($object->owner) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ->setOwnerUuid($object->owner_uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 ->setKey($object->key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 ->setValue($object->value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 ->setEnabled($object->enabled) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 ->setTenant($object->tenant); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $manager->persist($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $manager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |