| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * WebHemi. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP version 5.6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license   https://opensource.org/licenses/MIT The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @link      http://www.gixx-web.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace WebHemi\Data\Storage\AccessManagement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use WebHemi\DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use WebHemi\Data\Entity\DataEntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use WebHemi\Data\Entity\AccessManagement\ResourceEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use WebHemi\Data\Storage\AbstractDataStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class ResourceStorage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 22 |  | View Code Duplication | class ResourceStorage extends AbstractDataStorage | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $dataGroup = 'webhemi_am_resource'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $idKey = 'id_am_resource'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $name = 'name'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $title = 'title'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $description = 'description'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $isReadOnly = 'is_read_only'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $dateCreated = 'date_created'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $dateModified = 'date_modified'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Populates an entity with storage data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param DataEntityInterface $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param array               $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |     protected function populateEntity(DataEntityInterface &$entity, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         /* @var ResourceEntity $entity */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |         $entity->setResourceId($data[$this->idKey]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |             ->setName($data[$this->name]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             ->setTitle($data[$this->title]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |             ->setDescription($data[$this->description]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             ->setReadOnly($data[$this->isReadOnly]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |             ->setDateCreated(new DateTime($data[$this->dateCreated])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |             ->setDateModified(new DateTime($data[$this->dateModified])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Get data from an entity. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param DataEntityInterface $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |     protected function getEntityData(DataEntityInterface $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var ResourceEntity $entity */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         $dateCreated = $entity->getDateCreated(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         $dateModified = $entity->getDateModified(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             $this->idKey => $entity->getKeyData(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |             $this->name => $entity->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |             $this->title => $entity->getTitle(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |             $this->description => $entity->getDescription(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |             $this->isReadOnly => (int)$entity->getReadOnly(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |             $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |             $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Returns a Resource entity identified by (unique) ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param int $identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return bool|ResourceEntity | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |     public function getResourceById($identifier) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |         $entity = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |         $data = $this->getDataAdapter()->getData($identifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |         if (!empty($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |             $entity = $this->createEntity(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |             $this->populateEntity($entity, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 1 |  |         return $entity; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Returns an Resource entity by name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return bool|ResourceEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |     public function getResourceByName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $entity = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         $dataList = $this->getDataAdapter()->getDataSet([$this->name => $name], 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         if (!empty($dataList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |             $entity = $this->createEntity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             $this->populateEntity($entity, $dataList[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |         return $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
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.