ReportCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 7 1
A prepareHeader() 0 11 1
A prepareBody() 0 14 2
1
<?php
2
3
namespace Ben182\AbTesting\Commands;
4
5
use Illuminate\Console\Command;
6
use Ben182\AbTesting\Models\Experiment;
7
8
class ReportCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'ab:report';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Shows a table with experiment and goal insights';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
28
     */
29 51
    public function __construct()
30
    {
31 51
        parent::__construct();
32 51
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39 3
    public function handle()
40
    {
41 3
        $header = $this->prepareHeader();
42 3
        $body = $this->prepareBody();
43
44 3
        $this->table($header, $body);
45 3
    }
46
47 3
    public function prepareHeader()
48
    {
49
        $header = [
50 3
            'Experiment',
51
            'Visitors',
52
        ];
53
54
        return array_merge($header, array_map(function ($item) {
55 3
            return 'Goal '.$item;
56 3
        }, config('ab-testing.goals')));
57
    }
58
59 3
    public function prepareBody()
60
    {
61
        return Experiment::all()->map(function ($item) {
62 3
            $return = [$item->name, $item->visitors];
63
64
            $goalConversations = $item->goals->pluck('hit')->map(function ($hit) use ($item) {
65 3
                $item->visitors = $item->visitors ?: 1; // prevent division by zero exception
66
67 3
                return $hit.' ('.number_format($hit / $item->visitors * 100).'%)';
68 3
            });
69
70 3
            return array_merge($return, $goalConversations->toArray());
71 3
        });
72
    }
73
}
74