Passed
Pull Request — main (#3)
by Michael
05:46 queued 02:43
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
10
trait CachesBindings
11
{
12
    /**
13
     * Get the clue to access the cache.
14
     *
15
     * @return string
16
     */
17 16
    public function cacheClue(): string
18
    {
19 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...
20
    }
21
22
    /**
23
     * Use the bindings from the cache.
24
     *
25
     * @return void
26
     * @throws ContainerExceptionInterface
27
     * @throws NotFoundExceptionInterface
28
     */
29
    protected function fromCache(): void
30
    {
31
        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

31
        collect(/** @scrutinizer ignore-type */ cache()->get($this->cacheClue()))->each(
Loading history...
32
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
33
        );
34
    }
35
36
    /**
37
     * Cache the binding.
38
     *
39
     * @param  string  $interface
40
     * @param  \Closure|string  $concrete
41
     *
42
     * @return void
43
     * @throws ContainerExceptionInterface
44
     * @throws NotFoundExceptionInterface
45
     */
46 15
    protected function cacheBindingFor(string $interface, \Closure|string $concrete): void
47
    {
48 15
        $clue = $this->cacheClue();
49
50 15
        $cache = cache()->get($clue);
51
52 15
        $cache[$interface] = $concrete;
53
54 15
        cache()->put($clue, $cache);
55
    }
56
}
57