PrintChannels::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MallardDuck\DynamicEcho\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use MallardDuck\DynamicEcho\DynamicEchoService;
9
10
class PrintChannels extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'dynamic-echo:channels {--F|full : Get the full channel report.}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'An easy way to see what routes are discoverable by the DynamicEchoEvent services.';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @param DynamicEchoService $dynamicEchoService
40
     *
41
     * @return int
42
     */
43
    public function handle(DynamicEchoService $dynamicEchoService): int
44
    {
45
        $headers = ["Event Channel"];
46
        /**
47
         * @var Collection
48
         */
49
        $channelMap = collect([[]]);
50
        if (!$this->option('full')) {
51
            $channelMap = $channelMap->merge($dynamicEchoService->getDiscoveredChannels());
52
            $channelMap = $channelMap->map(static function ($val) {
53
                return [$val];
54
            });
55
        } else {
56
            $headers[] = 'Channel Type';
57
            $headers[] = 'Auth Callback';
58
            $headers[] = 'Events';
59
            $channelMap = $channelMap->merge($dynamicEchoService->getExtendedChannelInfo());
60
61
            $channelMap = $channelMap->map(static function ($val, $key) {
62
                $authReflection = new \ReflectionFunction($val['authCallback']);
63
                $callbackType = Str::afterLast($authReflection->getName(), "\\");
64
                $args = array_map(static function ($val) {
65
                    if (null !== $val->getType()) {
66
                        return sprintf("%s %s", $val->getType()->getName(), $val->getName());
67
                    }
68
                    return $val->getName();
69
                }, $authReflection->getParameters());
70
71
                return [
72
                    $key,
73
                    $val['type'],
74
                    sprintf("%s - %s", $callbackType, implode(", ", $args)),
75
                    implode("," . PHP_EOL, $val['events']),
76
                ];
77
            });
78
        }
79
80
        $this->table($headers, $channelMap->toArray());
81
        return 0;
82
    }
83
}
84