Passed
Push — main ( fb71e4...7f9a44 )
by Michael
01:00 queued 12s
created

CachesBindings   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 75
ccs 12
cts 15
cp 0.8
rs 10
c 4
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withoutCaching() 0 5 1
A cacheBindingFor() 0 9 1
A hasCache() 0 3 2
A cacheClue() 0 3 1
A fromCache() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Psr\SimpleCache\InvalidArgumentException;
10
11
trait CachesBindings
12
{
13
    /**
14
     * Determines if the caching is enabled.
15
     *
16
     * @var bool
17
     */
18
    public bool $caching = true;
19
20
    /**
21
     * Disables the caching.
22
     *
23
     * @return static
24
     */
25 1
    public function withoutCaching(): static
26
    {
27 1
        $this->caching = false;
28
29 1
        return $this;
30
    }
31
32
    /**
33
     * Get the clue to access the cache.
34
     *
35
     * @return string
36
     */
37 16
    public function cacheClue(): string
38
    {
39 16
        return static::CACHE_KEY . $this->classFolder;
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...chesBindings::CACHE_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
    }
41
42
    /**
43
     * Check if the caching is enabled.
44
     *
45
     * @return bool
46
     * @throws InvalidArgumentException
47
     */
48 17
    protected function hasCache(): bool
49
    {
50 17
        return $this->caching && cache()->has($this->cacheClue());
51
    }
52
53
    /**
54
     * Use the bindings from the cache.
55
     *
56
     * @return void
57
     * @throws ContainerExceptionInterface
58
     * @throws NotFoundExceptionInterface
59
     */
60
    protected function fromCache(): void
61
    {
62
        collect(cache()->get($this->cacheClue()))->each(
0 ignored issues
show
Bug introduced by
It seems like cache()->get($this->cacheClue()) can also be of type Illuminate\Contracts\Cache\Repository; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        collect(/** @scrutinizer ignore-type */ cache()->get($this->cacheClue()))->each(
Loading history...
63
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
64
        );
65
    }
66
67
    /**
68
     * Cache the binding.
69
     *
70
     * @param  string  $interface
71
     * @param  \Closure|string  $concrete
72
     *
73
     * @return void
74
     * @throws ContainerExceptionInterface
75
     * @throws NotFoundExceptionInterface
76
     */
77 15
    protected function cacheBindingFor(string $interface, \Closure|string $concrete): void
78
    {
79 15
        $clue = $this->cacheClue();
80
81 15
        $cache = cache()->get($clue);
82
83 15
        $cache[$interface] = $concrete;
84
85 15
        cache()->put($clue, $cache);
86
    }
87
}
88