BrandingCommand::getDefaults()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 8.8571
cc 5
eloc 8
nc 6
nop 1
crap 30
1
<?php
2
3
namespace Zoutapps\Laravel\Backpack\Branding\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Zoutapps\Laravel\Backpack\Branding\Services\BackpackBaseConfigService;
8
use Zoutapps\Laravel\Backpack\Branding\Services\CopyService;
9
use Zoutapps\Laravel\Backpack\Branding\Services\EnvService;
10
use Zoutapps\Laravel\Backpack\Branding\Services\BrandingService;
11
12
class BrandingCommand extends Command
13
{
14
    use ConfirmableTrait;
15
16
    protected $signature = 'za:brand
17
                    {path? : the path where your branding defaults are}
18
                    {--no-env : Skip setting .env values}
19
                    {--no-config : Skip setting config values}
20
                    {--no-copy : Skip copying the specified files}
21
                    {--no-helper : Skip setting up the Branding Facade}
22
                    {--force : Force the operation to run when in production}';
23
24
    protected $description = 'Brand your fresh Laravel-Backpack installation';
25
26
    public $defaultsPath;
27
28
    private $defaults;
29
30
    public function handle()
31
    {
32
        if (!$this->confirmToProceed()) {
33
            return false;
34
        }
35
36
        if ($path = $this->argument('path')) {
37
38
            if (!$fullPath = realpath($path)) {
39
                $this->error('Provided defaults path <comment>'.$fullPath.'</comment> does not exist.');
40
                return false;
41
            }
42
43
            $this->defaultsPath = $fullPath;
44
        }
45
46
        if (!$this->option('no-env')) {
47
            $envService = new EnvService($this, $this->envPath());
48
            $envService->perform();
49
        }
50
51
        if (!$this->option('no-config')) {
52
            $baseConfigService = new BackpackBaseConfigService($this, $this->laravel->basePath('config/backpack/base.php'));
0 ignored issues
show
Unused Code introduced by
The call to Application::basePath() has too many arguments starting with 'config/backpack/base.php'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
53
            $baseConfigService->perform();
54
        }
55
56
        if(!$this->option('no-copy')) {
57
            $copyService = new CopyService($this);
58
            $copyService->perform();
59
        }
60
61
        if (!$this->option('no-helper')) {
62
            $helperService = new BrandingService($this);
63
            $helperService->perform();
64
        }
65
    }
66
67
    /** @noinspection PhpDocMissingThrowsInspection */
68
    /**
69
     * @param string|null $key
70
     *
71
     * @return \Illuminate\Support\Collection
72
     */
73
    public function getDefaults($key = null)
74
    {
75
        if (!$this->defaults && $this->defaultsPath) {
76
            $content = file_get_contents($this->defaultsPath . '/defaults.json');
77
            $assoc = json_decode($content, true);
78
79
            if (json_last_error() !== JSON_ERROR_NONE) {
80
                $this->error('Provided defaults.json does not contain valid json.');
81
            }
82
83
            $this->defaults = collect($assoc);
84
        }
85
86
        return $key ? collect($this->defaults[$key] ?? null) : $this->defaults;
87
    }
88
89
    /**
90
     * Get the .env file path.
91
     *
92
     * @return string
93
     */
94
    protected function envPath()
95
    {
96
        if (method_exists($this->laravel, 'environmentFilePath')) {
97
            return $this->laravel->environmentFilePath();
0 ignored issues
show
Bug introduced by
The method environmentFilePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
        }
99
        return $this->laravel->basePath('.env');
0 ignored issues
show
Unused Code introduced by
The call to Application::basePath() has too many arguments starting with '.env'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
    }
101
}