Completed
Push — develop ( 752c7f...4a7063 )
by Tino
05:54
created

Aliases::getUriAlias()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 25
nop 2
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) by the ACP3 Developers.
5
 * See the LICENSE file at the top-level module directory for licensing details.
6
 */
7
8
namespace ACP3\Modules\ACP3\Seo\Core\Router;
9
10
use ACP3\Core;
11
use ACP3\Modules\ACP3\Seo;
12
13
class Aliases
14
{
15
    /**
16
     * @var \ACP3\Modules\ACP3\Seo\Cache
17
     */
18
    protected $seoCache;
19
    /**
20
     * @var array
21
     */
22
    protected $aliasesCache = [];
23
    /**
24
     * @var bool
25
     */
26
    private $isActive;
27
28
    /**
29
     * @param Core\Modules                 $modules
30
     * @param \ACP3\Modules\ACP3\Seo\Cache $seoCache
31
     */
32
    public function __construct(
33
        Core\Modules $modules,
34
        Seo\Cache $seoCache
35
    ) {
36
        $this->seoCache = $seoCache;
37
        $this->isActive = $modules->isActive(Seo\Installer\Schema::MODULE_NAME);
38
    }
39
40
    /**
41
     * Returns an uri alias by a given path.
42
     *
43
     * @param string $path
44
     * @param bool   $emptyOnNoResult
45
     *
46
     * @return string
47
     */
48
    public function getUriAlias(string $path, bool $emptyOnNoResult = false): string
49
    {
50
        if ($this->isActive === false) {
51
            return $path;
52
        }
53
54
        if ($this->aliasesCache === []) {
55
            $this->aliasesCache = $this->seoCache->getCache();
56
        }
57
58
        $path .= (!\preg_match('/\/$/', $path) ? '/' : '');
59
        $pathParts = \preg_split('=/=', $path, -1, PREG_SPLIT_NO_EMPTY);
60
        $routeArguments = [];
61
62
        while (\count($pathParts) >= 3) {
0 ignored issues
show
Bug introduced by Tino Goratsch
It seems like $pathParts can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        while (\count(/** @scrutinizer ignore-type */ $pathParts) >= 3) {
Loading history...
63
            $newPath = \implode('/', $pathParts) . '/';
0 ignored issues
show
Bug introduced by Tino Goratsch
It seems like $pathParts can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $newPath = \implode('/', /** @scrutinizer ignore-type */ $pathParts) . '/';
Loading history...
64
            if (!empty($this->aliasesCache[$newPath]['alias'])) {
65
                return $this->aliasesCache[$newPath]['alias']
66
                    . (!empty($routeArguments) ? '/' . \implode('/', $routeArguments) : '');
67
            }
68
69
            $routeArguments[] = \array_pop($pathParts);
0 ignored issues
show
Bug introduced by Tino Goratsch
It seems like $pathParts can also be of type false; however, parameter $array of array_pop() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            $routeArguments[] = \array_pop(/** @scrutinizer ignore-type */ $pathParts);
Loading history...
70
        }
71
72
        return $emptyOnNoResult === true ? '' : $path;
73
    }
74
75
    /**
76
     * Checks, whether an uri alias exists.
77
     *
78
     * @param string $path
79
     *
80
     * @return bool
81
     */
82
    public function uriAliasExists(string $path): bool
83
    {
84
        return $this->getUriAlias($path, true) !== '';
85
    }
86
}
87