| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the vendor/project 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\Factory\Generator\ChangeStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Puli\Manager\Api\Factory\Generator\GeneratorRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Puli\Manager\Api\Factory\Generator\ServiceGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Puli\Manager\Api\Php\Import; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Puli\Manager\Api\Php\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Puli\Manager\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Generates the setup code for a {@link KeyValueStoreChangeStream}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 27 |  | View Code Duplication | class KeyValueStoreChangeStreamGenerator implements ServiceGenerator | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private static $defaultOptions = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         'store' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             'type' => 'json', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         ), | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 8 |  |     public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 8 |  |         Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 7 |  |         $options = array_replace_recursive(self::$defaultOptions, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 7 |  |         Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         Assert::isArray($options['store'], 'The "store" option should be an array. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |         if (!isset($options['store']['path'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 4 |  |             $options['store']['path'] = $targetMethod->getClass()->getDirectory().'/change-stream.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |         $kvsOptions = $options['store']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |         $kvsOptions['root-dir'] = $options['root-dir']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 5 |  |         $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 5 |  |         $targetMethod->getClass()->addImport(new Import('Puli\Repository\ChangeStream\KeyValueStoreChangeStream')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 5 |  |         $targetMethod->addBody(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |             '$%s = new KeyValueStoreChangeStream($store);', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $varName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 5 |  |         )); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 | 5 |  |     } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                        
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.