PublishDNS::handle()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 0
dl 0
loc 26
rs 8.5806
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\ChecksForRootPermission;
7
use Acacha\ForgePublish\Commands\Traits\DNSisAlreadyConfigured;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Facades\File;
10
11
/**
12
 * Class PublishDNS.
13
 *
14
 * @package Acacha\ForgePublish\Commands
15
 */
16
class PublishDNS extends Command
17
{
18
    use ChecksForRootPermission, DNSisAlreadyConfigured, ChecksEnv;
19
20
    /**
21
     * The ip address.
22
     *
23
     * @var string
24
     */
25
    protected $ip;
26
27
    /**
28
     * Is DNS already resolved.
29
     *
30
     * @var string
31
     */
32
    protected $dnsAlreadyResolved = false;
33
34
    /**
35
     * The domain name.
36
     *
37
     * @var string
38
     */
39
    protected $domain;
40
41
    /**
42
     * Constant to /etc/hosts file
43
     */
44
    const ETC_HOSTS = '/etc/hosts';
45
46
    /**
47
     * The name and signature of the console command.
48
     *
49
     * @var string
50
     */
51
    protected $signature = 'publish:dns {ip?} {domain?} {--type=}';
52
53
    /**
54
     * The console command description.
55
     *
56
     * @var string
57
     */
58
    protected $description = 'Check DNS configuration';
59
60
    /**
61
     * Execute the console command.
62
     *
63
     */
64
    public function handle()
65
    {
66
        $this->info('Checking DNS configuration');
67
        $this->abortCommandExecution();
68
69
        if ($this->dnsAlreadyResolved) {
70
            $this->info("DNS resolution is ok. ");
71
            return;
72
        }
73
74
        $this->info("DNS resolution is not correct. Let me help you configure it...");
75
76
        $type = $this->option('type') ?
77
            $this->option('type') :
78
            $this->choice('Which system do you want to use?', ['hosts'], 0);
79
80
        if ($type != 'hosts') {
81
            //TODO Support Other services OpenDNS/Hover.com? DNS service wiht API
82
            // https://laracasts.com/series/server-management-with-forge/episodes/8
83
            $this->error('Type not supported');
84
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() 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...
85
        }
86
87
        $this->addEntryToEtcHostsFile($this->domain, $this->ip);
88
        $this->info('File ' . self::ETC_HOSTS . ' configured ok');
89
    }
90
91
    /**
92
     * Add entry to etc/hosts file.
93
     *
94
     * @param $domain
95
     * @param $ip
96
     */
97
    protected function addEntryToEtcHostsFile($domain, $ip)
98
    {
99
        $content = "\n# Forge server\n$ip $domain\n";
100
        File::append(self::ETC_HOSTS, $content);
101
    }
102
103
    /**
104
     * Abort command execution.
105
     */
106
    protected function abortCommandExecution()
107
    {
108
        $this->domain = $this->checkEnv('domain', 'ACACHA_FORGE_DOMAIN', 'argument');
109
        $this->ip = $this->checkEnv('ip', 'ACACHA_FORGE_IP_ADDRESS', 'argument');
110
111
        if ($this->dnsResolutionIsOk()) {
112
            return ;
113
        }
114
115
        $this->checkForRootPermission();
116
    }
117
}
118