| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the puli/manager package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Puli\Manager\Package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Puli\Manager\Api\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Puli\Manager\Api\Package\InstallInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Puli\Manager\Api\Package\RootPackageFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Puli\Manager\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Rhumsaa\Uuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Converts root package files to JSON and back. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class RootPackageFileConverter extends PackageFileConverter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * The default order of the keys in the written package file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private static $keyOrder = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'version', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'path-mappings', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'bindings', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'binding-types', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         'override', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         'override-order', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'config', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         'plugins', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         'extra', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         'packages', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 | 6 | View Code Duplication |     public function toJson($packageFile, array $options = array()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |         Assert::isInstanceOf($packageFile, 'Puli\Manager\Api\Package\RootPackageFile'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         $jsonData = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |         $this->addPackageFileToJson($packageFile, $jsonData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |         $this->addRootPackageFileToJson($packageFile, $jsonData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // Sort according to key order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  |         $jsonArray = (array) $jsonData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |         $orderedKeys = array_intersect_key(array_flip(self::$keyOrder), $jsonArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |         return (object) array_replace($orderedKeys, $jsonArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 30 |  |     public function fromJson($jsonData, array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 30 |  |         $path = isset($options['path']) ? $options['path'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 30 |  |         $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 30 |  |         Assert::isInstanceOf($jsonData, 'stdClass'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 30 |  |         Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 30 |  |         Assert::nullOrIsInstanceOf($baseConfig, 'Puli\Manager\Api\Config\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 30 |  |         $packageFile = new RootPackageFile(null, $path, $baseConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 30 |  |         $this->addJsonToPackageFile($jsonData, $packageFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 30 |  |         $this->addJsonToRootPackageFile($jsonData, $packageFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 30 |  |         return $packageFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 6 |  |     protected function addRootPackageFileToJson(RootPackageFile $packageFile, stdClass $jsonData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 6 |  |         $overrideOrder = $packageFile->getOverrideOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 6 |  |         $installInfos = $packageFile->getInstallInfos(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // Pass false to exclude base configuration values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 6 |  |         $configValues = $packageFile->getConfig()->toRawArray(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 6 |  |         if (count($overrideOrder) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |             $jsonData->{'override-order'} = $overrideOrder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         if (count($configValues) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |             $jsonData->config = (object) $configValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 6 |  |         if (array() !== $packageFile->getPluginClasses()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |             $jsonData->plugins = $packageFile->getPluginClasses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |             sort($jsonData->plugins); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 6 |  |         if (count($installInfos) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |             $packagesData = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |             foreach ($installInfos as $installInfo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |                 $installData = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |                 $installData->{'install-path'} = $installInfo->getInstallPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |                 if (InstallInfo::DEFAULT_INSTALLER_NAME !== $installInfo->getInstallerName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |                     $installData->installer = $installInfo->getInstallerName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 3 |  |                 if ($installInfo->hasDisabledBindingUuids()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |                     $installData->{'disabled-bindings'} = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |                     foreach ($installInfo->getDisabledBindingUuids() as $uuid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |                         $installData->{'disabled-bindings'}[] = $uuid->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |                     sort($installData->{'disabled-bindings'}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 3 |  |                 if (Environment::PROD !== $installInfo->getEnvironment()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |                     $installData->env = $installInfo->getEnvironment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  |                 $packagesData[$installInfo->getPackageName()] = $installData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |             ksort($packagesData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 3 |  |             $jsonData->packages = (object) $packagesData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 30 |  |     protected function addJsonToRootPackageFile(stdClass $jsonData, RootPackageFile $packageFile) | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 30 |  |         if (isset($jsonData->{'override-order'})) { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 1 |  |             $packageFile->setOverrideOrder((array) $jsonData->{'override-order'}); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 30 |  |         if (isset($jsonData->plugins)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 27 |  |             $packageFile->setPluginClasses($jsonData->plugins); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 | 30 |  |         if (isset($jsonData->config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 2 |  |             $config = $packageFile->getConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 2 |  |             foreach ($this->objectsToArrays($jsonData->config) as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 2 |  |                 $config->set($key, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 30 |  |         if (isset($jsonData->packages)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 | 27 |  |             foreach ($jsonData->packages as $packageName => $packageData) { | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 27 |  |                 $installInfo = new InstallInfo($packageName, $packageData->{'install-path'}); | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 166 | 27 |  |                 if (isset($packageData->env)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 167 | 1 |  |                     $installInfo->setEnvironment($packageData->env); | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 27 |  |                 if (isset($packageData->installer)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 1 |  |                     $installInfo->setInstallerName($packageData->installer); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 174 | 27 |  |                 if (isset($packageData->{'disabled-bindings'})) { | 
            
                                                                        
                            
            
                                    
            
            
                | 175 | 1 |  |                     foreach ($packageData->{'disabled-bindings'} as $uuid) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 | 1 |  |                         $installInfo->addDisabledBindingUuid(Uuid::fromString($uuid)); | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 180 | 27 |  |                 $packageFile->addInstallInfo($installInfo); | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 183 | 30 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 185 | 2 | View Code Duplication |     private function objectsToArrays($data) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 2 |  |         $data = (array) $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 2 |  |         foreach ($data as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 2 |  |             $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 2 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 195 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 196 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.