| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace SilverStripe\AssetAdmin\GraphQL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use GraphQL\Type\Definition\ResolveInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Assets\Folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\GraphQL\MutationCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\GraphQL\OperationResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\ORM\Versioning\Versioned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\GraphQL\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 12 |  | View Code Duplication | class CreateFolderMutationCreator extends MutationCreator implements OperationResolver | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var CaseInsensitiveFieldAccessor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $accessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct(Manager $manager = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->accessor = new CaseInsensitiveFieldAccessor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         parent::__construct($manager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function attributes() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             'name' => 'createFolder', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function type() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return function () { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             return $this->manager->getType('FileInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function args() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'folder' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'type' => function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     return $this->manager->getType('FolderInput'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function resolve($object, array $args, $context, ResolveInfo $info) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $parentID = isset($args['folder']['parentId']) ? intval($args['folder']['parentId']) : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($parentID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $parent = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)->byID($parentID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             if (!$parent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 throw new \InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     '%s#%s not found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     Folder::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     $parentID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // Check permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $canCreateContext = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         foreach ($args['folder'] as $name => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $canCreateContext[$this->accessor->getObjectFieldName(Folder::singleton(), $name)] = $val; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (!Folder::singleton()->canCreate($context['currentUser'], $canCreateContext)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             throw new \InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 '%s create not allowed', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 Folder::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $folder = Folder::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         foreach ($args['folder'] as $name => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->accessor->setValue($folder, $name, $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $folder->writeToStage(Versioned::DRAFT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                        
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.