1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reallyli\AB\Commands; |
4
|
|
|
|
5
|
|
|
use League\Csv\Writer; |
6
|
|
|
use SplTempFileObject; |
7
|
|
|
use Reallyli\AB\Models\Goal; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Reallyli\AB\Models\Experiment; |
10
|
|
|
use Illuminate\Support\Facades\File; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
|
13
|
|
|
class ExportCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'ab:export'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Export the A/B testing repor to a CSV file.'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new command instance. |
31
|
|
|
* |
32
|
|
|
* @return void |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the console command. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function handle() |
45
|
|
|
{ |
46
|
|
|
$experiments = Experiment::active()->get(); |
|
|
|
|
47
|
|
|
$goals = array_unique(Goal::active()->orderBy('name')->pluck('name')->toArray()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals)); |
50
|
|
|
|
51
|
|
|
$writer = new Writer(new SplTempFileObject); |
52
|
|
|
$writer->insertOne($columns); |
53
|
|
|
|
54
|
|
View Code Duplication |
foreach ($experiments as $experiment) { |
|
|
|
|
55
|
|
|
$engagement = $experiment->visitors ? ($experiment->engagement / $experiment->visitors * 100) : 0; |
56
|
|
|
|
57
|
|
|
$row = [ |
58
|
|
|
$experiment->name, |
59
|
|
|
$experiment->visitors, |
60
|
|
|
number_format($engagement, 2).' % ('.$experiment->engagement.')', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$results = $experiment->goals()->pluck('count', 'name'); |
64
|
|
|
|
65
|
|
|
foreach ($goals as $column) { |
66
|
|
|
$count = array_get($results, $column, 0); |
67
|
|
|
$percentage = $experiment->visitors ? ($count / $experiment->visitors * 100) : 0; |
68
|
|
|
|
69
|
|
|
$row[] = number_format($percentage, 2)." % ($count)"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$writer->insertOne($row); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$output = (string) $writer; |
76
|
|
|
|
77
|
|
|
if ($file = $this->argument('file')) { |
78
|
|
|
$this->info("Creating $file"); |
79
|
|
|
|
80
|
|
|
File::put($file, $output); |
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
$this->line($output); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the console command arguments. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function getArguments() |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
['file', InputArgument::OPTIONAL, 'The target CSV file to write the output to.'], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the console command options. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getOptions() |
104
|
|
|
{ |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.