ComponentFinder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 20 4
A expandComponentClassPath() 0 17 3
A resolveNamespaceFromAlias() 0 10 2
A sanitizeIdentifier() 0 6 1
1
<?php
2
3
namespace Spatie\ViewComponents;
4
5
use Illuminate\Contracts\Support\Htmlable;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
9
final class ComponentFinder
10
{
11
    /** @var string */
12
    private $rootNamespace;
13
14
    /** @var array */
15
    private $namespaces;
16
17
    public function __construct(string $rootNamespace, array $namespaces)
18
    {
19
        $this->rootNamespace = $rootNamespace;
20
        $this->namespaces = $namespaces;
21
    }
22
23
    public function find(string $identifier): string
24
    {
25
        $identifier = $this->sanitizeIdentifier($identifier);
26
27
        $componentClass = class_exists($identifier)
28
            ? $identifier
29
            : $this->expandComponentClassPath($identifier);
30
31
        if (! class_exists($componentClass)) {
32
            throw new InvalidArgumentException("View component [{$componentClass}] not found.");
33
        }
34
35
        if (! array_key_exists(Htmlable::class, class_implements($componentClass))) {
36
            throw new InvalidArgumentException(
37
                "View component [{$componentClass}] must implement Illuminate\Support\Htmlable."
38
            );
39
        }
40
41
        return $componentClass;
42
    }
43
44
    private function expandComponentClassPath(string $path): string
45
    {
46
        if (Str::contains($path, '::')) {
47
            [$namespaceAlias, $path] = explode('::', $path, 2);
0 ignored issues
show
Bug introduced by
The variable $namespaceAlias does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
48
        }
49
50
        $namespace = isset($namespaceAlias)
51
            ? $this->resolveNamespaceFromAlias($namespaceAlias)
52
            : $this->rootNamespace;
53
54
        return collect(explode('.', $path))
55
            ->map(function (string $part) {
56
                return ucfirst($part);
57
            })
58
            ->prepend($namespace)
59
            ->implode('\\');
60
    }
61
62
    private function resolveNamespaceFromAlias(string $alias): string
63
    {
64
        $namespace = $this->namespaces[$alias] ?? null;
65
66
        if (! $namespace) {
67
            throw new InvalidArgumentException("View component namespace [$alias] doesn't exist.");
68
        }
69
70
        return $namespace;
71
    }
72
73
    private function sanitizeIdentifier(string $identifier): string
74
    {
75
        $identifier = trim($identifier, '\'" ');
76
77
        return str_replace('::class', '', $identifier);
78
    }
79
}
80