Complex classes like MultiLang often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MultiLang, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MultiLang |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Language/Locale. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $lang; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * System environment |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $environment; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The instance of the laravel app. |
||
| 36 | * |
||
| 37 | * @var \Illuminate\Foundation\Application |
||
| 38 | */ |
||
| 39 | protected $app; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The instance of the cache. |
||
| 43 | * |
||
| 44 | * @var \Illuminate\Cache\CacheManager |
||
| 45 | */ |
||
| 46 | protected $cache; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Config. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $config; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The instance of the database. |
||
| 57 | * |
||
| 58 | * @var \Illuminate\Database\DatabaseManager |
||
| 59 | */ |
||
| 60 | protected $db; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Name of the cache. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $cache_name; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Texts collection. |
||
| 71 | * |
||
| 72 | * @var \Illuminate\Support\Collection |
||
| 73 | */ |
||
| 74 | protected $texts; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Missing texts. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $new_texts; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create a new MultiLang instance. |
||
| 85 | * |
||
| 86 | * @param string $environment |
||
| 87 | * @param array $config |
||
| 88 | * @param \Illuminate\Cache\CacheManager $cache |
||
| 89 | * @param \Illuminate\Database\DatabaseManager $db |
||
| 90 | * @return void |
||
|
|
|||
| 91 | */ |
||
| 92 | 5 | public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
|
| 100 | |||
| 101 | 5 | public function setConfig(array $config) |
|
| 102 | { |
||
| 103 | 5 | $this->config = [ |
|
| 104 | 'enabled' => true, |
||
| 105 | 'locales' => [ |
||
| 106 | 'en' => [ |
||
| 107 | 'name' => 'English', |
||
| 108 | 'native_name' => 'English', |
||
| 109 | 'default' => true, |
||
| 110 | ], |
||
| 111 | ], |
||
| 112 | 'autosave' => true, |
||
| 113 | 'cache' => true, |
||
| 114 | 'cache_lifetime' => 1440, |
||
| 115 | 'texts_table' => 'texts', |
||
| 116 | ]; |
||
| 117 | |||
| 118 | 5 | foreach ($config as $k => $v) { |
|
| 119 | 1 | $this->config[$k] = $v; |
|
| 120 | } |
||
| 121 | 5 | } |
|
| 122 | |||
| 123 | 5 | public function getConfig($key = null) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Set locale and load texts |
||
| 134 | * |
||
| 135 | * @param string $lang |
||
| 136 | * @param \Illuminate\Support\Collection|array $text |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | 5 | public function setLocale($lang, $texts = null) |
|
| 140 | { |
||
| 141 | 5 | if (!$lang) { |
|
| 142 | throw new InvalidArgumentException('Locale is empty!'); |
||
| 143 | } |
||
| 144 | 5 | $this->lang = $lang; |
|
| 145 | |||
| 146 | 5 | $this->setCacheName(); |
|
| 147 | |||
| 148 | 5 | if (is_array($texts)) { |
|
| 149 | $texts = new Collection($texts); |
||
| 150 | } |
||
| 151 | |||
| 152 | 5 | $this->texts = $texts ? $texts : $this->loadTexts($this->getLocale()); |
|
| 153 | 5 | } |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Load texts |
||
| 157 | * |
||
| 158 | * @param string $lang |
||
| 159 | * @return \Illuminate\Support\Collection |
||
| 160 | */ |
||
| 161 | 5 | public function loadTexts($lang = null) |
|
| 162 | { |
||
| 163 | 5 | $cache = $this->getConfig('cache'); |
|
| 164 | |||
| 165 | 5 | if (!$cache || $this->cache === null || $this->environment != 'production') { |
|
| 166 | 5 | $texts = $this->loadTextsFromDatabase($lang); |
|
| 167 | 5 | return $texts; |
|
| 168 | } |
||
| 169 | |||
| 170 | if ($this->mustLoadFromCache()) { |
||
| 171 | $texts = $this->loadTextsFromCache(); |
||
| 172 | } else { |
||
| 173 | $texts = $this->loadTextsFromDatabase($lang); |
||
| 174 | $this->storeTextsInCache($texts); |
||
| 175 | } |
||
| 176 | |||
| 177 | $texts = new Collection($texts); |
||
| 178 | |||
| 179 | return $texts; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get translated text |
||
| 184 | * |
||
| 185 | * @param string $key |
||
| 186 | * @param string $default |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 2 | public function get($key) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get texts |
||
| 212 | * |
||
| 213 | * @param string $lang |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getTexts($lang = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set texts manually |
||
| 224 | * |
||
| 225 | * @param array $texts_array |
||
| 226 | * @return \Longman\LaravelMultiLang\MultiLang |
||
| 227 | */ |
||
| 228 | 2 | public function setTexts(array $texts_array) |
|
| 229 | { |
||
| 230 | 2 | $texts = []; |
|
| 231 | 2 | foreach ($texts_array as $key => $value) { |
|
| 232 | 2 | $texts[$key] = $value; |
|
| 233 | } |
||
| 234 | |||
| 235 | 2 | $this->texts = new Collection($texts); |
|
| 236 | |||
| 237 | 2 | return $this; |
|
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Queue missing texts |
||
| 242 | * |
||
| 243 | * @param string $key |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | 1 | protected function queueToSave($key) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Check if we must load texts from cache |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | protected function mustLoadFromCache() |
||
| 260 | |||
| 261 | protected function storeTextsInCache(array $texts) |
||
| 267 | |||
| 268 | 5 | protected function loadTextsFromDatabase($lang) |
|
| 269 | { |
||
| 270 | 5 | $texts = $lang ? $this->db->table($this->getTableName()) |
|
| 271 | 5 | ->where('lang', $lang) |
|
| 272 | 5 | ->get(['key', 'value', 'lang', 'scope']) : $this->db->table($this->getTableName())->get(['key', 'value', 'lang', 'scope']); |
|
| 273 | |||
| 274 | 5 | $array = []; |
|
| 275 | 5 | foreach ($texts as $row) { |
|
| 276 | $array[$row->key] = $row->value; |
||
| 277 | } |
||
| 278 | 5 | return $array; |
|
| 279 | } |
||
| 280 | |||
| 281 | protected function loadTextsFromCache() |
||
| 286 | |||
| 287 | 5 | protected function setCacheName() |
|
| 291 | |||
| 292 | 1 | public function getUrl($path) |
|
| 293 | { |
||
| 294 | 1 | $locale = $this->getLocale(); |
|
| 295 | 1 | if ($locale) { |
|
| 296 | 1 | $path = $locale . '/' . $path; |
|
| 297 | } |
||
| 298 | 1 | return $path; |
|
| 299 | } |
||
| 300 | |||
| 301 | public function autoSaveIsAllowed() |
||
| 308 | |||
| 309 | 5 | public function getLocale() |
|
| 313 | |||
| 314 | public function saveTexts() |
||
| 315 | { |
||
| 316 | if (empty($this->new_texts)) { |
||
| 317 | return null; |
||
| 318 | } |
||
| 319 | |||
| 345 | |||
| 346 | 5 | protected function getTableName($with_prefix = false) |
|
| 355 | } |
||
| 356 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.