Passed
Push — main ( b2f07c...c5252d )
by Breno
02:07
created

Resolver::withCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus\Handler\Builder;
5
6
use OniBus\Attributes\Handler;
7
use OniBus\Handler\ClassMethod\ClassMethodExtractor;
8
use OniBus\Handler\ClassMethod\Extractor\CachedExtractor;
9
use OniBus\Handler\ClassMethod\Extractor\ExtractorUsingAttribute;
10
use OniBus\Handler\ClassMethod\Extractor\MethodFirstParameterExtractor;
11
use OniBus\Handler\ClassMethod\Mapper\DirectMapper;
12
use OniBus\Handler\ClassMethod\Mapper\ThrowingExceptionMapper;
13
use OniBus\Handler\ClassMethodResolver;
14
use OniBus\Handler\HandlerResolver;
15
use Psr\Container\ContainerInterface;
16
use Psr\SimpleCache\CacheInterface;
17
18
final class Resolver
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    /**
26
     * @var CacheInterface
27
     */
28
    private $cache;
29
30
    /**
31
     * @var string
32
     */
33
    private $cacheKey;
34
35
    /**
36
     * @var bool
37
     */
38
    private $throwingException;
39
40
    /**
41
     * @var array
42
     */
43
    private $handlers;
44
45
    public function __construct(ContainerInterface $container)
46
    {
47
        $this->container = $container;
48
        $this->throwingException = false;
49
        $this->handlers = [];
50
    }
51
52
    public static function new(ContainerInterface $container): self
53
    {
54
        return new self($container);
55
    }
56
57
    public function withCache(CacheInterface $cache, string $cacheKey): self
58
    {
59
        $this->cache = $cache;
60
        $this->cacheKey = $cacheKey;
61
        return $this;
62
    }
63
64
    public function withHandlers(array $handlersFQCN): self
65
    {
66
        $this->handlers = $handlersFQCN;
67
        return $this;
68
    }
69
70
    public function throwingExceptions(bool $throw = true): self
71
    {
72
        $this->throwingException = $throw;
73
        return $this;
74
    }
75
76
    public function mapByAttributes(string $attribute = Handler::class): HandlerResolver
77
    {
78
        return $this->build(new ExtractorUsingAttribute($attribute, $this->handlers));
79
    }
80
81
    public function mapByMethod(string $method = "__invoke"): HandlerResolver
82
    {
83
        return $this->build(new MethodFirstParameterExtractor($method, $this->handlers));
84
    }
85
86
    private function build(ClassMethodExtractor $extractor): HandlerResolver
87
    {
88
        if ($this->cache instanceof CacheInterface && !empty($this->cacheKey)) {
89
            $extractor = new CachedExtractor($extractor, $this->cache, $this->cacheKey);
90
        }
91
92
        $mapper = new DirectMapper(...$extractor->extractClassMethods());
93
        if ($this->throwingException) {
94
            $mapper = new ThrowingExceptionMapper($mapper);
95
        }
96
97
        return new ClassMethodResolver($this->container, $mapper);
98
    }
99
}
100