PublishCommandHandler::printMappingTable()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.439
cc 5
eloc 15
nc 12
nop 3
crap 5
1
<?php
2
3
/*
4
 * This file is part of the puli/asset-plugin 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\Cli\Handler;
13
14
use Puli\Manager\Api\Asset\AssetManager;
15
use Puli\Manager\Api\Asset\AssetMapping;
16
use Puli\Manager\Api\Installation\InstallationManager;
17
use Puli\Manager\Api\Installation\InstallationParams;
18
use Puli\Manager\Api\Server\Server;
19
use Puli\Manager\Api\Server\ServerManager;
20
use RuntimeException;
21
use Webmozart\Console\Api\Args\Args;
22
use Webmozart\Console\Api\IO\IO;
23
use Webmozart\Console\UI\Component\Table;
24
use Webmozart\Console\UI\Style\TableStyle;
25
use Webmozart\Expression\Expr;
26
use Webmozart\PathUtil\Path;
27
28
/**
29
 * @since  1.0
30
 *
31
 * @author Bernhard Schussek <[email protected]>
32
 */
33
class PublishCommandHandler
34
{
35
    /**
36
     * @var AssetManager
37
     */
38
    private $assetManager;
39
40
    /**
41
     * @var InstallationManager
42
     */
43
    private $installationManager;
44
45
    /**
46
     * @var ServerManager
47
     */
48
    private $serverManager;
49
50
    /**
51
     * @var string
52
     */
53
    private $currentPath = '/';
54
55 21
    public function __construct(AssetManager $assetManager, InstallationManager $installationManager, ServerManager $serveRManager)
56
    {
57 21
        $this->assetManager = $assetManager;
58 21
        $this->installationManager = $installationManager;
59 21
        $this->serverManager = $serveRManager;
60 21
    }
61
62 4
    public function handleList(Args $args, IO $io)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        /** @var AssetMapping[][] $mappingsByServer */
65 4
        $mappingsByServer = array();
66
67
        /** @var Server[] $servers */
68 4
        $servers = array();
69 4
        $nonExistingServers = array();
70
71
        // Assemble mappings and validate servers
72 4
        foreach ($this->assetManager->getAssetMappings() as $mapping) {
73 3
            $serverName = $mapping->getServerName();
74
75 3
            if (!isset($mappingsByServer[$serverName])) {
76 3
                $mappingsByServer[$serverName] = array();
77
78 3
                if ($this->serverManager->hasServer($serverName)) {
79 2
                    $servers[$serverName] = $this->serverManager->getServer($serverName);
80
                } else {
81 2
                    $nonExistingServers[$serverName] = true;
82
                }
83
            }
84
85 3
            $mappingsByServer[$serverName][] = $mapping;
86
        }
87
88 4
        if (empty($mappingsByServer)) {
89 1
            $io->writeLine('No public resources. Use "puli publish <path> <server>" to publish resources.');
90
91 1
            return 0;
92
        }
93
94 3
        if (count($servers) > 0) {
95 2
            $io->writeLine('The following resources are published:');
96 2
            $io->writeLine('');
97
98 2
            foreach ($servers as $serverName => $server) {
99 2
                $serverTitle = 'Server <bu>'.$serverName.'</bu>';
100
101 2
                $io->writeLine(sprintf('    <b>%s</b>', $serverTitle));
102 2
                $io->writeLine(sprintf('    Location:   <c2>%s</c2>', $server->getDocumentRoot()));
103 2
                $io->writeLine(sprintf('    Installer:  %s', $server->getInstallerName()));
104 2
                $io->writeLine(sprintf('    URL Format: <c1>%s</c1>', $server->getUrlFormat()));
105 2
                $io->writeLine('');
106
107 2
                $this->printMappingTable($io, $mappingsByServer[$serverName]);
108 2
                $io->writeLine('');
109
            }
110
111 2
            $io->writeLine('Use "puli publish --install" to install the resources on your servers.');
112
        }
113
114 3
        if (count($servers) > 0 && count($nonExistingServers) > 0) {
115 1
            $io->writeLine('');
116
        }
117
118 3
        if (count($nonExistingServers) > 0) {
119 2
            $io->writeLine('The following public resources are disabled since their server does not exist:');
120 2
            $io->writeLine('');
121
122 2
            foreach ($nonExistingServers as $serverName => $_) {
123 2
                $io->writeLine(sprintf('    <b>Server <bu>%s</bu></b>', $serverName));
124 2
                $io->writeLine('');
125
126 2
                $this->printMappingTable($io, $mappingsByServer[$serverName], false);
127 2
                $io->writeLine('');
128
            }
129
130 2
            $io->writeLine('Use "puli server add <name> <document-root>" to add a server.');
131
        }
132
133 3
        return 0;
134
    }
135
136 5 View Code Duplication
    public function handleAdd(Args $args)
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...
137
    {
138 5
        $flags = $args->isOptionSet('force') ? AssetManager::IGNORE_SERVER_NOT_FOUND : 0;
139 5
        $path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath);
140
141 5
        $this->assetManager->addRootAssetMapping(new AssetMapping(
142
            $path,
143 5
            $args->getArgument('server'),
144 5
            $args->getArgument('server-path')
145
        ), $flags);
146
147 5
        return 0;
148
    }
149
150 5
    public function handleUpdate(Args $args)
151
    {
152 5
        $flags = $args->isOptionSet('force')
153 1
            ? AssetManager::OVERRIDE | AssetManager::IGNORE_SERVER_NOT_FOUND
154 5
            : AssetManager::OVERRIDE;
155 5
        $mappingToUpdate = $this->getMappingByUuidPrefix($args->getArgument('uuid'));
156 5
        $path = $mappingToUpdate->getGlob();
157 5
        $serverPath = $mappingToUpdate->getServerPath();
158 5
        $serverName = $mappingToUpdate->getServerName();
159
160 5
        if ($args->isOptionSet('path')) {
161 3
            $path = Path::makeAbsolute($args->getOption('path'), $this->currentPath);
162
        }
163
164 5
        if ($args->isOptionSet('server-path')) {
165 2
            $serverPath = $args->getOption('server-path');
166
        }
167
168 5
        if ($args->isOptionSet('server')) {
169 1
            $serverName = $args->getOption('server');
170
        }
171
172 5
        $updatedMapping = new AssetMapping($path, $serverName, $serverPath, $mappingToUpdate->getUuid());
173
174 5
        if ($this->mappingsEqual($mappingToUpdate, $updatedMapping)) {
175 1
            throw new RuntimeException('Nothing to update.');
176
        }
177
178 4
        $this->assetManager->addRootAssetMapping($updatedMapping, $flags);
179
180 4
        return 0;
181
    }
182
183 3
    public function handleDelete(Args $args)
184
    {
185 3
        $mapping = $this->getMappingByUuidPrefix($args->getArgument('uuid'));
186
187 1
        $this->assetManager->removeRootAssetMapping($mapping->getUuid());
188
189 1
        return 0;
190
    }
191
192 4
    public function handleInstall(Args $args, IO $io)
193
    {
194 4
        if ($args->isArgumentSet('server')) {
195 1
            $expr = Expr::method('getServerName', Expr::same($args->getArgument('server')));
196 1
            $mappings = $this->assetManager->findAssetMappings($expr);
197
        } else {
198 3
            $mappings = $this->assetManager->getAssetMappings();
199
        }
200
201 4
        if (!$mappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mappings of type Puli\Manager\Api\Asset\AssetMapping[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
202 1
            $io->writeLine('Nothing to install.');
203
204 1
            return 0;
205
        }
206
207
        /** @var InstallationParams[] $paramsToInstall */
208 3
        $paramsToInstall = array();
209
210
        // Prepare and validate the installation of all matching mappings
211 3
        foreach ($mappings as $mapping) {
212 3
            $paramsToInstall[] = $this->installationManager->prepareInstallation($mapping);
213
        }
214
215 2
        foreach ($paramsToInstall as $params) {
216 2
            foreach ($params->getResources() as $resource) {
217 2
                $serverPath = rtrim($params->getDocumentRoot(), '/').$params->getServerPathForResource($resource);
218
219 2
                $io->writeLine(sprintf(
220 2
                    'Installing <c1>%s</c1> into <c2>%s</c2> via <u>%s</u>...',
221 2
                    $resource->getRepositoryPath(),
222 2
                    trim($serverPath, '/'),
223 2
                    $params->getInstallerDescriptor()->getName()
224
                ));
225
226 2
                $this->installationManager->installResource($resource, $params);
227
            }
228
        }
229
230 2
        return 0;
231
    }
232
233
    /**
234
     * @param IO             $io
235
     * @param AssetMapping[] $mappings
236
     * @param bool           $enabled
237
     */
238 3
    private function printMappingTable(IO $io, array $mappings, $enabled = true)
239
    {
240 3
        $table = new Table(TableStyle::borderless());
241
242 3
        $globTag = $enabled ? 'c1' : 'bad';
243 3
        $pathTag = $enabled ? 'c2' : 'bad';
244
245 3
        foreach ($mappings as $mapping) {
246 3
            $uuid = substr($mapping->getUuid()->toString(), 0, 6);
247 3
            $glob = $mapping->getGlob();
248 3
            $serverPath = $mapping->getServerPath();
249
250 3
            if (!$enabled) {
251 2
                $uuid = sprintf('<bad>%s</bad>', $uuid);
252
            }
253
254 3
            $table->addRow(array(
255 3
                $uuid,
256 3
                sprintf('<%s>%s</%s>', $globTag, $glob, $globTag),
257 3
                sprintf('<%s>%s</%s>', $pathTag, $serverPath, $pathTag),
258
            ));
259
        }
260
261 3
        $table->render($io, 8);
262 3
    }
263
264
    /**
265
     * @param string $uuidPrefix
266
     *
267
     * @return AssetMapping
268
     */
269 8 View Code Duplication
    private function getMappingByUuidPrefix($uuidPrefix)
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...
270
    {
271 8
        $expr = Expr::method('getUuid', Expr::startsWith($uuidPrefix));
272
273 8
        $mappings = $this->assetManager->findAssetMappings($expr);
274
275 8
        if (!$mappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mappings of type Puli\Manager\Api\Asset\AssetMapping[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
276 1
            throw new RuntimeException(sprintf(
277 1
                'The mapping with the UUID prefix "%s" does not exist.',
278
                $uuidPrefix
279
            ));
280
        }
281
282 7
        if (count($mappings) > 1) {
283 1
            throw new RuntimeException(sprintf(
284 1
                'More than one mapping matches the UUID prefix "%s".',
285
                $uuidPrefix
286
            ));
287
        }
288
289 6
        return reset($mappings);
290
    }
291
292 5
    private function mappingsEqual(AssetMapping $mapping1, AssetMapping $mapping2)
293
    {
294 5
        return $mapping1->getUuid() === $mapping2->getUuid() &&
295 5
            $mapping1->getGlob() === $mapping2->getGlob() &&
296 5
            $mapping1->getServerPath() === $mapping2->getServerPath() &&
297 5
            $mapping1->getServerName() === $mapping2->getServerName();
298
    }
299
}
300