| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * compiler pass for building a listing of fields for compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Graviton\DocumentBundle\DependencyInjection\Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\ArrayField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class DocumentFormFieldsCompilerPass implements CompilerPassInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var DocumentMap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $documentMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $typeMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'string'  => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'extref'  => 'extref', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'int'     => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'float'   => 'number', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'boolean' => 'strictboolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'date'    => 'datetime', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param DocumentMap $documentMap Document map | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function __construct(DocumentMap $documentMap) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->documentMap = $documentMap; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * load services | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param ContainerBuilder $container container builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication |     public function process(ContainerBuilder $container) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $map = ['stdclass' => []]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($this->documentMap->getDocuments() as $document) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $map[$document->getClass()] = $this->getFormFields($document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $container->setParameter('graviton.document.form.type.document.field_map', $map); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Get document fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param Document $document Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function getFormFields(Document $document) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $reflection = new \ReflectionClass($document->getClass()); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 76 |  | View Code Duplication |         if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $instance = $reflection->newInstanceWithoutConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $translatableFields = $instance->getTranslatableFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $translatableFields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         foreach ($document->getFields() as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if ($field instanceof Field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 list($type, $options) = $this->resolveFieldParams( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     $translatableFields, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     $field->getFieldName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     $field->getType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $result[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $field->getFormName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     array_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         ['property_path' => $field->getFieldName()], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                         $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } elseif ($field instanceof ArrayField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 list($type, $options) = $this->resolveFieldParams( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     $translatableFields, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     $field->getFieldName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $field->getItemType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $result[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     $field->getFormName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     'collection', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                         'property_path' => $field->getFieldName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                         'type' => $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         'options' => $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } elseif ($field instanceof EmbedOne) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $result[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     $field->getFormName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     'form', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                         'property_path' => $field->getFieldName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                         'data_class' => $field->getDocument()->getClass(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                         'required' => $field->isRequired(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } elseif ($field instanceof EmbedMany) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $result[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     $field->getFormName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     'collection', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         'property_path' => $field->getFieldName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                         'type' => 'form', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                         'options' => ['data_class' => $field->getDocument()->getClass()], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Resolve simple field type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param array  $translatable Translatable fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $fieldName    Field name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $fieldType    Field type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param mixed  $field        optional field to pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return array Form type and options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     private function resolveFieldParams(array $translatable, $fieldName, $fieldType, $field = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if (in_array($fieldName, $translatable, true) || in_array($fieldName.'[]', $translatable, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $type = 'translatable'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             if ($field instanceof Field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 $options['required'] = $field->isRequired(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } elseif ($fieldType === 'hash') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             $type = 'freeform'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } elseif ($fieldType === 'hasharray') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $type = 'collection'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             $options['type'] = 'freeform'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } elseif ($fieldType === 'datearray') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $type = 'datearray'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } elseif (isset($this->typeMap[$fieldType])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $type = $this->typeMap[$fieldType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $type = 'text'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         return [$type, $options]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                        
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.