Completed
Push — master ( 9d3655...931357 )
by Omar
02:35
created

LocalLocation::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace TemplesOfCode\CodeSanity\Location;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use TemplesOfCode\Sofa\Exception\ShellExecutionException;
7
use TemplesOfCode\Sofa\CommandChain;
8
use TemplesOfCode\CodeSanity\Location;
9
use TemplesOfCode\CodeSanity\RosterItem;
10
use TemplesOfCode\CodeSanity\Roster;
11
12
/**
13
 * Class LocalLocation
14
 * @package TemplesOfCode\CodeSanity
15
 */
16
class LocalLocation extends Location
17
{
18
    /*
19
     * @param string $directory
20
     * @return bool
21
     */
22 5
    protected function isReadable($directory)
23
    {
24 5
        return is_readable($directory);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 6
    public function isValid()
31
    {
32
        /**
33
         * @var bool $verdict
34
         */
35 6
        $verdict = !empty($this->directory) &&
36 5
            $this->isReadable($this->directory)
37 6
        ;
38
39 6
        return $verdict;
40
    }
41
42
    /**
43
     * @return CommandChain
44
     */
45 3
    protected function getRosterListCommand()
46
    {
47
        /**
48
         * todo: explore the OOP approach by iterating through the dir tree with RecursiveDirectoryIterator.
49
         */
50
51 3
        if (!$this->isValid()) {
52 1
            throw new \InvalidArgumentException(sprintf(
53 1
                "Local location validation failed for Location with directory '%s'",
54 1
                $this->directory
55 1
            ));
56
        }
57
58
        /**
59
         * @var CommandChain $pipeChainedCommands
60
         */
61 2
        $pipeChainedCommands = $this->buildPipeChainedCommands();
62
63
        /**
64
         * @var CommandChain $sequenceChainedCommands
65
         */
66 2
        $sequenceChainedCommands = $this->buildSequenceChainedCommands();
67 2
        $sequenceChainedCommands->addCommand($pipeChainedCommands);
68
69 2
        return $sequenceChainedCommands;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getName()
76
    {
77 1
        if (empty($this->name)) {
78 1
            $this->name = $this->getDirectory();
79 1
        }
80
81 1
        return $this->name;
82
    }
83
84
    /**
85
     * @param RosterItem $rosterItem
86
     * @return  string
87
     */
88 2
    public function getFullPath(RosterItem $rosterItem)
89
    {
90
91
        /**
92
         * @var string $targetFileName
93
         */
94 2
        $targetFileName = $rosterItem->getRelativeFileName();
95
96 2
        $fullPath = sprintf(
97 2
            '%s/%s',
98 2
            $this->getName(),
99
            $targetFileName
100 2
        );
101
102 2
        return $fullPath;
103
    }
104
105
}
106