Completed
Push — 1.0 ( 0e2cc5...14cdbe )
by Bernhard
02:42
created

LinkResource::postUnserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
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 61
    public function __construct($targetPath, $path = null)
38
    {
39 61
        parent::__construct($path);
40
41 61
        $this->targetPath = $targetPath;
42 61
    }
43
44
    /**
45
     * @return string
46
     */
47 27
    public function getTargetPath()
48
    {
49 27
        return $this->targetPath;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 13
    public function getTarget()
56
    {
57 13
        if (!$this->getRepository()) {
58 8
            throw ResourceNotFoundException::forPath($this->getTargetPath());
59
        }
60
61 5
        return $this->getRepository()->get($this->getTargetPath());
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 4 View Code Duplication
    public function getChild($relPath)
68
    {
69 4
        if (!$this->getRepository()) {
70 2
            throw ResourceNotFoundException::forPath($this->getTargetPath().'/'.$relPath);
71
        }
72
73 2
        return $this->getRepository()->get($this->getTargetPath().'/'.$relPath);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 3
    public function hasChild($relPath)
80
    {
81 3
        if (!$this->getRepository()) {
82 1
            return false;
83
        }
84
85 2
        return $this->getRepository()->contains($this->getTargetPath().'/'.$relPath);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 11
    public function hasChildren()
92
    {
93 11
        if (!$this->getRepository()) {
94 9
            return false;
95
        }
96
97 2
        return $this->getRepository()->hasChildren($this->getRepositoryPath());
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 5 View Code Duplication
    public function listChildren()
104
    {
105 5
        $children = new ArrayResourceCollection();
106
107 5
        if (!$this->getRepository()) {
108 3
            return $children;
109
        }
110
111 2
        foreach ($this->getRepository()->listChildren($this->getTargetPath()) as $child) {
112 2
            $children[$child->getName()] = $child;
113 2
        }
114
115 2
        return $children;
116
    }
117
118 7
    protected function preSerialize(array &$data)
119
    {
120 7
        parent::preSerialize($data);
121
122 7
        $data[] = $this->targetPath;
123 7
    }
124
125 7
    protected function postUnserialize(array $data)
126
    {
127 7
        $this->targetPath = array_pop($data);
128
129 7
        parent::postUnserialize($data);
130 7
    }
131
}
132