PublishDeploymentScriptWithHooks::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\ChecksEnv;
6
use Acacha\ForgePublish\Commands\Traits\DiesIfEnvVariableIsnotInstalled;
7
use Acacha\ForgePublish\Compiler\RCFileCompiler;
8
use GuzzleHttp\Client;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\File;
11
12
/**
13
 * Class PublishDeploymentScriptWithHooks.
14
 *
15
 * @package Acacha\ForgePublish\Commands
16
 */
17
class PublishDeploymentScriptWithHooks extends Command
18
{
19
    use ChecksEnv, DiesIfEnvVariableIsnotInstalled;
20
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'publish:deployment_script_with_hooks {--domain=}';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Install deployment script with hooks';
34
35
    /**
36
     * Domain.
37
     *
38
     * @var string
39
     */
40
    protected $domain;
41
42
    /**
43
     * Compiler for llumrc file.
44
     *
45
     * @var RCFileCompiler
46
     */
47
    protected $compiler;
48
49
    /**
50
     * Constructor.
51
     *
52
     */
53
    public function __construct(RCFileCompiler $compiler)
54
    {
55
        parent::__construct();
56
        $this->compiler = $compiler;
57
    }
58
59
    /**
60
     * Execute the console command.
61
     *
62
     */
63
    public function handle()
64
    {
65
        $this->abortCommandExecution();
66
        $this->info('Installing a deployment script with hooks...');
67
        $data = [
68
            "ACACHA_FORGE_DOMAIN" => $this->domain,
69
            "PHP_VERSION_FPM" => 'php7.1-fpm',
70
        ];
71
        $content = $this->compiler->compile(
72
            file_get_contents($this->getStubPath()),
73
            $data);
74
        $tmp_file = tempnam(sys_get_temp_dir(), 'deploy_script_with_hooks');
75
        file_put_contents($tmp_file, $content);
76
        $this->call('publish:deployment_script', [
77
            'file' => $tmp_file
78
        ]);
79
    }
80
81
    /**
82
     * Get stub path.
83
     *
84
     * @return string
85
     */
86
    protected function getStubPath()
87
    {
88
        return __DIR__ . '/stubs/deployment_script_default.sh';
89
    }
90
91
92
93
    /**
94
     * Update deployment script.
95
     */
96 View Code Duplication
    protected function updateDeploymentScript()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $this->info('Updating deployment script for site ' . $this->site . '...');
99
100
        $this->url = $this->obtainAPIURLEndpoint('update_deployment_script_uri');
101
102
        if (! File::exists($this->file)) {
103
            $this->error("File " . $this->file . " doesn't exists");
104
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method updateDeploymentScript() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
105
        }
106
107
        $this->http->put($this->url, [
108
                'form_params' => [
109
                    'content' => file_get_contents($this->file)
110
                ],
111
                'headers' => [
112
                    'X-Requested-With' => 'XMLHttpRequest',
113
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
114
                ]
115
            ]
116
        );
117
    }
118
119
    /**
120
     * Obtain API URL endpoint.
121
     *
122
     * @return string
123
     */
124
    protected function obtainAPIURLEndpoint($type)
125
    {
126
        $uri = str_replace('{forgeserver}', $this->server, config('forge-publish.' . $type));
127
        $uri = str_replace('{forgesite}', $this->site, $uri);
128
        return config('forge-publish.url') . $uri;
129
    }
130
131
    /**
132
     * Abort command execution.
133
     */
134
    protected function abortCommandExecution()
135
    {
136
        $this->domain = $this->checkEnv('domain', 'ACACHA_FORGE_DOMAIN');
137
    }
138
}
139