Completed
Push — master ( ea31e5...cf0b1d )
by Andrii
02:13
created

VhostController::setAliases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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 $_ip;
33
    protected $_domain;
34
    protected $_webDir;
35
    protected $_logDir;
36
    protected $_fpmSocket;
37
38
    public function actionChmodSsl()
39
    {
40
        $dir = $this->getSslDir();
41
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
42
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
43
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
44
    }
45
46
    public function renderConf()
47
    {
48
        return $this->nginx->render('default.twig', [
49
            'this' => $this,
50
        ]);
51
    }
52
53
    public function setDomain($value)
54
    {
55
        $this->_domain = $value;
56
    }
57
58
    public function getDomain()
59
    {
60
        if ($this->_domain === null || $this->_domain === 'default') {
61
            $this->_domain = $this->takePackage()->name;
62
        }
63
64
        return $this->_domain;
65
    }
66
67
    public function setIp($value)
68
    {
69
        $this->_ip = $value;
70
    }
71
72
    public function getIp()
73
    {
74
        if ($this->_ip === null) {
75
            $this->_ip = $this->findIp();
76
        }
77
78
        return $this->_ip;
79
    }
80
81
    public function findIp()
82
    {
83
        return gethostbyname($this->getDomain()) ?: '127.0.0.1';
84
    }
85
86
    public function setWebDir($value)
87
    {
88
        $this->_webDir = Yii::getAlias($value);
89
    }
90
91
    public function getWebDir()
92
    {
93
        if ($this->_webDir === null) {
94
            $this->_webDir = $this->takeGoal('start')->buildRootPath('web');
95
        }
96
97
        return $this->_webDir;
98
    }
99
100
    public function setLogDir($value)
101
    {
102
        $this->_logDir = Yii::getAlias($value);
103
    }
104
105
    public function getLogDir()
106
    {
107
        if ($this->_logDir === null) {
108
            $this->_logDir = $this->nginx->getLogDir();
109
        }
110
111
        return $this->_logDir;
112
    }
113
114
    public function setFpmSocket($value)
115
    {
116
        $this->_fpmSocket = $value;
117
    }
118
119
    public function getFpmSocket()
120
    {
121
        if ($this->_fpmSocket === null) {
122
            $this->_fpmSocket = $this->nginx->getFpmSocket();
123
        }
124
125
        return $this->_fpmSocket;
126
    }
127
128
    public function setSslDir($value)
129
    {
130
        $this->_sslDir = Yii::getAlias($value);
131
        if ($this->_sslDir[0] !== '/') {
132
            $this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir);
133
        }
134
    }
135
136
    public function getSslDir()
137
    {
138
        if ($this->_sslDir === null) {
139
            $this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl');
140
        }
141
142
        return $this->_sslDir;
143
    }
144
    public function setAliases($aliases)
145
    {
146
        if (!is_array($aliases)) {
147
            $aliases = preg_split('/[\s,]+/', trim($aliases));
148
        }
149
        $this->_aliases = $aliases;
150
    }
151
152
    public function getAliases()
153
    {
154
        return $this->_aliases;
155
    }
156
157
    public function getDomains()
158
    {
159
        $domains = $this->getAliases();
160
        array_unshift($domains, $this->getDomain());
161
162
        return $domains;
163
    }
164
165
    public function getServerName()
166
    {
167
        return implode(' ', $this->getDomains());
168
    }
169
}
170