Passed
Push — master ( c227df...528f97 )
by Petr
07:58
created

Link::link()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 6
rs 9.6111
ccs 5
cts 5
cp 1
crap 5
1
<?php
2
3
namespace kalanis\kw_routed_paths\Linking;
4
5
6
use kalanis\kw_paths\Interfaces\IPaths;
7
use kalanis\kw_paths\Stuff;
8
use kalanis\kw_routed_paths\Support;
9
10
11
/**
12
 * Class Link
13
 * @package kalanis\kw_routed_paths\Linking
14
 * The reverse side of class
15
 * @see \kalanis\kw_routed_paths\Sources\Request
16
 */
17
class Link
18
{
19
    /** @var string */
20
    protected $prefix = '';
21
22 31
    public function __construct(string $prefix = '')
23
    {
24 31
        $this->prefix = $prefix;
25 31
    }
26
27
    /**
28
     * @param string[] $path
29
     * @param string[] $module
30
     * @param bool $moduleSingle
31
     * @param string|null $user
32
     * @return string
33
     */
34 31
    public function link(array $path, array $module = [], bool $moduleSingle = false, ?string $user = null): string
35
    {
36 31
        $prefix = !empty($this->prefix) ? Stuff::canonize($this->prefix) . IPaths::SPLITTER_SLASH : '';
37 31
        $modulePath = !empty($module) ? [Support::prefixWithSeparator($moduleSingle ? Support::PREFIX_MOD_SINGLE : Support::PREFIX_MOD_NORMAL) . Support::requestFromModuleName($module)] : [];
38 31
        $userPath = !empty($user) ? [Support::prefixWithSeparator(Support::PREFIX_USER) . $user] : [];
39 31
        return $prefix . strval(implode(IPaths::SPLITTER_SLASH, array_filter(array_merge($modulePath, $userPath, $path))));
40
    }
41
}
42