Passed
Pull Request — main (#3)
by Michael
03:25
created

CachesBindings   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 44
ccs 7
cts 10
cp 0.7
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A applyCache() 0 4 1
A cacheBindingFor() 0 9 1
A cacheClue() 0 3 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
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
     * Cache the binding.
24
     *
25
     * @param  string  $interface
26
     * @param  \Closure|string  $concrete
27
     *
28
     * @return void
29
     * @throws ContainerExceptionInterface
30
     * @throws NotFoundExceptionInterface
31
     */
32 15
    protected function cacheBindingFor(string $interface, \Closure|string $concrete): void
33
    {
34 15
        $clue = $this->cacheClue();
35
36 15
        $cache = cache()->get($clue);
37
38 15
        $cache[$interface] = $concrete;
39
40 15
        cache()->put($clue, $cache);
41
    }
42
43
    /**
44
     * Apply the caching.
45
     *
46
     * @return void
47
     * @throws ContainerExceptionInterface
48
     * @throws NotFoundExceptionInterface
49
     */
50
    protected function applyCache(): void
51
    {
52
        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

52
        collect(/** @scrutinizer ignore-type */ cache()->get($this->cacheClue()))->each(
Loading history...
53
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
54
        );
55
    }
56
}
57