| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sludio\HelperBundle\Translatable\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sludio\HelperBundle\Translatable\Repository\TranslatableRepository as Sludio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | abstract class BaseEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     public $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     public $translates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public $localeArr = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         'lv' => 'lv_LV', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         'en' => 'en_US', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         'ru' => 'ru_RU', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     abstract public function getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         if (method_exists($this, 'getId') && $this->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $this->translates = $this->getTranslations(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->getClassName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function getClassName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if (!$this->className) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $className = explode('\\', get_called_class()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $this->className = strtolower(end($className)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return $this->className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getLocaleVar($locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return isset($this->localeArr[$locale]) ? $this->localeArr[$locale] : $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 43 |  | View Code Duplication |     public function __get($property) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         if (!method_exists($this, 'get'.ucfirst($property))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $locale = Sludio::getDefaultLocale(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $locale = strtolower(substr($property, -2)); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $property = substr($property, 0, -2); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (in_array($locale, array_keys($this->localeArr))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             return $this->getVariableByLocale($property, $this->localeArr[$locale]); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return $this->{$property}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 59 |  | View Code Duplication |     public function __set($property, $value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $locale = strtolower(substr($property, -2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (in_array($locale, array_keys($this->localeArr))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $property = substr($property, 0, -2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             Sludio::updateTranslations(get_class($this), $this->localeArr[$locale], $property, $value, $this->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->{$property} = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function getTranslations() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return Sludio::getTranslations(get_called_class(), $this->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function getVariableByLocale($variable, $locale = null, $returnOriginal = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if (!$this->translates && $this->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->translates = $this->getTranslations(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (isset($this->translates[$locale][$variable])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $this->translates[$locale][$variable]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if ($returnOriginal) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $variables = explode('_', $variable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             foreach ($variables as &$var) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $var = ucfirst($var); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $variable = implode('', $variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $result = $this->{'get'.$variable}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (is_numeric($result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function cleanText($text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return html_entity_decode(preg_replace('/\s+/S', ' ', str_replace(' ?', '', mb_convert_encoding(strip_tags($text), "UTF-8", "UTF-8")))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                        
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.