PublishCreateSite   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 235
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 7

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 8 3
A getForgeServer() 0 4 1
A getDomain() 0 4 1
A getProjectType() 0 4 1
A getSiteDirectory() 0 4 1
A handle() 0 14 2
A obtainFields() 0 7 1
A createSiteOnForge() 0 21 2
A obtainApiEndPointURL() 0 5 1
A abortCommandExecution() 0 6 1
A siteIsAlreadyCreated() 0 9 2
1
<?php
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment;
6
use Acacha\ForgePublish\Commands\Traits\ShowsErrorResponse;
7
use Acacha\ForgePublish\Commands\Traits\DiesIfEnvVariableIsnotInstalled;
8
use Acacha\ForgePublish\Commands\Traits\DiesIfNoEnvFileExists;
9
use Acacha\ForgePublish\Commands\Traits\WaitsForSite;
10
use GuzzleHttp\Client;
11
use Illuminate\Console\Command;
12
13
/**
14
 * Class PublishCreateSite.
15
 *
16
 * @package Acacha\ForgePublish\Commands
17
 */
18
class PublishCreateSite extends Command
19
{
20
    use WaitsForSite, InteractsWithEnvironment, ShowsErrorResponse, DiesIfNoEnvFileExists, DiesIfEnvVariableIsnotInstalled;
21
22
    /**
23
     * Project type.
24
     *
25
     * @var String
26
     */
27
    protected $project_type;
28
29
    /**
30
     * Site directory.
31
     *
32
     * @var String
33
     */
34
    protected $site_directory;
35
36
    /**
37
     * Domain.
38
     *
39
     * @var String
40
     */
41
    protected $domain;
42
43
44
    /**
45
     * The name and signature of the console command.
46
     *
47
     * @var string
48
     */
49
    protected $signature = 'publish:create_site {server?} {domain?} {project_type?} {site_directory?}';
50
51
    /**
52
     * The console command description.
53
     *
54
     * @var string
55
     */
56
    protected $description = 'Create site on forge for the current user';
57
58
    /**
59
     * Guzzle Http client
60
     *
61
     * @var Client
62
     */
63
    protected $http;
64
65
    /**
66
     * Laravel Forge site name.
67
     *
68
     * @var array
69
     */
70
    protected $sites;
71
72
    /**
73
     * Laravel Forge site name.
74
     *
75
     * @var String
76
     */
77
    protected $site;
78
79
    /**
80
     * Laravel Forge server.
81
     *
82
     * @var String
83
     */
84
    protected $server;
85
86
    /**
87
     * Laravel Forge site id.
88
     *
89
     * @var integer
90
     */
91
    protected $site_id;
92
93
    /**
94
     * API endpoint URL
95
     *
96
     * @var String
97
     */
98
    protected $url;
99
100
    /**
101
     * PublishCreateSite constructor.
102
     *
103
     * @param Client $client
0 ignored issues
show
Bug introduced by
There is no parameter named $client. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     */
105
    public function __construct(Client $http)
106
    {
107
        parent::__construct();
108
        $this->http = $http;
109
    }
110
111
    /**
112
     * Get Value.
113
     *
114
     * @return array|int|null|string
115
     */
116
    protected function getValue($value, $env_var, $command)
117
    {
118
        if ($this->argument($value)) {
119
            return $this->argument($value);
120
        } else {
121
            return fp_env($env_var) ? fp_env($env_var) : $this->call("publish:$command");
122
        }
123
    }
124
125
    /**
126
     * Get Forge server.
127
     *
128
     * @return array|int|null|string
129
     */
130
    protected function getForgeServer()
131
    {
132
        return $this->getValue('server', 'ACACHA_FORGE_SERVER', 'server');
133
    }
134
135
    /**
136
     * Get Domain.
137
     *
138
     * @return array|int|null|string
139
     */
140
    protected function getDomain()
141
    {
142
        return $this->getValue('domain', 'ACACHA_FORGE_DOMAIN', 'domain');
143
    }
144
145
    /**
146
     * Get project type.
147
     *
148
     * @return array|int|null|string
149
     */
150
    protected function getProjectType()
151
    {
152
        return $this->getValue('project_type', 'ACACHA_FORGE_PROJECT_TYPE', 'project_type');
153
    }
154
155
    /**
156
     * Get site directory.
157
     *
158
     * @return array|int|null|string
159
     */
160
    protected function getSiteDirectory()
161
    {
162
        return $this->getValue('site_directory', 'ACACHA_FORGE_SITE_DIRECTORY', 'site_directory');
163
    }
164
165
    /**
166
     * Execute the console command.
167
     *
168
     */
169
    public function handle()
170
    {
171
        $this->abortCommandExecution();
172
        $this->obtainFields();
173
        if ($this->siteIsAlreadyCreated()) {
174
            $this->info("Site $this->site ($this->site_id) is already created. Skipping...");
175
            return;
176
        }
177
        $this->url = $this->obtainApiEndPointURL();
178
        $site = $this->createSiteOnForge();
179
        $this->waitForSite($site['id']);
180
        $this->addValueToEnv('ACACHA_FORGE_SITE', $site['id']);
181
        $this->info('The site has been added to Forge');
182
    }
183
184
185
    /**
186
     * Obtain fields.
187
     */
188
    protected function obtainFields()
189
    {
190
        $this->server = $this->getForgeServer();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getForgeServer() can also be of type array or integer. However, the property $server is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
191
        $this->domain = $this->getDomain();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getDomain() can also be of type array or integer. However, the property $domain is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
192
        $this->project_type = $this->getProjectType();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getProjectType() can also be of type array or integer. However, the property $project_type is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
193
        $this->site_directory = $this->getSiteDirectory();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getSiteDirectory() can also be of type array or integer. However, the property $site_directory is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
194
    }
195
196
    /**
197
     * Create site on Forge.
198
     */
199
    protected function createSiteOnForge()
200
    {
201
        try {
202
            $this->info('Creating site ' . $this->domain . ' on server ' . $this->server);
203
            $response =  $this->http->post($this->url, [
204
                'form_params' => [
205
                    'domain' => $this->domain,
206
                    'project_type' => $this->project_type,
207
                    'directory' => $this->site_directory
208
                ],
209
                'headers' => [
210
                    'X-Requested-With' => 'XMLHttpRequest',
211
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
212
                ]
213
            ]);
214
            return json_decode($response->getBody(), true);
215
        } catch (\Exception $e) {
216
            $this->showErrorAndDie($e);
217
            return null;
218
        }
219
    }
220
221
    /**
222
     * Obtain API endpoint URL.
223
     */
224
    protected function obtainApiEndPointURL()
225
    {
226
        $uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_sites_uri'));
227
        return config('forge-publish.url') . $uri;
228
    }
229
230
    /**
231
     * Check if command have to be skipped.
232
     */
233
    protected function abortCommandExecution()
234
    {
235
        $this->dieIfNoEnvFileIsFound();
236
        $this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
237
        $this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_SERVER');
238
    }
239
240
    /**
241
     * Is site already created?
242
     */
243
    protected function siteIsAlreadyCreated()
244
    {
245
        $this->sites = $this->fetchSites(fp_env('ACACHA_FORGE_SERVER'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->fetchSites(fp_env('ACACHA_FORGE_SERVER')) of type * is incompatible with the declared type array of property $sites.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
246
        $this->site = fp_env('ACACHA_FORGE_SITE');
247
        if (in_array($this->site, collect($this->sites)->pluck('id')->toArray())) {
248
            return true;
249
        }
250
        return in_array($this->domain, collect($this->sites)->pluck('name')->toArray());
251
    }
252
}
253