Failed Conditions
Pull Request — 1.0 (#64)
by Titouan
04:36
created

AbstractRepository::followLinksOrFail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
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\Resource\PuliResource;
15
use Puli\Repository\Api\ResourceNotFoundException;
16
use Puli\Repository\Api\ResourceRepository;
17
use Puli\Repository\Api\UnsupportedLanguageException;
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
     * Validate a language is usable to search in repositories.
33
     *
34
     * @param string $language
35
     */
36 214
    protected function validateSearchLanguage($language)
37
    {
38 214
        if ('glob' !== $language) {
39 10
            throw UnsupportedLanguageException::forLanguage($language);
40
        }
41 204
    }
42
43
    /**
44
     * Sanitize a given path and check its validity.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     */
50 549
    protected function sanitizePath($path)
51
    {
52 549
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
53 529
        Assert::startsWith($path, '/', 'The path %s is not absolute.');
54
55 519
        return Path::canonicalize($path);
56
    }
57
58
    /**
59
     * @param string $path
60
     *
61
     * @return PuliResource
62
     */
63 16
    protected function followLinksOrFail($path)
64
    {
65 16
        $resolvedWithLinks = $this->followLinks($path);
66
67 16
        if ($resolvedWithLinks) {
68 9
            return $resolvedWithLinks;
69
        }
70
71 7
        throw ResourceNotFoundException::forPath($path);
72
    }
73
74
    /**
75
     * Resolve links in the given path to find the resource.
76
     *
77
     * @param string $path
78
     *
79
     * @return PuliResource|null Return the found resource or null if nothing was found.
80
     */
81
    abstract protected function followLinks($path);
82
}
83