Code Duplication    Length = 72-77 lines in 2 locations

reportwidgets/AccessLogChartLineAggregated.php 1 location

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

reportwidgets/Registrations.php 1 location

@@ 17-88 (lines=72) @@
14
 *
15
 * @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets
16
 */
17
class Registrations extends ReportWidgetBase
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