Failed Conditions
Push — 1.0 ( 9f5a0b...fe7a2f )
by Bernhard
30:36 queued 17:00
created

AbstractRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 0
cbo 5
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersions() 0 9 2
A failUnlessGlob() 0 6 2
A sanitizePath() 0 7 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\ChangeStream\VersionList;
15
use Puli\Repository\Api\NoVersionFoundException;
16
use Puli\Repository\Api\ResourceNotFoundException;
17
use Puli\Repository\Api\ResourceRepository;
18
use Puli\Repository\Api\UnsupportedLanguageException;
19
use Webmozart\Assert\Assert;
20
use Webmozart\PathUtil\Path;
21
22
/**
23
 * Abstract base for repositories providing tools to avoid code duplication.
24
 *
25
 * @since  1.0
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 * @author Titouan Galopin <[email protected]>
29
 */
30
abstract class AbstractRepository implements ResourceRepository
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 21
    public function getVersions($path)
36
    {
37
        // Non-editable repositories always contain only one version of a resource
38
        try {
39 21
            return new VersionList($path, array($this->get($path)));
40 7
        } catch (ResourceNotFoundException $e) {
41 7
            throw NoVersionFoundException::forPath($path, $e);
42
        }
43
    }
44
45
    /**
46
     * Validate a language is usable to search in repositories.
47
     *
48
     * @param string $language
49
     */
50 325
    protected function failUnlessGlob($language)
51
    {
52 325
        if ('glob' !== $language) {
53 18
            throw UnsupportedLanguageException::forLanguage($language);
54
        }
55 307
    }
56
57
    /**
58
     * Sanitize a given path and check its validity.
59
     *
60
     * @param string $path
61
     *
62
     * @return string
63
     */
64 835
    protected function sanitizePath($path)
65
    {
66 835
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
67 807
        Assert::startsWith($path, '/', 'The path %s is not absolute.');
68
69 793
        return Path::canonicalize($path);
70
    }
71
}
72