| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | //[PHPCOMPRESSOR(remove,start)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Created by Vitaly Iegorov <[email protected]>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * on 22.03.16 at 15:46 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace samsoncms\api\generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use samsoncms\api\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use samsoncms\api\generator\metadata\VirtualMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Virtual entity class generator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package samsoncms\api\generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class VirtualEntity extends RealEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Class uses generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected function createUses($metadata) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->generator | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             ->newLine('use samsonframework\core\ViewInterface;') | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             ->newLine('use samsonframework\orm\QueryInterface;') | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             ->newLine(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Class definition generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function createDefinition($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |          * TODO: Parent problem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |          * Should be changed to merging fields instead of extending with OOP for structure_relation support | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |          * or creating traits and using them on shared parent entities. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $parentClass = null !== $metadata->parent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ? $metadata->parent->entityClassName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             : '\\'.\samsoncms\api\Entity::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->defClass($this->className, $parentClass) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->newLine('use \samsoncms\api\generated\TableTrait;') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Class constants generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     protected function createConstants($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->commentVar('string', 'Entity full class name, use ::class instead') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->defClassConst('ENTITY', $metadata->entityClassName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->commentVar('string', 'Entity manager full class name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->defClassConst('MANAGER', $metadata->entityClassName . 'Query') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ->commentVar('string', 'Entity database identifier') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->defClassConst('IDENTIFIER', $metadata->entityID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->commentVar('string', 'Not transliterated entity name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->defClassConst('NAME', $metadata->entityRealName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         // Create all entity fields constants storing each additional field metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($metadata->fields as $fieldID => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ->defClassConst('F_' . $fieldName, $fieldName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 ->defClassConst('F_' . $fieldName . '_ID', $fieldID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Class static fields generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     protected function createStaticFields($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->defClassVar('$_map', 'public static ', $metadata->arMap) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->defClassVar('$_own_group', 'public static ', $metadata->arGroup) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->commentVar('array', '@deprecated Old ActiveRecord data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->defClassVar('$_relations', 'public static ', $metadata->arRelations) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->commentVar('array', 'Collection of navigation identifiers') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->defClassVar('$fieldIDs', 'protected static', $metadata->fields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->commentVar('array', 'Collection of additional fields value column names') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Class fields generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 119 |  | View Code Duplication |     protected function createFields($metadata) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach ($metadata->fields as $fieldID => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $this->generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 ->defClassVar('$' . $fieldName, 'public'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Class methods generation part. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param VirtualMetadata $metadata Entity metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function createMethods($metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $methods = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         // Generate Query::where() analog for specific field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         foreach ($metadata->fields as $fieldID => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 // We need only gallery fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 if ($metadata->allFieldCmsTypes[$fieldID] === Field::TYPE_GALLERY) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     $galleryName = preg_replace('/Gallery$/i', '', $fieldName) . 'Gallery'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     $code = "\n\t" . '/**'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                     $code .= "\n\t" . ' * Get ' . $fieldName . '(#' . $fieldID . ') gallery collection instance.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     $code .= "\n\t" . ' * @param ViewInterface $renderer Render instance'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     $code .= "\n\t" . ' *'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                     $code .= "\n\t" . ' * @return GalleryCollection Gallery collection instance'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                     $code .= "\n\t" . ' */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                     $code .= "\n\t" . 'public function create' . ucfirst($galleryName) . '(ViewInterface $renderer)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     $code .= "\n\t" . '{'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     $code .= "\n\t\t" . '$materialFieldID = (new MaterialFieldQuery($this->query))->materialID($this->id)->fieldID('.$fieldID.')->first();'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                     $code .= "\n\t\t" . 'return (new GalleryCollection($renderer, $this->query))->materialID($this->id)->materialFieldID($materialFieldID->id);'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     $code .= "\n\t" . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                     $methods[] = $code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 throw new \Exception($metadata->entity . ' cms field type for [' . $fieldName . '] not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         // Add method text to generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->generator->text(implode("\n", $methods)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  | //[PHPCOMPRESSOR(remove,end)] | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                        
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.