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

CachesBindings::applyCachingBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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
     * Cache the binding.
14
     *
15
     * @param  string  $interface
16
     * @param  \Closure|string  $concrete
17
     *
18
     * @return void
19
     * @throws ContainerExceptionInterface
20
     * @throws NotFoundExceptionInterface
21
     */
22 13
    protected function cacheBindingFor(string $interface, \Closure|string $concrete): void
23
    {
24 13
        $clue = 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...
25
26 13
        $cache = cache()->get($clue);
27
28 13
        $cache[$interface] = $concrete;
29
30 13
        cache()->put($clue, $cache);
31
    }
32
33
    /**
34
     * Apply the caching.
35
     *
36
     * @param  string  $clue
37
     *
38
     * @return void
39
     * @throws ContainerExceptionInterface
40
     * @throws NotFoundExceptionInterface
41
     */
42
    protected function applyCachingBy(string $clue): void
43
    {
44
        collect(cache()->get($clue))->each(
0 ignored issues
show
Bug introduced by
It seems like cache()->get($clue) 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

44
        collect(/** @scrutinizer ignore-type */ cache()->get($clue))->each(
Loading history...
45
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
46
        );
47
    }
48
}
49