Completed
Push — master ( 00bc8a...3b8fcd )
by Andrii
11:08
created

VhostController::getIps()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
1
<?php
2
3
/*
4
 * HiDev Nginx plugin
5
 *
6
 * @link      https://github.com/hiqdev/hidev-nginx
7
 * @package   hidev-nginx
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\nginx\controllers;
13
14
use hidev\modifiers\Sudo;
15
use Yii;
16
17
/**
18
 * Goal for Nginx virtual host.
19
 */
20
class VhostController extends \hidev\controllers\CommonController
21
{
22
    /**
23
     * @var NginxController
24
     */
25
    public $nginx;
26
27
    public $ssl;
28
29
    public $_aliases = [];
30
31
    protected $_sslDir;
32
    protected $_ips = [];
33
    protected $_localIps = [];
34
    protected $_domain;
35
    protected $_webDir;
36
    protected $_logDir;
37
    protected $_fpmSocket;
38
39
    public function actionChmodSsl()
40
    {
41
        $dir = $this->getSslDir();
42
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
43
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
44
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
45
    }
46
47 1
    public function renderConf()
48
    {
49 1
        return $this->nginx->render('default.twig', [
50 1
            'this' => $this,
51 1
        ]);
52
    }
53
54 1
    public function setDomain($value)
55
    {
56 1
        $this->_domain = $value;
57 1
    }
58
59 1
    public function getDomain()
60
    {
61 1
        if ($this->_domain === null || $this->_domain === 'default') {
62 1
            $this->_domain = $this->takePackage()->name;
63 1
        }
64
65 1
        return $this->_domain;
66
    }
67
68 1
    public function setIps($value)
69
    {
70 1
        $this->_ips = $value;
71 1
    }
72
73 1
    public function getIps()
74
    {
75 1
        if ($this->_ips === null || $this->_ips === []) {
76 1
            $this->_ips = $this->findIps();
77 1
        }
78
79 1
        return $this->_ips;
80
    }
81
82 1
    public function findIps()
83
    {
84 1
        return [gethostbyname($this->getDomain()) ?: '127.0.0.1'];
85
    }
86
87 1
    public function setLocalIps($value)
88
    {
89 1
        $this->_localIps = $value;
90 1
    }
91
92 1
    public function getLocalIps()
93
    {
94 1
        if ($this->_localIps === null || $this->_localIps === []) {
95 1
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
96 1
        }
97
98 1
        return $this->_localIps;
99
    }
100
101 1
    public function setWebDir($value)
102
    {
103 1
        $this->_webDir = Yii::getAlias($value);
104 1
    }
105
106 1
    public function getWebDir()
107
    {
108 1
        if ($this->_webDir === null) {
109
            $this->_webDir = $this->takeGoal('start')->buildRootPath('web');
110
        }
111
112 1
        return $this->_webDir;
113
    }
114
115 1
    public function setLogDir($value)
116
    {
117 1
        $this->_logDir = Yii::getAlias($value);
118 1
    }
119
120 1
    public function getLogDir()
121
    {
122 1
        if ($this->_logDir === null) {
123 1
            $this->_logDir = $this->nginx->getLogDir();
124 1
        }
125
126 1
        return $this->_logDir;
127
    }
128
129 1
    public function setFpmSocket($value)
130
    {
131 1
        $this->_fpmSocket = $value;
132 1
    }
133
134 1
    public function getFpmSocket()
135
    {
136 1
        if ($this->_fpmSocket === null) {
137 1
            $this->_fpmSocket = $this->nginx->getFpmSocket();
138 1
        }
139
140 1
        return $this->_fpmSocket;
141
    }
142
143 1
    public function setSslDir($value)
144
    {
145 1
        $this->_sslDir = Yii::getAlias($value);
146 1
        if ($this->_sslDir[0] !== '/') {
147
            $this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir);
148
        }
149 1
    }
150
151 1
    public function getSslDir()
152
    {
153 1
        if ($this->_sslDir === null) {
154
            $this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl');
155
        }
156
157 1
        return $this->_sslDir;
158
    }
159
160
    public function setAliases($aliases)
161
    {
162
        if (!is_array($aliases)) {
163
            $aliases = preg_split('/[\s,]+/', trim($aliases));
164
        }
165
        $this->_aliases = $aliases;
166
    }
167
168 1
    public function getAliases()
169
    {
170 1
        return $this->_aliases;
171
    }
172
173 1
    public function getDomains()
174
    {
175 1
        $domains = $this->getAliases();
176 1
        array_unshift($domains, $this->getDomain());
177
178 1
        return $domains;
179
    }
180
181 1
    public function getServerName()
182
    {
183 1
        return implode(' ', $this->getDomains());
184
    }
185
}
186