Completed
Push — master ( eb6b30...d2126b )
by Vojta
02:24
created

Registrations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 0
dl 72
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 12 12 2
A defineProperties() 18 18 1
B loadData() 33 33 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace VojtaSvoboda\UserAccessLog\ReportWidgets;
4
5
use App;
6
use ApplicationException;
7
use Carbon\Carbon;
8
use Exception;
9
use Backend\Classes\ReportWidgetBase;
10
use RainLab\User\Models\User;
11
12
/**
13
 * Registrations overview widget.
14
 *
15
 * @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets
16
 */
17 View Code Duplication
class Registrations extends ReportWidgetBase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /**
20
     * Renders the widget.
21
     */
22
    public function render()
23
    {
24
        try {
25
            $this->vars['registrations'] = $this->loadData();
26
27
        } catch (Exception $ex) {
28
            $this->vars['error'] = $ex->getMessage();
29
            $this->vars['registrations'] = [];
30
        }
31
32
        return $this->makePartial('widget');
33
    }
34
35
    public function defineProperties()
36
    {
37
        return [
38
            'title' => [
39
                'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title',
40
                'default' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title_default',
41
                'type' => 'string',
42
                'validationPattern' => '^.+$',
43
                'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title_validation',
44
            ],
45
            'days' => [
46
                'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.days_title',
47
                'default' => '30',
48
                'type' => 'string',
49
                'validationPattern' => '^[0-9]+$',
50
            ]
51
        ];
52
    }
53
54
    protected function loadData()
55
    {
56
        $days = $this->property('days');
57
        if (!$days) {
58
            throw new ApplicationException('Invalid days value: ' . $days);
59
        }
60
61
        // all accesses for last month
62
        $items = User::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get();
63
64
        // parse data
65
        $all = [];
66
        foreach ($items as $item)
67
        {
68
            // date
69
            $timestamp = strtotime($item->created_at) * 1000;
70
            $day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d');
71
72
            if (isset($all[$day])) {
73
                $all[$day][1]++;
74
            } else {
75
                $all[$day] = [$timestamp, 1];
76
            }
77
        }
78
79
        // count all
80
        $all_render = [];
81
        foreach ($all as $a) {
82
            $all_render[] = [$a[0], $a[1]];
83
        }
84
85
        return $all_render;
86
    }
87
88
}
89