| 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 |  |  | use Webmozart\PathUtil\Path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Generates the setup code for a {@link JsonChangeStream}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 28 |  | View Code Duplication | class JsonChangeStreamGenerator implements ServiceGenerator | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 7 |  |     public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |         Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |         if (!isset($options['path'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |             $options['path'] = $targetMethod->getClass()->getDirectory().'/change-stream.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 6 |  |         Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 6 |  |         Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |         $path = Path::makeAbsolute($options['path'], $options['root-dir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |         $escPath = '__DIR__.'.var_export('/'.$relPath, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         $targetMethod->getClass()->addImport(new Import('Puli\Repository\ChangeStream\JsonChangeStream')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |         $targetMethod->addBody(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |             '$%s = new JsonChangeStream(%s);', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |             $varName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $escPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |         )); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 | 6 |  |     } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                        
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.