Passed
Push — main ( 55e132...0fb55a )
by Breno
01:58
created

Fqcn   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 100
rs 10
c 3
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A withFilter() 0 4 1
A new() 0 3 1
A includeDeclaredTraits() 0 4 1
A withCache() 0 5 1
A includeDeclaredClasses() 0 4 1
A includeComposerClassMap() 0 4 1
A addDirectory() 0 4 1
A compose() 0 7 2
A fromDir() 0 3 1
A find() 0 13 3
A includeDeclaredInterfaces() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace FlexFqcnFinder;
5
6
use FlexFqcnFinder\Filter\FqcnSpecification;
7
use FlexFqcnFinder\Finder\ComposerClassMap;
8
use FlexFqcnFinder\Finder\Decorator\CachedFqcnFinder;
9
use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder;
10
use FlexFqcnFinder\Finder\FqcnFinder;
11
use FlexFqcnFinder\Finder\GetDeclaredClasses;
12
use FlexFqcnFinder\Finder\GetDeclaredInterfaces;
13
use FlexFqcnFinder\Finder\GetDeclaredTraits;
14
use FlexFqcnFinder\Repository\FilesFromDir;
15
use Psr\SimpleCache\CacheInterface;
16
17
class Fqcn implements FqcnFinderInterface
18
{
19
    /**
20
     * @var FqcnSpecification|null
21
     */
22
    protected $filter;
23
24
    /**
25
     * @var CacheInterface
26
     */
27
    private $cache;
28
29
    /**
30
     * @var string
31
     */
32
    protected $cacheKey;
33
34
    /**
35
     * @var FqcnFinderInterface
36
     */
37
    protected $finders = [];
38
39
    public static function new(): Fqcn
40
    {
41
        return new self();
42
    }
43
44
    public static function fromDir(string $dir, bool $recursive = true): Fqcn
45
    {
46
        return (new self())->addDirectory($dir, $recursive);
47
    }
48
49
    public function addDirectory(string $dir, bool $recursive = true): Fqcn
50
    {
51
        $this->finders[] = new FqcnFinder(new FilesFromDir($dir, $recursive));
52
        return $this;
53
    }
54
55
    public function includeDeclaredClasses(): Fqcn
56
    {
57
        $this->finders[] = new GetDeclaredClasses();
58
        return $this;
59
    }
60
61
    public function includeDeclaredTraits(): Fqcn
62
    {
63
        $this->finders[] = new GetDeclaredTraits();
64
        return $this;
65
    }
66
67
    public function includeDeclaredInterfaces(): Fqcn
68
    {
69
        $this->finders[] = new GetDeclaredInterfaces();
70
        return $this;
71
    }
72
73
    public function includeComposerClassMap(string $pathToComposerAutoload): Fqcn
74
    {
75
        $this->finders[] = new ComposerClassMap($pathToComposerAutoload);
76
        return $this;
77
    }
78
79
    public function withFilter(FqcnSpecification $filter): Fqcn
80
    {
81
        $this->filter = $filter;
82
        return $this;
83
    }
84
85
    public function withCache(CacheInterface $cache, string $cacheKey): Fqcn
86
    {
87
        $this->cache = $cache;
88
        $this->cacheKey = $cacheKey;
89
        return $this;
90
    }
91
92
    public function compose(FqcnFinderInterface ...$finders): Fqcn
93
    {
94
        foreach ($finders as $finder) {
95
            $this->finders[] = $finder;
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function find(): array
105
    {
106
        $finder = new FqcnFinderComposite(...$this->finders);
107
108
        if ($this->filter instanceof FqcnSpecification) {
109
            $finder = new FilteringFqcnFinder($finder, $this->filter);
110
        }
111
112
        if ($this->cache instanceof CacheInterface) {
0 ignored issues
show
introduced by
$this->cache is always a sub-type of Psr\SimpleCache\CacheInterface.
Loading history...
113
            $finder = new CachedFqcnFinder($finder, $this->cache, $this->cacheKey);
114
        }
115
116
        return $finder->find();
117
    }
118
}
119