Completed
Pull Request — master (#27)
by Hari
09:21
created

UrlCommandHandler::printUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the puli/cli 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\Repository\Api\ResourceRepository;
15
use Puli\UrlGenerator\Api\UrlGenerator;
16
use Webmozart\Console\Api\Args\Args;
17
use Webmozart\Console\Api\IO\IO;
18
use Webmozart\Glob\Glob;
19
use Webmozart\PathUtil\Path;
20
21
/**
22
 * Handles the "url" command.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class UrlCommandHandler
29
{
30
    /**
31
     * @var UrlGenerator
32
     */
33
    private $urlGenerator;
34
35
    /**
36
     * @var ResourceRepository
37
     */
38
    private $repo;
39
40
    /**
41
     * @var string
42
     */
43
    private $currentPath = '/';
44
45
    /**
46
     * Creates the handler.
47
     *
48
     * @param UrlGenerator       $urlGenerator The URL generator.
49
     * @param ResourceRepository $repo         The resource repository.
50
     */
51
    public function __construct(UrlGenerator $urlGenerator, ResourceRepository $repo)
52
    {
53
        $this->urlGenerator = $urlGenerator;
54
        $this->repo = $repo;
55
    }
56
57
    /**
58
     * Handles the "url" command.
59
     *
60
     * @param Args $args The console arguments.
61
     * @param IO   $io   The I/O.
62
     *
63
     * @return int The status code.
64
     */
65
    public function handle(Args $args, IO $io)
66
    {
67
        foreach ($args->getArgument('path') as $path) {
68
            if (!Glob::isDynamic($path)) {
69
                $this->printUrl($path, $io);
70
71
                continue;
72
            }
73
74
            foreach ($this->repo->find($path) as $resource) {
75
                $this->printUrl($resource->getPath(), $io);
76
            }
77
        }
78
79
        return 0;
80
    }
81
82
    /**
83
     * Prints the URL of a Puli path.
84
     *
85
     * @param string $path A Puli path.
86
     * @param IO     $io   The I/O.
87
     */
88
    private function printUrl($path, IO $io)
89
    {
90
        $path = Path::makeAbsolute($path, $this->currentPath);
91
        $io->writeLine($this->urlGenerator->generateUrl($path));
92
    }
93
}
94