1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reallyli\AB\Commands; |
4
|
|
|
|
5
|
|
|
use Reallyli\AB\Models\Goal; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Reallyli\AB\Models\Experiment; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
|
10
|
|
|
class ReportCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'ab:report'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Print the A/B testing report.'; |
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
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
$experiments = Experiment::active()->get(); |
|
|
|
|
44
|
|
|
$goals = array_unique(Goal::active()->orderBy('name')->pluck('name')->toArray()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals)); |
47
|
|
|
|
48
|
|
|
$table = new Table($this->output); |
49
|
|
|
$table->setHeaders($columns); |
50
|
|
|
|
51
|
|
View Code Duplication |
foreach ($experiments as $experiment) { |
|
|
|
|
52
|
|
|
$engagement = $experiment->visitors ? ($experiment->engagement / $experiment->visitors * 100) : 0; |
53
|
|
|
|
54
|
|
|
$row = [ |
55
|
|
|
$experiment->name, |
56
|
|
|
$experiment->visitors, |
57
|
|
|
number_format($engagement, 2).' % ('.$experiment->engagement.')', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$results = $experiment->goals()->pluck('count', 'name'); |
61
|
|
|
|
62
|
|
|
foreach ($goals as $column) { |
63
|
|
|
$count = array_get($results, $column, 0); |
64
|
|
|
$percentage = $experiment->visitors ? ($count / $experiment->visitors * 100) : 0; |
65
|
|
|
|
66
|
|
|
$row[] = number_format($percentage, 2)." % ($count)"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$table->addRow($row); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$table->render(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the console command arguments. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getArguments() |
81
|
|
|
{ |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the console command options. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function getOptions() |
91
|
|
|
{ |
92
|
|
|
return []; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.