Completed
Push — master ( a18f4a...b0c220 )
by Greg
01:31
created

SiteAliasManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 0
loc 195
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setReferenceData() 0 4 1
A setRoot() 0 5 1
A addSearchLocation() 0 5 1
A addSearchLocations() 0 7 2
A searchLocations() 0 4 1
A get() 0 12 3
A getSelf() 0 4 1
A setSelf() 0 6 1
A getAlias() 0 17 3
B getMultiple() 0 24 4
A listAllFilePaths() 0 4 1
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
/**
5
 * Site Alias manager
6
 */
7
class SiteAliasManager
8
{
9
    protected $aliasLoader;
10
    protected $selfAliasRecord;
11
    protected $specParser;
12
    protected $root = '';
13
14
    /**
15
     * Constructor for SiteAliasManager
16
     *
17
     * @param SiteAliasFileLoader|null $aliasLoader an alias loader
18
     */
19
    public function __construct($aliasLoader = null, $root = '')
20
    {
21
        $this->aliasLoader = $aliasLoader ?: new SiteAliasFileLoader();
22
        $this->specParser = new SiteSpecParser();
23
        $this->selfAliasRecord = new AliasRecord();
24
        $this->root = $root;
25
    }
26
27
    /**
28
     * Allow configuration data to be used in replacements in the alias file.
29
     */
30
    public function setReferenceData($data)
31
    {
32
        $this->aliasLoader->setReferenceData($data);
33
    }
34
35
    /**
36
     * Inject the root of the selected site
37
     *
38
     * @param string $root
39
     * @return $this
40
     */
41
    public function setRoot($root)
42
    {
43
        $this->root = $root;
44
        return $this;
45
    }
46
47
    /**
48
     * Add a search location to our site alias discovery object.
49
     *
50
     * @param string $path
51
     *
52
     * @return $this
53
     */
54
    public function addSearchLocation($path)
55
    {
56
        $this->aliasLoader->discovery()->addSearchLocation($path);
57
        return $this;
58
    }
59
60
    /**
61
     * Add search locations to our site alias discovery object.
62
     *
63
     * @param array $paths Any path provided in --alias-path option
64
     *   or drush.path.alias-path configuration item.
65
     *
66
     * @return $this
67
     */
68
    public function addSearchLocations(array $paths)
69
    {
70
        foreach ($paths as $path) {
71
            $this->aliasLoader->discovery()->addSearchLocation($path);
72
        }
73
        return $this;
74
    }
75
76
    /**
77
     * Return all of the paths where alias files may be found.
78
     * @return string[]
79
     */
80
    public function searchLocations()
81
    {
82
        return $this->aliasLoader->discovery()->searchLocations();
83
    }
84
85
    /**
86
     * Get an alias record by name, or convert a site specification
87
     * into an alias record via the site alias spec parser. If a
88
     * simple alias name is provided (e.g. '@alias'), it is interpreted
89
     * as a sitename, and the default environment for that site is returned.
90
     *
91
     * @param string $name Alias name or site specification
92
     *
93
     * @return AliasRecord|false
94
     */
95
    public function get($name)
96
    {
97
        if (SiteAliasName::isAliasName($name)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Consolidation\SiteAlias...ame::isAliasName($name) of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
98
            return $this->getAlias($name);
99
        }
100
101
        if ($this->specParser->validSiteSpec($name)) {
102
            return new AliasRecord($this->specParser->parse($name, $this->root), $name);
103
        }
104
105
        return false;
106
    }
107
108
    /**
109
     * Get the '@self' alias record.
110
     *
111
     * @return AliasRecord
112
     */
113
    public function getSelf()
114
    {
115
        return $this->selfAliasRecord;
116
    }
117
118
    /**
119
     * Force-set the current @self alias.
120
     *
121
     * @param AliasRecord $selfAliasRecord
122
     * @return $this
123
     */
124
    public function setSelf(AliasRecord $selfAliasRecord)
125
    {
126
        $this->selfAliasRecord = $selfAliasRecord;
127
        $this->setRoot($selfAliasRecord->localRoot());
128
        return $this;
129
    }
130
131
    /**
132
     * Get an alias record from a name. Does not accept site specifications.
133
     *
134
     * @param string $aliasName alias name
135
     *
136
     * @return AliasRecord
137
     */
138
    public function getAlias($aliasName)
139
    {
140
        $aliasName = SiteAliasName::parse($aliasName);
141
142
        if ($aliasName->isSelf()) {
143
            return $this->getSelf();
144
        }
145
146
        if ($aliasName->isNone()) {
147
            return new AliasRecord([], '@none');
148
        }
149
150
        // Search through all search locations, load
151
        // matching and potentially-matching alias files,
152
        // and return the alias matching the provided name.
153
        return $this->aliasLoader->load($aliasName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->aliasLoader->load($aliasName); of type Consolidation\SiteAlias\AliasRecord|false adds false to the return on line 153 which is incompatible with the return type documented by Consolidation\SiteAlias\SiteAliasManager::getAlias of type Consolidation\SiteAlias\AliasRecord. It seems like you forgot to handle an error condition.
Loading history...
154
    }
155
156
    /**
157
     * Given a simple alias name, e.g. '@alias', returns all of the
158
     * environments in the specified site.
159
     *
160
     * If the provided name is a site specification et. al.,
161
     * then this method will return 'false'.
162
     *
163
     * @param string $name Alias name or site specification
164
     * @return AliasRecord[]|false
165
     */
166
    public function getMultiple($name)
167
    {
168
        if (empty($name)) {
169
            return $this->aliasLoader->loadAll();
170
        }
171
172
        if (!SiteAliasName::isAliasName($name)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Consolidation\SiteAlias...ame::isAliasName($name) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
173
            return false;
174
        }
175
176
        // Trim off the '@' and load all that match
177
        $result = $this->aliasLoader->loadMultiple(ltrim($name, '@'));
178
179
        // Special checking for @self
180
        if ($name == '@self') {
181
            $self = $this->getSelf();
182
            $result = array_merge(
183
                ['@self' => $self],
184
                $result
185
            );
186
        }
187
188
        return $result;
189
    }
190
191
    /**
192
     * Return the paths to all alias files in all search locations known
193
     * to the alias manager.
194
     *
195
     * @return string[]
196
     */
197
    public function listAllFilePaths()
198
    {
199
        return $this->aliasLoader->listAll();
200
    }
201
}
202