Test Failed
Push — feature-laravel-5.4 ( dbda8c...02b5b6 )
by Kirill
03:30
created

NavMatcher::isCurrent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Services;
10
11
use Illuminate\Routing\Route;
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class NavMatcher.
16
 */
17
class NavMatcher
18
{
19
    /**
20
     * @var string
21
     */
22
    private $active;
23
24
    /**
25
     * @var Route
26
     */
27
    private $route;
28
29
    /**
30
     * NavMatcher constructor.
31
     * @param Route  $route
32
     * @param string $active
33
     */
34
    public function __construct(Route $route, string $active = 'active')
35
    {
36
        $this->active = $active;
37
        $this->route = $route;
38
    }
39
40
    /**
41
     * @param string $route
42
     * @return string
43
     */
44
    public function match(string $route): string
45
    {
46
        return $this->isCurrent($route) || $this->isMatched($route)
47
            ? $this->active
48
            : '';
49
    }
50
51
    /**
52
     * @param string $needle
53
     * @return bool
54
     */
55
    private function isCurrent(string $needle): bool
56
    {
57
        return $this->route->getName() === $needle;
58
    }
59
60
    /**
61
     * @param string $needle
62
     * @return bool
63
     */
64
    private function isMatched(string $needle): bool
65
    {
66
        return Str::startsWith($this->route->getName(), $needle . '.');
67
    }
68
}