Completed
Push — master ( 843b3f...a4441d )
by Benjamin
02:53
created

ReportCommand::prepareBody()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 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 = 'Command description';
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 42
    public function __construct()
30
    {
31 42
        parent::__construct();
32 42
    }
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
        $header = [
49 3
            'Experiment',
50
            'Visitors',
51
        ];
52
53 2
        return array_merge($header, array_map(function ($item) {
54 3
            return 'Goal '.$item;
55 3
        }, config('ab-testing.goals')));
56
    }
57
58 1
    public function prepareBody() {
59 2
        return Experiment::all()->map(function ($item) {
60 3
            $return = [$item->name, $item->visitors];
61
62 2
            $goalConversations = $item->goals->pluck('hit')->map(function ($hit) use ($item) {
63 3
                $item->visitors = $item->visitors ?: 1; // prevent division by zero exception
64
65 3
                return $hit.' ('.number_format($hit / $item->visitors * 100).'%)';
66 3
            });
67
68 3
            return array_merge($return, $goalConversations->toArray());
69 3
        });
70
    }
71
}
72