Failed Conditions
Pull Request — 1.0 (#64)
by Titouan
02:55
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\ResourceNotFoundException;
15
use Puli\Repository\Api\ResourceRepository;
16
use Puli\Repository\Api\UnsupportedLanguageException;
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
     * Validate a language is usable to search in repositories.
32
     *
33
     * @param string $language
34
     */
35 214
    protected function validateSearchLanguage($language)
36
    {
37 214
        if ('glob' !== $language) {
38 10
            throw UnsupportedLanguageException::forLanguage($language);
39
        }
40 204
    }
41
42
    /**
43
     * Sanitize a given path and check its validity.
44
     *
45
     * @param string $path
46
     *
47
     * @return string
48
     */
49 537
    protected function sanitizePath($path)
50
    {
51 537
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
52 517
        Assert::startsWith($path, '/', 'The path %s is not absolute.');
53
54 507
        return Path::canonicalize($path);
55
    }
56
57
    /**
58
     * @param string $path
59
     *
60
     * @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...
61
     */
62 10
    protected function followLinksOrFail($path)
63
    {
64 10
        $resolvedWithLinks = $this->followLinks($path);
65
66 10
        if ($resolvedWithLinks) {
67 3
            return $resolvedWithLinks;
68
        }
69
70 7
        throw ResourceNotFoundException::forPath($path);
71
    }
72
73
    /**
74
     * Resolve links in the given path to find the resource.
75
     *
76
     * @param string $path
77
     *
78
     * @return resource|null Return the found resource or null if nothing was found.
79
     */
80
    abstract protected function followLinks($path);
81
}
82