Completed
Pull Request — 1.0 (#58)
by Titouan
02:39
created

AbstractRepository::sanitizePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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