-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathPanCommand.php
More file actions
59 lines (48 loc) · 1.76 KB
/
Copy pathPanCommand.php
File metadata and controls
59 lines (48 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Pan\Adapters\Laravel\Console\Commands;
use Illuminate\Console\Command;
use Pan\Console\Table;
use Pan\Contracts\AnalyticsRepository;
use Pan\Presentors\AnalyticPresentor;
use Pan\ValueObjects\Analytic;
/**
* @internal
*/
final class PanCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pan {--filter= : Filter the analytics by name} {--tenant= : Show only analytics for specific tenant}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display all your analytics';
/**
* Execute the console command.
*/
public function handle(AnalyticsRepository $analytics, AnalyticPresentor $presentor): void
{
$analytics = $analytics->all();
if (is_string($filter = $this->option('filter'))) {
$analytics = array_filter($analytics, fn (Analytic $analytic): bool => str_contains($analytic->name, $filter));
}
if (! empty($tenant = $this->option('tenant'))) {
$tenant = ctype_digit($tenant) ? (int) $tenant : $tenant;
$analytics = array_filter($analytics, fn (Analytic $analytic): bool => $analytic->tenant === $filter);
}
if ($analytics === []) {
$this->components->info('No analytics have been recorded yet. Get started collecting analytics by adding the [data-pan="my-button"] attribute to your HTML elements.');
return;
}
(new Table($this->output))->display(
['', 'Name', 'Impressions', 'Hovers', 'Clicks'],
array_map(fn (Analytic $analytic): array => array_values($presentor->present($analytic)), $analytics)
);
}
}