| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace SilverStripe\AssetAdmin\GraphQL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use SilverStripe\Assets\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Assets\Folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\ORM\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GraphQL\Type\Definition\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GraphQL\Type\Definition\UnionType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\GraphQL\Pagination\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\ORM\Versioning\Versioned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ReadFileQueryCreator extends PaginatedQueryCreator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function attributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'name' => 'readFiles' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function connection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return Connection::create('readFiles') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->setConnectionType(function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 $unionType = new UnionType([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                     'name' => 'Result', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     'types' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                         $this->manager->getType('File'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                         $this->manager->getType('Folder') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 |  | View Code Duplication |                     'resolveType' => function ($object) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                         if ($object instanceof Folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                             return $this->manager->getType('Folder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                         if ($object instanceof File) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                             return $this->manager->getType('File'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 return $unionType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->setArgs([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'id' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     'type' => Type::id(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'parentId' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     'type' => Type::id(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->setSortableFields(['ID', 'Title', 'Created', 'LastEdited']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->setConnectionResolver(array($this, 'resolveConnection')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     public function resolveConnection($object, array $args, $context, $info) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         // Permission checks | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $parent = Folder::singleton(); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         if (isset($args['parentId'])) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             $parent = Folder::get()->byID($args['parentId']); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             if (!$parent) { | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |                 throw new \InvalidArgumentException(sprintf( | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |                     '%s#%s not found', | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                     Folder::class, | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                     $args['parentId'] | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                 )); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         if (!$parent->canView($context['currentUser'])) { | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             throw new \InvalidArgumentException(sprintf( | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |                 '%s#%s view access not permitted', | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |                 Folder::class, | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |                 $parent->ID | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             )); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         $list = Versioned::get_by_stage(File::class, Versioned::DRAFT); | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         if (isset($args['parentId'])) { | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             $list = $list->filter('ParentID', $args['parentId']); | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |         if (isset($args['id']) && (int)$args['id'] > 0) { | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             $list = $list->filter('ID', $args['id']); | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         } elseif (isset($args['id']) && (int)$args['id'] === 0) { | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             // Special case for root folder | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             $list = new ArrayList([new Folder([ | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                 'ID' => 0, | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             ])]); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         $list = $list->filterByCallback(function (File $file) use ($context) { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             return $file->canView($context['currentUser']); | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         // TODO Sorting | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         return $list; | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                        
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.