Completed
Pull Request — 1.0 (#58)
by Titouan
04:38 queued 02:05
created

GenericResource::getStack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 6
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\Resource\ResourceMetadata;
16
use Puli\Repository\Api\ResourceNotFoundException;
17
use Puli\Repository\Api\ResourceRepository;
18
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
19
20
/**
21
 * A generic resource.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class GenericResource implements PuliResource
28
{
29
    /**
30
     * @var ResourceRepository
31
     */
32
    private $repo;
33
34
    /**
35
     * @var string
36
     */
37
    private $path;
38
39
    /**
40
     * @var string
41
     */
42
    private $repoPath;
43
44
    /**
45
     * Creates a new resource.
46
     *
47
     * @param string|null $path The path of the resource.
48
     */
49 733
    public function __construct($path = null)
50
    {
51 733
        $this->path = $path;
52 733
        $this->repoPath = $path;
53 733
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 307
    public function getPath()
59
    {
60 307
        return $this->path;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 317
    public function getName()
67
    {
68 317
        return $this->path ? basename($this->path) : null;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4 View Code Duplication
    public function getChild($relPath)
75
    {
76 4
        if (!$this->getRepository()) {
77 1
            throw ResourceNotFoundException::forPath($this->getRepositoryPath().'/'.$relPath);
78
        }
79
80 3
        return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 3
    public function hasChild($relPath)
87
    {
88 3
        if (!$this->getRepository()) {
89 1
            return false;
90
        }
91
92 2
        return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 3
    public function hasChildren()
99
    {
100 3
        if (!$this->getRepository()) {
101 1
            return false;
102
        }
103
104 2
        return $this->getRepository()->hasChildren($this->getRepositoryPath());
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 6 View Code Duplication
    public function listChildren()
111
    {
112 6
        $children = new ArrayResourceCollection();
113
114 6
        if (!$this->getRepository()) {
115 1
            return $children;
116
        }
117
118 5
        foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) {
119 2
            $children[$child->getName()] = $child;
120 5
        }
121
122 5
        return $children;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getStack()
129
    {
130
        if (!$this->getRepository()) {
131
            throw ResourceNotFoundException::forPath($this->getRepositoryPath());
132
        }
133
134
        return $this->getRepository()->getStack($this->getRepositoryPath());
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getMetadata()
141
    {
142
        return new ResourceMetadata();
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 439
    public function attachTo(ResourceRepository $repo, $path = null)
149
    {
150 439
        $this->repo = $repo;
151
152 439
        if (null !== $path) {
153 369
            $this->path = $path;
154 369
            $this->repoPath = $path;
155 369
        }
156 439
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 25
    public function detach()
162
    {
163 25
        $this->repo = null;
164 25
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 245
    public function getRepository()
170
    {
171 245
        return $this->repo;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 128
    public function getRepositoryPath()
178
    {
179 128
        return $this->repoPath;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 287
    public function isAttached()
186
    {
187 287
        return null !== $this->repo;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 56
    public function createReference($path)
194
    {
195 56
        $ref = clone $this;
196 56
        $ref->path = $path;
197
198 56
        return $ref;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 84
    public function isReference()
205
    {
206 84
        return $this->path !== $this->repoPath;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 39
    public function serialize()
213
    {
214 39
        $data = array();
215
216 39
        $this->preSerialize($data);
217
218 39
        return serialize($data);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224 35
    public function unserialize($string)
225
    {
226 35
        $data = unserialize($string);
227
228 35
        $this->postUnserialize($data);
229 35
    }
230
231
    /**
232
     * Invoked before serializing a resource.
233
     *
234
     * Override this method if you want to serialize custom data in subclasses.
235
     *
236
     * @param array $data The data to serialize. Add custom data at the end of
237
     *                    the array.
238
     */
239 39
    protected function preSerialize(array &$data)
240
    {
241 39
        $data[] = $this->path;
242 39
        $data[] = $this->repoPath;
243 39
    }
244
245
    /**
246
     * Invoked after unserializing a resource.
247
     *
248
     * Override this method if you want to unserialize custom data in
249
     * subclasses.
250
     *
251
     * @param array $data The unserialized data. Pop your custom data from the
252
     *                    end of the array before calling the parent method.
253
     */
254 35
    protected function postUnserialize(array $data)
255
    {
256 35
        $this->repoPath = array_pop($data);
257 35
        $this->path = array_pop($data);
258 35
    }
259
}
260