Failed Conditions
Pull Request — 1.0 (#64)
by Titouan
03:11
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 0
Metric Value
c 1
b 0
f 0
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 Puli\Repository\Resource\LinkResource;
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
     * Validate a language is usable to search in repositories.
34
     *
35
     * @param string $language
36
     */
37 214
    protected function validateSearchLanguage($language)
38
    {
39 214
        if ('glob' !== $language) {
40 10
            throw UnsupportedLanguageException::forLanguage($language);
41
        }
42 204
    }
43
44
    /**
45
     * Sanitize a given path and check its validity.
46
     *
47
     * @param string $path
48
     *
49
     * @return string
50
     */
51 537
    protected function sanitizePath($path)
52
    {
53 537
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
54 517
        Assert::startsWith($path, '/', 'The path %s is not absolute.');
55
56 507
        return Path::canonicalize($path);
57
    }
58
59
    /**
60
     * @param string $path
61
     *
62
     * @return Resource
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
63
     */
64 10
    protected function followLinksOrFail($path)
65
    {
66 10
        $resolvedWithLinks = $this->followLinks($path);
67
68 10
        if ($resolvedWithLinks) {
69 3
            return $resolvedWithLinks;
70
        }
71
72 7
        throw ResourceNotFoundException::forPath($path);
73
    }
74
75
    /**
76
     * Resolve links in the given path to find the resource.
77
     *
78
     * @param string $path
79
     *
80
     * @return Resource|null Return the found resource or null if nothing was found.
81
     */
82
    abstract protected function followLinks($path);
83
}
84