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

CachesBindings::cacheClue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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