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

BaseUrlSegment::strip()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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