AutoBinderClearCommand::getClueFor()   A
last analyzed

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Commands;
6
7
use Illuminate\Cache\Repository;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Collection;
10
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...
11
12
class AutoBinderClearCommand extends Command
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $signature = 'binder:clear {folder : Folder to clear the cache from}';
18
19
    /**
20
     * @var string
21
     */
22
    protected $description = 'Clear a cached bindings';
23
24
    /**
25
     * Execute the command.
26
     */
27 2
    public function handle(Repository $cache): void
28
    {
29 2
        $this->getFolders()->each(function ($folder) use ($cache) {
30 2
            $clue = $this->getClueFor($folder);
31
32 2
            if (! $cache->has($clue)) {
33 1
                $this->warn('Cached folder ' . $folder . ' not found.');
34
35 1
                return;
36
            }
37
38 1
            $this->flushContainerBindings($cache, $clue);
39
40 1
            $cache->forget($clue);
41 2
        });
42
43 2
        $this->info('Container binding cache cleared successfully!');
44
    }
45
46
    /**
47
     * Retrieves folders to flush from the command input.
48
     *
49
     * @return Collection<int, string>
50
     */
51 2
    private function getFolders(): Collection
52
    {
53 2
        $folders = explode(',', $this->argument('folder'));
54
55 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

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