Failed Conditions
Push — master ( ebb1ea...d49c03 )
by Bernhard
06:05
created

InstallationParams::validateParameterValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Api\Installation;
13
14
use Puli\Manager\Api\Asset\AssetMapping;
15
use Puli\Manager\Api\Installer\InstallerDescriptor;
16
use Puli\Manager\Api\Installer\ResourceInstaller;
17
use Puli\Manager\Api\Installer\Validation\ConstraintViolation;
18
use Puli\Manager\Api\Installer\Validation\InstallerParameterValidator;
19
use Puli\Manager\Api\Server\Server;
20
use Puli\Repository\Api\Resource\PuliResource;
21
use Puli\Repository\Api\ResourceCollection;
22
use Webmozart\Glob\Glob;
23
use Webmozart\PathUtil\Path;
24
25
/**
26
 * Contains all the necessary information to install resources on a server.
27
 *
28
 * @since  1.0
29
 *
30
 * @author Bernhard Schussek <[email protected]>
31
 */
32
class InstallationParams
33
{
34
    /**
35
     * @var ResourceInstaller
36
     */
37
    private $installer;
38
39
    /**
40
     * @var InstallerDescriptor
41
     */
42
    private $installerDescriptor;
43
44
    /**
45
     * @var ResourceCollection
46
     */
47
    private $resources;
48
49
    /**
50
     * @var AssetMapping
51
     */
52
    private $mapping;
53
54
    /**
55
     * @var Server
56
     */
57
    private $server;
58
59
    /**
60
     * @var string
61
     */
62
    private $rootDir;
63
64
    /**
65
     * @var array
66
     */
67
    private $parameterValues;
68
69
    /**
70
     * Creates the installation request.
71
     *
72
     * @param ResourceInstaller   $installer           The used resource installer.
73
     * @param InstallerDescriptor $installerDescriptor The descriptor of the
74
     *                                                 resource installer.
75
     * @param ResourceCollection  $resources           The resources to install.
76
     * @param AssetMapping        $mapping             The asset mapping.
77
     * @param Server              $server              The asset server.
78
     * @param string              $rootDir             The project's root directory.
79
     */
80 16
    public function __construct(ResourceInstaller $installer, InstallerDescriptor $installerDescriptor, ResourceCollection $resources, AssetMapping $mapping, Server $server, $rootDir)
81
    {
82 16
        $glob = $mapping->getGlob();
83 16
        $parameterValues = $server->getParameterValues();
84
85 16
        $this->validateParameterValues($parameterValues, $installerDescriptor);
86
87 14
        $this->installer = $installer;
88 14
        $this->installerDescriptor = $installerDescriptor;
89 14
        $this->resources = $resources;
90 14
        $this->mapping = $mapping;
91 14
        $this->server = $server;
92 14
        $this->rootDir = $rootDir;
93 14
        $this->basePath = Glob::isDynamic($glob) ? Glob::getBasePath($glob) : $glob;
0 ignored issues
show
Bug introduced by
The property basePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94 14
        $this->parameterValues = array_replace(
95 14
            $installerDescriptor->getParameterValues(),
96
            $parameterValues
97
        );
98 14
    }
99
100
    /**
101
     * Returns the used resource installer.
102
     *
103
     * @return ResourceInstaller The installer used to install the resources in
104
     *                           the server's document root.
105
     */
106 2
    public function getInstaller()
107
    {
108 2
        return $this->installer;
109
    }
110
111
    /**
112
     * Returns the descriptor of the installer.
113
     *
114
     * @return InstallerDescriptor The descriptor of the installer.
115
     */
116 1
    public function getInstallerDescriptor()
117
    {
118 1
        return $this->installerDescriptor;
119
    }
120
121
    /**
122
     * Returns the installed resources.
123
     *
124
     * @return ResourceCollection The installed resources.
125
     */
126 1
    public function getResources()
127
    {
128 1
        return $this->resources;
129
    }
130
131
    /**
132
     * Returns the asset mapping.
133
     *
134
     * @return AssetMapping The asset mapping.
135
     */
136
    public function getMapping()
137
    {
138
        return $this->mapping;
139
    }
140
141
    /**
142
     * Returns the root directory of the Puli project.
143
     *
144
     * @return string The project's root directory.
145
     */
146 8
    public function getRootDirectory()
147
    {
148 8
        return $this->rootDir;
149
    }
150
151
    /**
152
     * Returns the common base path of the installed resources.
153
     *
154
     * @return string The common base path of the installed resources.
155
     */
156 2
    public function getBasePath()
157
    {
158 2
        return $this->basePath;
159
    }
160
161
    /**
162
     * Returns the target server.
163
     *
164
     * @return Server The target server.
165
     */
166
    public function getServer()
167
    {
168
        return $this->server;
169
    }
170
171
    /**
172
     * Returns the document root of the server.
173
     *
174
     * This can be a directory name, a URL or any other string that can be
175
     * interpreted by the installer.
176
     *
177
     * @return string The document root.
178
     */
179 8
    public function getDocumentRoot()
180
    {
181 8
        return $this->server->getDocumentRoot();
182
    }
183
184
    /**
185
     * Returns the path where the resources are going to be installed.
186
     *
187
     * This is a path relative to the document root of the target server.
188
     *
189
     * @return string The server path.
190
     */
191 1
    public function getServerPath()
192
    {
193 1
        return $this->mapping->getServerPath();
194
    }
195
196
    /**
197
     * Returns the path where a resource is going to be installed.
198
     *
199
     * This is a path relative to the document root of the target server.
200
     *
201
     * @param PuliResource $resource The resource.
202
     *
203
     * @return string The server path.
204
     */
205 10
    public function getServerPathForResource(PuliResource $resource)
206
    {
207 10
        $relPath = Path::makeRelative($resource->getRepositoryPath(), $this->basePath);
208
209 10
        return '/'.trim($this->mapping->getServerPath().'/'.$relPath, '/');
210
    }
211
212
    /**
213
     * Returns the installer parameters.
214
     *
215
     * The result is a merge of the default parameter values of the installer
216
     * and the parameter values set for the server.
217
     *
218
     * @return array The installer parameters.
219
     */
220 8
    public function getParameterValues()
221
    {
222 8
        return $this->parameterValues;
223
    }
224
225 16
    private function validateParameterValues(array $parameterValues, InstallerDescriptor $installerDescriptor)
226
    {
227 16
        $validator = new InstallerParameterValidator();
228 16
        $violations = $validator->validate($parameterValues, $installerDescriptor);
229
230 16
        foreach ($violations as $violation) {
231 2
            switch ($violation->getCode()) {
232 2
                case ConstraintViolation::MISSING_PARAMETER:
233 1
                    throw NotInstallableException::missingParameter(
234 1
                        $violation->getParameterName(),
235 1
                        $violation->getInstallerName()
236
                    );
237 1
                case ConstraintViolation::NO_SUCH_PARAMETER:
238 1
                    throw NotInstallableException::noSuchParameter(
239 1
                        $violation->getParameterName(),
240 1
                        $violation->getInstallerName()
241
                    );
242
            }
243
        }
244 14
    }
245
}
246