Passed
Push — main ( af5401...201db1 )
by Michael
05:14 queued 01:57
created

AutoBinderClearCommand::flushContainerBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MichaelRubel\AutoBinder\Commands;
4
5
use Illuminate\Cache\Repository;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use InvalidArgumentException;
9
use MichaelRubel\AutoBinder\AutoBinder;
0 ignored issues
show
Bug introduced by
The type MichaelRubel\AutoBinder\AutoBinder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class AutoBinderClearCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $signature = 'binder:clear {folder : Folder to clear the cache from}';
17
18
    /**
19
     * @var string
20
     */
21
    protected $description = 'Clear a cached bindings';
22
23
    /**
24
     * @param  Repository  $cache
25
     *
26
     * @return void
27
     */
28 2
    public function handle(Repository $cache): void
29
    {
30 2
        $this->getFolders()->each(function ($folder) use ($cache) {
31 2
            $clue = $this->getClueFor($folder);
32
33 2
            if (! $cache->has($clue)) {
34 1
                throw new InvalidArgumentException('Cached folder ' . $folder . ' not found.');
35
            }
36
37 1
            $this->flushContainerBindings($cache, $clue);
38
39 1
            $cache->forget($clue);
40 2
        });
41
42 1
        $this->info('Container binding cache cleared successfully!');
43
    }
44
45
    /**
46
     * Retrieves folders to flush from the command input.
47
     *
48
     * @return Collection<int, string>
49
     */
50 2
    private function getFolders(): Collection
51
    {
52 2
        $folders = explode(',', $this->argument('folder'));
53
54 2
        return collect($folders);
0 ignored issues
show
Bug introduced by
$folders of type string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

54
        return collect(/** @scrutinizer ignore-type */ $folders);
Loading history...
55
    }
56
57
    /**
58
     * Retrieves the cache clue for the specified folder.
59
     *
60
     * @param  string  $folder
61
     *
62
     * @return string
63
     */
64 2
    private function getClueFor(string $folder): string
65
    {
66 2
        return (new AutoBinder($folder))->cacheClue();
67
    }
68
69
    /**
70
     * Cleans up the application container bindings.
71
     *
72
     * @param  Repository  $cache
73
     * @param  string  $clue
74
     *
75
     * @return void
76
     */
77 1
    private function flushContainerBindings(Repository $cache, string $clue): void
78
    {
79 1
        with($cache->get($clue), fn ($fromCache) => collect($fromCache)->each(
80 1
            fn ($concrete, $interface) => app()->offsetUnset($interface)
81 1
        ));
82
    }
83
}
84