Passed
Pull Request — main (#13)
by Michael
11:56
created

AutoBinderClearCommand::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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

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