RevisionableController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 4.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 6
loc 127
rs 10
c 1
b 0
f 0
wmc 16
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
B trackModel() 0 30 3
A getMessage() 6 16 3
A getResponsibleInfo() 0 6 1
A getResponsibleUsername() 0 7 2
A getAffectedResourceName() 0 13 4
A getIconBackground() 0 11 3

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 Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Http\Requests\TrackModelRequest;
6
use Venturecraft\Revisionable\Revision;
7
8
/**
9
 * Class RevisionableController.
10
 *
11
 * @package Acacha\Users\Http\Controllers
12
 *
13
 */
14
class RevisionableController extends Controller
15
{
16
    /**
17
     * Obtain tracking info for a Model.
18
     *
19
     * @param TrackModelRequest $request
20
     * @return array
21
     */
22
    public function trackModel(TrackModelRequest $request)
23
    {
24
        $model = $request->input('model');
25
        $revisions = Revision::where(['revisionable_type' => $model])
26
            ->orderBy('created_at','desv')->get();
27
28
        $result = [];
29
        $lastDate = '';
30
        foreach ($revisions as $revision) {
31
            $date = $revision->created_at->toDateString();
32
            if ($lastDate != $date) {
33
                $result[] = [
34
                    'time' => $revision->created_at->toDateString(),
35
                    'type' => 'time-label'
36
                ];
37
            }
38
            $result[] = [
39
                'time' => $revision->created_at->toTimeString(),
40
                'icon' => 'fa-user',
41
                'iconBackground' => $this->getIconBackground($revision),
42
                'header' => $this->getMessage($revision)
43
44
            ];
45
            $lastDate = $date;
46
        }
47
48
        return $result;
49
50
51
    }
52
53
    /**
54
     * Get icon background.
55
     *
56
     * @param $revision
57
     * @return string
58
     */
59
    private function getMessage($revision)
60
    {
61
        switch ($revision->key) {
62 View Code Duplication
            case 'created_at':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
                return $this->getResponsibleInfo($revision) . ' created user '
64
                    . $this->getAffectedResourceName($revision) . ' (' . $revision->revisionable_id . ')';
65 View Code Duplication
            case 'deleted_at':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
                return $this->getResponsibleInfo($revision) . ' deleted user '
67
                    . $this->getAffectedResourceName($revision) . ' (' . $revision->revisionable_id . ')';
68
            default:
69
                return $this->getResponsibleInfo($revision) . ' changed user '
70
                    . $this->getAffectedResourceName($revision) . ' (' . $revision->revisionable_id . ')'
71
                    . ' field ' .  $revision->fieldName() . ' from ' .$revision->oldValue() . ' to ' .
72
                    $revision->newValue();
73
        }
74
    }
75
76
    /**
77
     * Get responsible info.
78
     *
79
     * @param $revision
80
     * @return string
81
     */
82
    private function getResponsibleInfo($revision)
83
    {
84
        $username = $this->getResponsibleUsername($revision);
85
        return '<span title="'. $username . ' (' . $revision->user_id  . ')' . '" class="responsibleUser" 
86
            model="' . $revision->revisionable_type . '">' . $username . '</span>';
87
    }
88
89
    /**
90
     * Get responsible user name.
91
     *
92
     * @param $revision
93
     * @return string
94
     */
95
    private function getResponsibleUsername($revision)
96
    {
97
        if ($revision->userResponsible()) {
98
            return $revision->userResponsible()->name;
99
        }
100
        return 'Non existing user';
101
    }
102
103
    /**
104
     * Get affected resource name.
105
     *
106
     * @param $revision
107
     * @return string
108
     */
109
    private function getAffectedResourceName($revision)
110
    {
111
        if ($revision->historyOf()) {
112
            return $revision->historyOf()->name;
113
        }
114
        if ($revision->key == 'deleted_at') {
115
            $oldUser = json_decode($revision->old_value);
116
            if($oldUser) {
117
                return $oldUser->name;
118
            }
119
        }
120
        return 'Non existing user';
121
    }
122
123
    /**
124
     * Get icon background.
125
     *
126
     * @param $revision
127
     * @return string
128
     */
129
    private function getIconBackground($revision)
130
    {
131
        switch ($revision->key) {
132
            case 'created_at':
133
                return 'bg-green';
134
            case 'deleted_at':
135
                return 'bg-red';
136
            default:
137
                return 'bg-yellow';
138
        }
139
    }
140
}