Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

BaseUrlSegment   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 30
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 13 3
A strip() 0 13 3
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls\ProvidesUrl;
4
5
use Thinktomorrow\Chief\Management\Managers;
6
7
class BaseUrlSegment
8
{
9
    public static function strip($value)
10
    {
11
        $originalValue = $value = trim($value, '/');
12
13
        $segments = static::all();
14
15
        foreach ($segments as $segment) {
16
            if (0 === strpos($originalValue, $segment)) {
17
                $value = substr($value, strlen($segment));
18
            }
19
        }
20
21
        return trim($value, '/');
22
    }
23
24
    private static function all(): array
25
    {
26
        $segments = [];
27
28
        $managers = app(Managers::class)->all();
29
30
        foreach ($managers as $manager) {
31
            if (contract($manager->model(), ProvidesUrl::class)) {
32
                $segments[] = $manager->model()->baseUrlSegment();
33
            }
34
        }
35
36
        return array_unique($segments);
37
    }
38
}
39