Completed
Push — master ( 0bfae6...7513f1 )
by Bernhard
04:21
created

CatCommandHandler::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 4.128
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\Resource\BodyResource;
15
use Puli\Repository\Api\ResourceRepository;
16
use Webmozart\Console\Api\Args\Args;
17
use Webmozart\Console\Api\IO\IO;
18
use Webmozart\PathUtil\Path;
19
20
/**
21
 * Handles the "cat" command.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Stephan Wentz <[email protected]>
26
 */
27
class CatCommandHandler
28
{
29
    /**
30
     * @var ResourceRepository
31
     */
32
    private $repo;
33
34
    /**
35
     * @var string
36
     */
37
    private $currentPath = '/';
38
39
    /**
40
     * Creates the handler.
41
     *
42
     * @param ResourceRepository $repo The resource repository.
43
     */
44 3
    public function __construct(ResourceRepository $repo)
45
    {
46 3
        $this->repo = $repo;
47 3
    }
48
49
    /**
50
     * Handles the "ls" command.
51
     *
52
     * @param Args $args The console arguments.
53
     * @param IO   $io   The I/O.
54
     *
55
     * @return int The status code.
56
     */
57 3
    public function handle(Args $args, IO $io)
58
    {
59 3
        $path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath);
60
61 3
        $resources = $this->repo->find($path);
62
63 3
        if (!count($resources)) {
64
            $io->errorLine(sprintf('No resources found for path %s', $path));
65
66
            return 1;
67
        }
68
69 3
        foreach ($resources as $resource) {
70 3
            if ($resource instanceof BodyResource) {
71 3
                $io->writeLine($resource->getBody());
72
            }
73
        }
74
75 3
        return 0;
76
    }
77
}
78