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 cache. |
||
| 36 | * |
||
| 37 | * @var \Illuminate\Cache\CacheManager |
||
| 38 | */ |
||
| 39 | protected $cache; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Config. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $config; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The instance of the database. |
||
| 50 | * |
||
| 51 | * @var \Illuminate\Database\DatabaseManager |
||
| 52 | */ |
||
| 53 | protected $db; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Name of the cache. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $cache_name; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Texts collection. |
||
| 64 | * |
||
| 65 | * @var \Illuminate\Support\Collection |
||
| 66 | */ |
||
| 67 | protected $texts; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Missing texts. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $new_texts; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Create a new MultiLang instance. |
||
| 78 | * |
||
| 79 | * @param string $environment |
||
| 80 | * @param array $config |
||
| 81 | * @param \Illuminate\Cache\CacheManager $cache |
||
| 82 | * @param \Illuminate\Database\DatabaseManager $db |
||
| 83 | * @return void |
||
|
|
|||
| 84 | */ |
||
| 85 | 13 | public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
|
| 86 | { |
||
| 87 | 13 | $this->environment = $environment; |
|
| 88 | 13 | $this->cache = $cache; |
|
| 89 | 13 | $this->db = $db; |
|
| 90 | |||
| 91 | 13 | $this->setConfig($config); |
|
| 92 | 13 | } |
|
| 93 | |||
| 94 | 13 | public function setConfig(array $config) |
|
| 95 | { |
||
| 96 | 13 | $this->config = [ |
|
| 97 | 13 | 'enabled' => true, |
|
| 98 | 'locales' => [ |
||
| 99 | 'en' => [ |
||
| 100 | 13 | 'name' => 'English', |
|
| 101 | 13 | 'native_name' => 'English', |
|
| 102 | 13 | 'default' => true, |
|
| 103 | 13 | ], |
|
| 104 | 13 | ], |
|
| 105 | 13 | 'autosave' => true, |
|
| 106 | 13 | 'cache' => true, |
|
| 107 | 13 | 'cache_lifetime' => 1440, |
|
| 108 | 13 | 'texts_table' => 'texts', |
|
| 109 | ]; |
||
| 110 | |||
| 111 | 13 | foreach ($config as $k => $v) { |
|
| 112 | 1 | $this->config[$k] = $v; |
|
| 113 | 13 | } |
|
| 114 | 13 | } |
|
| 115 | |||
| 116 | 12 | public function getConfig($key = null) |
|
| 117 | { |
||
| 118 | 12 | if ($key === null) { |
|
| 119 | return $this->config; |
||
| 120 | } |
||
| 121 | |||
| 122 | 12 | return isset($this->config[$key]) ? $this->config[$key] : null; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set locale and load texts |
||
| 127 | * |
||
| 128 | * @param string $lang |
||
| 129 | * @param array $text |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 13 | public function setLocale($lang, $texts = null) |
|
| 133 | { |
||
| 134 | 13 | if (!$lang) { |
|
| 135 | 1 | throw new InvalidArgumentException('Locale is empty'); |
|
| 136 | } |
||
| 137 | 12 | $this->lang = $lang; |
|
| 138 | |||
| 139 | 12 | $this->setCacheName($lang); |
|
| 140 | |||
| 141 | 12 | if (is_array($texts)) { |
|
| 142 | $texts = new Collection($texts); |
||
| 143 | } |
||
| 144 | |||
| 145 | 12 | if ($texts === null) { |
|
| 146 | 12 | $texts = $this->loadTexts($this->getLocale()); |
|
| 147 | 12 | } |
|
| 148 | |||
| 149 | 12 | $this->texts = new Collection($texts); |
|
| 150 | 12 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Load texts |
||
| 154 | * |
||
| 155 | * @param string $lang |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 12 | public function loadTexts($lang = null) |
|
| 159 | { |
||
| 160 | 12 | $cache = $this->getConfig('cache'); |
|
| 161 | |||
| 162 | 12 | if (!$cache || $this->cache === null || $this->environment != 'production') { |
|
| 163 | 9 | $texts = $this->loadTextsFromDatabase($lang); |
|
| 164 | 9 | return $texts; |
|
| 165 | } |
||
| 166 | |||
| 167 | 4 | if ($this->mustLoadFromCache()) { |
|
| 168 | $texts = $this->loadTextsFromCache(); |
||
| 169 | } else { |
||
| 170 | 4 | $texts = $this->loadTextsFromDatabase($lang); |
|
| 171 | 4 | $this->storeTextsInCache($texts); |
|
| 172 | } |
||
| 173 | |||
| 174 | 4 | return $texts; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get translated text |
||
| 179 | * |
||
| 180 | * @param string $key |
||
| 181 | * @param string $default |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 4 | public function get($key) |
|
| 185 | { |
||
| 186 | |||
| 187 | 4 | if (empty($key)) { |
|
| 188 | 1 | throw new InvalidArgumentException('String key not provided'); |
|
| 189 | } |
||
| 190 | |||
| 191 | 3 | if (!$this->lang) { |
|
| 192 | return $key; |
||
| 193 | } |
||
| 194 | |||
| 195 | 3 | if (!$this->texts->has($key)) { |
|
| 196 | 2 | $this->queueToSave($key); |
|
| 197 | 2 | return $key; |
|
| 198 | } |
||
| 199 | |||
| 200 | 1 | $text = $this->texts->get($key); |
|
| 201 | |||
| 202 | 1 | return $text; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get texts |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 2 | public function getTexts() |
|
| 211 | { |
||
| 212 | |||
| 213 | 2 | return $this->texts->toArray(); |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set texts manually |
||
| 218 | * |
||
| 219 | * @param array $texts_array |
||
| 220 | * @return \Longman\LaravelMultiLang\MultiLang |
||
| 221 | */ |
||
| 222 | 3 | public function setTexts(array $texts_array) |
|
| 223 | { |
||
| 224 | 3 | $texts = []; |
|
| 225 | 3 | foreach ($texts_array as $key => $value) { |
|
| 226 | 3 | $texts[$key] = $value; |
|
| 227 | 3 | } |
|
| 228 | |||
| 229 | 3 | $this->texts = new Collection($texts); |
|
| 230 | |||
| 231 | 3 | return $this; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Queue missing texts |
||
| 236 | * |
||
| 237 | * @param string $key |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 2 | protected function queueToSave($key) |
|
| 241 | { |
||
| 242 | 2 | $this->new_texts[$key] = $key; |
|
| 243 | 2 | } |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Check if we must load texts from cache |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 4 | public function mustLoadFromCache() |
|
| 251 | { |
||
| 252 | 4 | return $this->cache->has($this->getCacheName()); |
|
| 253 | } |
||
| 254 | |||
| 255 | 4 | protected function storeTextsInCache(array $texts) |
|
| 256 | { |
||
| 257 | 4 | $cache_lifetime = $this->getConfig('cache_lifetime', 1440); |
|
| 258 | 4 | $status = $this->cache->put($this->getCacheName(), $texts, $cache_lifetime); |
|
| 259 | 4 | return $status; |
|
| 260 | } |
||
| 261 | |||
| 262 | 12 | public function loadTextsFromDatabase($lang) |
|
| 263 | { |
||
| 264 | 12 | $texts = $lang ? $this->db->table($this->getTableName()) |
|
| 265 | 12 | ->where('lang', $lang) |
|
| 266 | 12 | ->get(['key', 'value', 'lang', 'scope']) : $this->db->table($this->getTableName())->get(['key', 'value', 'lang', 'scope']); |
|
| 267 | |||
| 268 | 12 | $array = []; |
|
| 269 | 12 | foreach ($texts as $row) { |
|
| 270 | 12 | $array[$row->key] = $row->value; |
|
| 271 | 12 | } |
|
| 272 | 12 | return $array; |
|
| 273 | } |
||
| 274 | |||
| 275 | 1 | public function loadTextsFromCache() |
|
| 276 | { |
||
| 277 | 1 | $texts = $this->cache->get($this->getCacheName()); |
|
| 278 | |||
| 279 | 1 | return $texts; |
|
| 280 | } |
||
| 281 | |||
| 282 | 12 | public function setCacheName($lang) |
|
| 283 | { |
||
| 284 | 12 | $this->cache_name = $this->getConfig('texts_table') . '_' . $lang; |
|
| 285 | 12 | } |
|
| 286 | |||
| 287 | 4 | public function getCacheName() |
|
| 288 | { |
||
| 289 | 4 | return $this->cache_name; |
|
| 290 | } |
||
| 291 | |||
| 292 | 1 | public function getUrl($path) |
|
| 300 | |||
| 301 | 1 | public function autoSaveIsAllowed() |
|
| 302 | { |
||
| 303 | 1 | if ($this->environment == 'local' && $this->getConfig('autosave') && $this->db !== null) { |
|
| 304 | 1 | return true; |
|
| 305 | } |
||
| 306 | 1 | return false; |
|
| 307 | } |
||
| 308 | |||
| 309 | 12 | public function getLocale() |
|
| 313 | |||
| 314 | 1 | public function saveTexts() |
|
| 315 | { |
||
| 316 | 1 | if (empty($this->new_texts)) { |
|
| 317 | 1 | return false; |
|
| 318 | } |
||
| 319 | |||
| 320 | 1 | $lang = $this->lang; |
|
| 321 | 1 | $table = $this->getTableName(); |
|
| 322 | 1 | foreach ($this->new_texts as $k => $v) { |
|
| 323 | 1 | $exists = $this->db->table($table)->where([ |
|
| 324 | 1 | 'key' => $k, |
|
| 325 | 1 | 'lang' => $lang, |
|
| 326 | 1 | ])->first(); |
|
| 327 | |||
| 328 | 1 | if ($exists) { |
|
| 329 | continue; |
||
| 340 | |||
| 341 | 12 | protected function getTableName() |
|
| 346 | } |
||
| 347 |
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.