PublishSite   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A envVar() 0 4 1
A argKey() 0 4 1
A questionText() 0 4 1
A before() 0 6 1
A default() 0 11 3
A value() 0 22 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 157.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\ItFetchesSites;
6
use GuzzleHttp\Client;
7
8
/**
9
 * Class PublishSite.
10
 *
11
 * @package Acacha\ForgePublish\Commands
12
 */
13
class PublishSite extends SaveEnvVariable
14
{
15
    use ItFetchesSites;
16
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'publish:site {site?}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Save AcachaForge site';
30
31
    /**
32
     * Sites.
33
     *
34
     * @var string
35
     */
36
    protected $sites;
37
38
    /**
39
     * Default value.
40
     *
41
     * @var string
42
     */
43
    protected $default;
44
45
    /**
46
     * Site names.
47
     *
48
     * @var array
49
     */
50
    protected $site_names;
51
52
    /**
53
     * Site already exists?.
54
     *
55
     * @var boolean
56
     */
57
    protected $site_already_exists = false;
58
59
    /**
60
     * Server names.
61
     *
62
     * @var Client
63
     */
64
    protected $http;
65
66
    /**
67
     * SaveEnvVariable constructor.
68
     *
69
     */
70
    public function __construct(Client $http)
71
    {
72
        parent::__construct();
73
        $this->http = $http;
74
    }
75
76
    /**
77
     * Env var to set.
78
     *
79
     * @return mixed
80
     */
81
    protected function envVar()
82
    {
83
        return 'ACACHA_FORGE_SITE';
84
    }
85
86
    /**
87
     * Argument key.
88
     *
89
     * @return mixed
90
     */
91
    protected function argKey()
92
    {
93
        return 'site';
94
    }
95
96
    /**
97
     * Question text.
98
     *
99
     * @return mixed
100
     */
101
    protected function questionText()
102
    {
103
        return 'Acacha forge site id?';
104
    }
105
106
    /**
107
     * Before hook.
108
     */
109
    protected function before()
110
    {
111
        $this->sites = $this->fetchSites(fp_env('ACACHA_FORGE_SERVER'));
112
        $this->site_names = collect($this->sites)->pluck('name')->toArray();
113
        $this->default = $this->default();
114
    }
115
116
    /**
117
     * Default proposed value when asking.
118
     *
119
     */
120
    protected function default()
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
121
    {
122
        $current_value = fp_env('ACACHA_FORGE_SITE');
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $current_value.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
123
        if ($current_value) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $current_value.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
124
            $site_name = $this->getSiteName($this->sites, $current_value);
125
            if ($site_name) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $site_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
126
                return $site_name;
127
            }
128
        }
129
        return fp_env('ACACHA_FORGE_DOMAIN');
130
    }
131
132
    /**
133
     * Value.
134
     */
135
    protected function value()
136
    {
137
        $default=null;
0 ignored issues
show
Unused Code introduced by
$default is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
        $choices = $this->site_names ;
139
        if ($result = array_search($this->default, $this->site_names)) {
140
            $default = $result;
141
        } else {
142
            $newChoice = 'Create new Site using domain name: ' . fp_env('ACACHA_FORGE_DOMAIN');
143
            $choices = array_merge([$newChoice], $this->site_names);
144
            $default = array_search($newChoice, $this->site_names);
145
        }
146
147
        $site_name = $this->choice($this->questionText(), $choices, $default);
148
        $site_id = $this->getSiteId($this->sites, $site_name);
149
        if (! $site_id) {
150
            $this->call('publish:create_site');
151
            return fp_env('ACACHA_FORGE_SITE');
152
        } else {
153
            $this->info("Site ($site_name) already exists on Laravel Forge with site_id: $site_id");
154
            return $site_id;
155
        }
156
    }
157
}
158