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

AbstractRepository::getStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4286
cc 1
eloc 2
nc 1
nop 1
crap 2
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;
13
14
use Puli\Repository\Api\ResourceRepository;
15
use Puli\Repository\Api\UnsupportedLanguageException;
16
use Puli\Repository\ChangeStream\ResourceStack;
17
use Webmozart\Assert\Assert;
18
use Webmozart\PathUtil\Path;
19
20
/**
21
 * Abstract base for repositories providing tools to avoid code duplication.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 * @author Titouan Galopin <[email protected]>
27
 */
28
abstract class AbstractRepository implements ResourceRepository
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getStack($path)
34
    {
35
        // The basic repositories are not editable: you cannot have multiple versions of the same resource.
36
        return new ResourceStack(array($this->get($path)));
37
    }
38
39
    /**
40
     * Validate a language is usable to search in repositories.
41
     *
42
     * @param string $language
43
     */
44 214
    protected function validateSearchLanguage($language)
45
    {
46 214
        if ('glob' !== $language) {
47 10
            throw UnsupportedLanguageException::forLanguage($language);
48
        }
49 204
    }
50
51
    /**
52
     * Sanitize a given path and check its validity.
53
     *
54
     * @param string $path
55
     *
56
     * @return string
57
     */
58 556
    protected function sanitizePath($path)
59
    {
60 556
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
61 536
        Assert::startsWith($path, '/', 'The path %s is not absolute.');
62
63 526
        return Path::canonicalize($path);
64
    }
65
}
66