| Total Complexity | 43 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Theme 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.
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 Theme, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Theme extends Foundation implements ThemeContract |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Nome do distribuidor do tema/nome do tema |
||
| 16 | * Ex: vendor/theme |
||
| 17 | * |
||
| 18 | * @var Vendor |
||
| 19 | */ |
||
| 20 | protected Vendor $vendorInstance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Informações do autor do tema |
||
| 24 | * |
||
| 25 | * @var Author |
||
| 26 | */ |
||
| 27 | protected Author $authorInstance; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Controle de caminhos de tema |
||
| 31 | * |
||
| 32 | * @var Structure |
||
| 33 | */ |
||
| 34 | protected Structure $structureInstance; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Controle do arquivo composer.json do tema |
||
| 38 | * |
||
| 39 | * @var Composer |
||
| 40 | */ |
||
| 41 | protected Composer $composerInstance; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Regras de negócio sobre busca de diretivas dentro do tema. |
||
| 45 | * |
||
| 46 | * @var DirectiveFinder |
||
| 47 | */ |
||
| 48 | protected DirectiveFinder $finderInstance; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Regras de negócio do tema |
||
| 52 | * |
||
| 53 | * @param string $package |
||
| 54 | */ |
||
| 55 | public function __construct(string $package) |
||
| 56 | { |
||
| 57 | $this->init($package); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | public function vendor() : Vendor |
||
| 64 | { |
||
| 65 | return $this->vendorInstance; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | */ |
||
| 71 | public function author(string $author = null) : Author|Theme |
||
| 72 | { |
||
| 73 | if (! $author) { |
||
| 74 | return $this->getAuthor(); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this->setAuthor($author); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritDoc} |
||
| 82 | */ |
||
| 83 | public function name() : string |
||
| 84 | { |
||
| 85 | return $this->vendor()->name(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritDoc} |
||
| 90 | */ |
||
| 91 | public function namespace() : string |
||
| 92 | { |
||
| 93 | return $this->vendor()->namespace(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | */ |
||
| 99 | public function paths() : Structure |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritDoc} |
||
| 106 | */ |
||
| 107 | public function package() : string |
||
| 108 | { |
||
| 109 | return $this->vendor()->package(); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritDoc} |
||
| 114 | */ |
||
| 115 | public function exists() : bool |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritDoc} |
||
| 122 | */ |
||
| 123 | public function preview() : string |
||
| 124 | { |
||
| 125 | return $this->composer()->preview(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | */ |
||
| 131 | public function find() : ?Theme |
||
| 132 | { |
||
| 133 | if (! $this->exists()) { |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this->import(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | */ |
||
| 143 | public function description(string $description = null) : Theme|string |
||
| 144 | { |
||
| 145 | if (! $description) { |
||
| 146 | return $this->composer()->description(); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->composer()->description($description); |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritDoc} |
||
| 156 | */ |
||
| 157 | public function make() : Theme |
||
| 158 | { |
||
| 159 | if ($this->exists()) { |
||
| 160 | throw new ThemeExistsException($this->package()); |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->structure()->init(); |
||
| 164 | |||
| 165 | $this->composer()->create()->load(); |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritDoc} |
||
| 172 | */ |
||
| 173 | public function findOrCreate() : Theme |
||
| 174 | { |
||
| 175 | if (! $this->exists()) { |
||
| 176 | return $this->make(); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $this->import(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritDoc} |
||
| 184 | */ |
||
| 185 | public function use() : Theme |
||
| 186 | { |
||
| 187 | $this->guard(); |
||
| 188 | |||
| 189 | $this->register(); |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritDoc} |
||
| 196 | */ |
||
| 197 | public function include(string $sentence) : Includer |
||
| 198 | { |
||
| 199 | return $this->finder()->include($sentence); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritDoc} |
||
| 204 | */ |
||
| 205 | public function component(string $sentence) : Component |
||
| 206 | { |
||
| 207 | return $this->finder()->component($sentence); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritDoc} |
||
| 212 | */ |
||
| 213 | public function directives() : array |
||
| 214 | { |
||
| 215 | return $this->finder()->all(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritDoc} |
||
| 220 | */ |
||
| 221 | public function load() : Theme |
||
| 222 | { |
||
| 223 | foreach ($this->directives() as $directive) { |
||
| 224 | $directive->load(); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | public function publish() : bool |
||
| 234 | { |
||
| 235 | if (! $this->exists()) { |
||
| 236 | throw new ThemeNotFoundException($this->package()); |
||
| 237 | } |
||
| 238 | |||
| 239 | $from = $this->structure()->assets(); |
||
| 240 | $to = $this->structure()->public(); |
||
| 241 | |||
| 242 | File::copyDirectory($from, $to); |
||
| 243 | |||
| 244 | return (is_dir($to)) ? true : false; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritDoc} |
||
| 249 | */ |
||
| 250 | public function url(string $file = null) : string |
||
| 251 | { |
||
| 252 | $domain = $this->env()->get('APP_URL'); |
||
| 253 | $public = $this->config()->publishable(); |
||
| 254 | $package = $this->package(); |
||
| 255 | |||
| 256 | $file = '/' . $file ?? ''; |
||
| 257 | $file = str_replace("'", "", $file); |
||
| 258 | |||
| 259 | return sprintf('%s/%s/%s%s', $domain, $public, $package, $file); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Registra o nome do pacote do tema no arquivo de ambiente |
||
| 264 | * do projeto Laravel |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | private function register() |
||
| 269 | { |
||
| 270 | $name = $this->package(); |
||
| 271 | |||
| 272 | $key = $this->config()->env(); |
||
| 273 | |||
| 274 | $this->env()->set($key, $name); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Inicia as regras de negócio do tema |
||
| 279 | * Se o tema já existir dentro do projeto, carrega suas informações |
||
| 280 | * |
||
| 281 | * @param string $package |
||
| 282 | * @return Theme |
||
| 283 | */ |
||
| 284 | private function init(string $package) : Theme |
||
| 285 | { |
||
| 286 | $this->setVendor($package) |
||
| 287 | ->setDirectiveFinder() |
||
| 288 | ->setComposer() |
||
| 289 | ->setStructure(); |
||
| 290 | |||
| 291 | if ($this->exists()) { |
||
| 292 | return $this->import(); |
||
| 293 | } |
||
| 294 | |||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Carrega as informações vindas do arquivo composer.json |
||
| 300 | * para dentro da instância |
||
| 301 | * |
||
| 302 | * @return Theme |
||
| 303 | */ |
||
| 304 | private function import() : Theme |
||
| 305 | { |
||
| 306 | $info = $this->composer()->load()->info(); |
||
| 307 | |||
| 308 | $this->author()->name($info->authors[0]->name); |
||
| 309 | $this->author()->email($info->authors[0]->email); |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Define o nome do vendor do tema. |
||
| 316 | * |
||
| 317 | * @return Theme |
||
| 318 | */ |
||
| 319 | private function setVendor(string $vendor) : Theme |
||
| 320 | { |
||
| 321 | $this->vendorInstance = new Vendor($vendor); |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Define as informações do autor do tema |
||
| 328 | * |
||
| 329 | * @param string $author |
||
| 330 | * @return Theme |
||
| 331 | */ |
||
| 332 | private function setAuthor(string $author) : Theme |
||
| 333 | { |
||
| 334 | $this->authorInstance = new Author($author); |
||
| 335 | |||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Retorna as informações do autor do tema |
||
| 341 | * |
||
| 342 | * @return Author |
||
| 343 | */ |
||
| 344 | private function getAuthor() : Author |
||
| 345 | { |
||
| 346 | return $this->authorInstance ?? new Author(); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Define a instância de estrutura de diretórios do tema |
||
| 351 | * |
||
| 352 | * @return Theme |
||
| 353 | */ |
||
| 354 | private function setStructure() : Theme |
||
| 355 | { |
||
| 356 | $vendor = $this->vendor(); |
||
| 357 | |||
| 358 | $this->structureInstance = new Structure($vendor); |
||
| 359 | |||
| 360 | return $this; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Retorna a instância de estrutura do tema |
||
| 365 | * |
||
| 366 | * @return Structure |
||
| 367 | */ |
||
| 368 | private function structure() : Structure |
||
| 369 | { |
||
| 370 | return $this->structureInstance; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Define o composer do tema |
||
| 375 | * |
||
| 376 | * @return Theme |
||
| 377 | */ |
||
| 378 | private function setComposer() : Theme |
||
| 379 | { |
||
| 380 | $this->composerInstance = new Composer($this); |
||
| 381 | |||
| 382 | return $this; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Retorna a instância do composer do tema |
||
| 387 | * |
||
| 388 | * @return Theme |
||
| 389 | */ |
||
| 390 | private function composer() : Composer |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Define a instância de busca de diretivas dentro do tema. |
||
| 397 | * |
||
| 398 | * @return Theme |
||
| 399 | */ |
||
| 400 | private function setDirectiveFinder() : Theme |
||
| 401 | { |
||
| 402 | $this->finderInstance = new DirectiveFinder($this); |
||
| 403 | |||
| 404 | return $this; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Retorna a instância de busca de diretivas dentro do tema. |
||
| 409 | * |
||
| 410 | * @return DirectiveFinder |
||
| 411 | */ |
||
| 412 | private function finder() : DirectiveFinder |
||
| 413 | { |
||
| 414 | return $this->finderInstance; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Faz as devidas validações sobre o tema. |
||
| 419 | * Se existir algo incompátivel, emite um Exception. |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | private function guard() : void |
||
| 430 | } |
||
| 431 | |||
| 432 | } |
||
| 433 |