Passed
Push — main ( 463ab6...e73dd9 )
by Michael
01:09 queued 13s
created

CachesBindings   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 85
ccs 17
cts 17
cp 1
rs 10
c 6
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A withoutCaching() 0 5 1
A cacheClue() 0 3 1
A cacheEnabled() 0 3 3
A cacheBindingFor() 0 9 1
A hasCache() 0 3 2
A fromCache() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Closure;
8
use Psr\Container\ContainerExceptionInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use Psr\SimpleCache\InvalidArgumentException;
11
12
trait CachesBindings
13
{
14
    /**
15
     * Determines if the caching is enabled.
16
     *
17
     * @var bool
18
     */
19
    public bool $caching = true;
20
21
    /**
22
     * Disables the caching.
23
     *
24
     * @return static
25
     */
26 1
    public function withoutCaching(): static
27
    {
28 1
        $this->caching = false;
29
30 1
        return $this;
31
    }
32
33
    /**
34
     * Get the clue to access the cache.
35
     *
36
     * @return string
37
     */
38 21
    public function cacheClue(): string
39
    {
40 21
        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...
41
    }
42
43
    /**
44
     * Check if the caching is enabled.
45
     *
46
     * @return bool
47
     */
48 23
    protected function cacheEnabled(): bool
49
    {
50 23
        return isset($this->caching) && $this->caching && ! app()->isLocal();
0 ignored issues
show
introduced by
The method isLocal() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

50
        return isset($this->caching) && $this->caching && ! app()->/** @scrutinizer ignore-call */ isLocal();
Loading history...
51
    }
52
53
    /**
54
     * Check if the caching is enabled.
55
     *
56
     * @return bool
57
     * @throws InvalidArgumentException
58
     */
59 23
    protected function hasCache(): bool
60
    {
61 23
        return $this->cacheEnabled() && cache()->has($this->cacheClue());
62
    }
63
64
    /**
65
     * Use the bindings from the cache.
66
     *
67
     * @return void
68
     * @throws ContainerExceptionInterface
69
     * @throws NotFoundExceptionInterface
70
     */
71 3
    protected function fromCache(): void
72
    {
73 3
        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

73
        collect(/** @scrutinizer ignore-type */ cache()->get($this->cacheClue()))->each(
Loading history...
74 3
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
75
        );
76
    }
77
78
    /**
79
     * Cache the binding.
80
     *
81
     * @param  string  $interface
82
     * @param  Closure|string  $concrete
83
     *
84
     * @return void
85
     * @throws ContainerExceptionInterface
86
     * @throws NotFoundExceptionInterface
87
     */
88 20
    protected function cacheBindingFor(string $interface, Closure|string $concrete): void
89
    {
90 20
        $clue = $this->cacheClue();
91
92 20
        $cache = cache()->get($clue);
93
94 20
        $cache[$interface] = $concrete;
95
96 20
        cache()->put($clue, $cache);
97
    }
98
}
99