Completed
Pull Request — master (#75)
by Rok
03:19
created

CloudEnvironment::setSiteName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Acquia\Cloud\Environment;
4
5
use Acquia\Environment\Environment;
6
use Acquia\Json\Json;
7
8
/**
9
 * @see https://docs.acquia.com/cloud/configure/env-variable
10
 */
11
class CloudEnvironment extends Environment implements CloudEnvironmentInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $sitegroup;
17
18
    /**
19
     * @var string
20
     */
21
    private $sitename;
22
23
    /**
24
     * @var string
25
     */
26
    private $currentregion;
27
28
    /**
29
     * @var string
30
     */
31
    private $filepath;
32
33
    /**
34
     * @var array
35
     */
36 78
    private $creds;
37
38 78
    /**
39 78
     * Acquia Cloud variables may be set in settings.inc after PHP init,
40 63
     * so make sure that we are loading them.
41
     *
42
     * @param string $key
43 63
     * @return string The value of the environment variable or false if not found
44
     * @see https://github.com/acquia/acquia-sdk-php/pull/58#issuecomment-45167451
45
     */
46 63
    protected function getenv($key)
0 ignored issues
show
Coding Style introduced by
getenv uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getenv uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
47 78
    {
48
        $value = getenv($key);
49
        if ($value === false) {
50
            if (isset($_ENV[$key])) {
51
                $value = $_ENV[$key];
52
            }
53 78
            if (isset($_SERVER[$key])) {
54
                $value = $_SERVER[$key];
55 78
            }
56 78
        }
57
        return $value;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62 6
     */
63
    public function init()
64 6
    {
65
        $environment = $this->getenv('AH_SITE_ENVIRONMENT');
66
        return $environment ?: self::LOCAL;
67
    }
68
69
    /**
70 6
     * {@inheritDoc}
71
     */
72 6
    public function isAcquia()
73
    {
74
        return $this->getEnvironment() != self::LOCAL;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 3
    public function isProduction()
81
    {
82 3
        return (bool) $this->getenv('AH_PRODUCTION');
83 3
    }
84
85
    /**
86
     * @param string $sitegroup
87
     *
88
     * @return \Acquia\Cloud\Environment\CloudEnvironment
89
     */
90
    public function setSiteGroup($sitegroup)
91 15
    {
92
        $this->sitegroup = $sitegroup;
93 15
        return $this;
94 12
    }
95 12
96 3
    /**
97
     * @return string
98 9
     *
99 12
     * @throws \UnexpectedValueException
100
     */
101 View Code Duplication
    public function getSiteGroup()
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...
102
    {
103
        if (!isset($this->sitegroup)) {
104
            $this->sitegroup = $this->getenv('AH_SITE_GROUP');
105
            if (!$this->sitegroup) {
106
                throw new \UnexpectedValueException('Expecting environment variable AH_SITE_GROUP to be set');
107 21
            }
108
        }
109 21
        return $this->sitegroup;
110 21
    }
111
112
    /**
113
     * @param string $sitename
114
     *
115
     * @return \Acquia\Cloud\Environment\CloudEnvironment
116 21
     */
117
    public function setSiteName($sitename)
118 21
    {
119 3
        $this->sitename = $sitename;
120 3
        return $this;
121 3
    }
122 21
123
    /**
124
     * @return string
125
     *
126
     * @throws \UnexpectedValueException
127
     */
128 View Code Duplication
    public function getSiteName()
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...
129
    {
130 15
        if (!isset($this->sitename)) {
131
            $this->sitename = $this->getenv('AH_SITE_NAME');
132 15
            if (!$this->sitename) {
133 15
                throw new \UnexpectedValueException('Expecting environment variable AH_SITE_NAME to be set');
134 15
            }
135 15
        }
136 15
        return $this->sitename;
137
    }
138
139
    /**
140
     * @param string $currentregion
141
     *
142
     * @return \Acquia\Cloud\Environment\CloudEnvironment
143
     */
144
    public function setCurrentRegion($currentregion)
145
    {
146
        $this->currentregion = $currentregion;
147
        return $this;
148
    }
149
150
    /**
151
     * @return string
152
     *
153
     * @throws \UnexpectedValueException
154
     */
155 View Code Duplication
    public function getCurrentRegion()
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...
156
    {
157
        if (!isset($this->currentregion)) {
158
            $this->currentregion = $this->getenv('AH_CURRENT_REGION');
159
            if (!$this->currentregion) {
160
                throw new \UnexpectedValueException('Expecting environment variable AH_CURRENT_REGION to be set');
161
            }
162
        }
163
        return $this->currentregion;
164
    }
165
166
    /**
167
     * @param string $filepath
168
     *
169
     * @return \Acquia\Cloud\Environment\CloudEnvironment
170
     */
171
    public function setCredentialsFilepath($filepath)
172
    {
173
        $this->filepath = $filepath;
174
        return $this;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getCredentialsFilepath()
181
    {
182
        if (!isset($this->filepath)) {
183
            $settingsDir = $this->getSiteGroup() . '.' . $this->getEnvironment();
184
            $this->filepath = '/var/www/site-php/' . $settingsDir . '/creds.json';
185
        }
186
        return $this->filepath;
187
    }
188
189
    /**
190
     * @return array
191
     *
192
     * @throws \RuntimeException
193
     */
194
    public function serviceCredentials()
195
    {
196
        if (!isset($this->creds)) {
197
            $filepath = $this->getCredentialsFilepath();
198
            $this->creds = Json::parseFile($filepath);
199
        }
200
        return $this->creds;
201
    }
202
}
203