LocalEnvironment::addDatabaseCredentials()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 6
crap 2
1
<?php
2
3
namespace Acquia\Cloud\Environment;
4
5
use Acquia\Environment\Environment;
6
7
/**
8
 * @see https://docs.acquia.com/cloud/configure/env-variable
9
 */
10
class LocalEnvironment extends Environment implements CloudEnvironmentInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $creds = array();
16
17
    /**
18
     * @var string
19
     */
20
    protected $sitegroup;
21
22
    /**
23
     * @param string $sitegroup
24
     */
25 18
    public function __construct($sitegroup)
26
    {
27 18
        $this->sitegroup = $sitegroup;
28 18
        parent::__construct();
29 18
    }
30
31
    /**
32
     * Calculates and returns the environment.
33
     *
34
     * @return string
35
     */
36 18
    protected function init()
37
    {
38 18
        return self::LOCAL;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 3
    public function isAcquia()
45
    {
46 3
        return false;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 3
    public function setSiteGroup($sitegroup)
53
    {
54 3
        $this->sitegroup = $sitegroup;
55 3
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Acquia\Cloud\Environment\LocalEnvironment) is incompatible with the return type declared by the interface Acquia\Cloud\Environment...Interface::setSiteGroup of type Acquia\Cloud\Environment\CloudEnvironment.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 6
    public function getSiteGroup()
62
    {
63 6
        return $this->sitegroup;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 6
    public function serviceCredentials()
70
    {
71 6
        return $this->creds;
72
    }
73
74
    /**
75
     * Adds credentials to a database server.
76
     *
77
     * @param string $acquiaDbName
78
     * @param string $localDbName
79
     * @param string $username
80
     * @param string|null $password
81
     * @param string $host
82
     * @param integer $port
83
     *
84
     * @return \Acquia\Cloud\Environment\LocalEnvironment
85
     */
86 3
    public function addDatabaseCredentials($acquiaDbName, $localDbName, $username, $password = null, $host = 'localhost', $port = 3306)
87
    {
88 3
        $connString = $username;
89 3
        if ($password !== null) {
90 3
            $connString . ':' . $password;
91 3
        }
92
93 3
        $this->creds['databases'][$acquiaDbName] = array(
94 3
            'id'  => '1',
95 3
            'role' => $this->sitegroup, // Is this right? For local doesn't really matter.
96 3
            'name' => $localDbName,
97 3
            'user' => $username,
98 3
            'pass' => $password,
99
            'db_url_ha' => array(
100 3
                $host => "mysqli://$connString@$host:$port/mysiteprod"
101 3
            ),
102 3
            'db_cluster_id' => '1',
103 3
            'port' => $port,
104
        );
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Adds credentials to a Memcached server.
111
     *
112
     * @param string $host
113
     * @param string $port
114
     *
115
     * @return \Acquia\Cloud\Environment\LocalEnvironment
116
     */
117 3
    public function addMemcacheCredentials($host, $port)
118
    {
119 3
        $this->creds['memcached_servers'][] = $host . ':' . $port;
120 3
        return $this;
121
    }
122
}
123