Failed Conditions
Pull Request — 1.0 (#64)
by Titouan
04:36
created

LinkResource::listChildren()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 14
loc 14
ccs 4
cts 8
cp 0.5
rs 9.4286
cc 3
eloc 7
nc 3
nop 0
crap 4.125
1
<?php
2
3
/*
4
 * This file is part of the puli/repository 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\Repository\Resource;
13
14
use Puli\Repository\Api\Resource\PuliResource;
15
use Puli\Repository\Api\ResourceNotFoundException;
16
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
17
18
/**
19
 * A link resource targeting to another resource.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 * @author Titouan Galopin <[email protected]>
25
 */
26
class LinkResource extends GenericResource implements PuliResource
27
{
28
    /**
29
     * @var string
30
     */
31
    private $targetPath;
32
33
    /**
34
     * @param string      $targetPath
35
     * @param string|null $path
36
     */
37 40
    public function __construct($targetPath, $path = null)
38
    {
39 40
        parent::__construct($path);
40
41 40
        $this->targetPath = $targetPath;
42 40
    }
43
44
    /**
45
     * Return this link target path.
46
     *
47
     * @return string
48
     */
49 32
    public function getTargetPath()
50
    {
51 32
        return $this->targetPath;
52
    }
53
54
    /**
55
     * Return the resource associated in the link repository to this link target path.
56
     *
57
     * @param boolean $followLinks Whether to fully follow links or not.
58
     *
59
     * @return PuliResource
60
     */
61 21
    public function getTarget($followLinks = false)
62
    {
63 21
        if (!$this->getRepository()) {
64 7
            throw ResourceNotFoundException::forPath($this->getTargetPath());
65
        }
66
67 14
        $target = $this->getRepository()->get($this->getTargetPath());
68
69 14
        if (!$followLinks) {
70 11
            return $target;
71
        }
72
73 3
        while ($target instanceof LinkResource) {
74 2
            $target = $target->getTarget(true);
75 2
        }
76
77 3
        return $target;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 View Code Duplication
    public function getChild($relPath)
84
    {
85
        if (!$this->getRepository()) {
86
            throw ResourceNotFoundException::forPath($this->getTargetPath().'/'.$relPath);
87
        }
88
89
        return $this->getRepository()->get($this->getTargetPath().'/'.$relPath);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function hasChild($relPath)
96
    {
97
        if (!$this->getRepository()) {
98
            return false;
99
        }
100
101
        return $this->getRepository()->contains($this->getTargetPath().'/'.$relPath);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 10
    public function hasChildren()
108
    {
109 10
        if (!$this->getRepository()) {
110 10
            return false;
111
        }
112
113
        return $this->getRepository()->hasChildren($this->getRepositoryPath());
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 7 View Code Duplication
    public function listChildren()
120
    {
121 7
        $children = new ArrayResourceCollection();
122
123 7
        if (!$this->getRepository()) {
124 7
            return $children;
125
        }
126
127
        foreach ($this->getRepository()->listChildren($this->getTargetPath()) as $child) {
128
            $children[$child->getName()] = $child;
129
        }
130
131
        return $children;
132
    }
133
}
134