Reservations   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 135
Duplicated Lines 11.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 16
loc 135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A index() 0 5 1
A listOverrideColumnValue() 0 15 4
A index_onBulkAction() 0 15 6
A isReturning() 0 4 1
A getFacade() 0 4 1
A getStateColor() 8 8 3
A getStateName() 8 8 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 namespace VojtaSvoboda\Reservations\Controllers;
2
3
use App;
4
use Backend\Classes\Controller;
5
use BackendMenu;
6
use Flash;
7
use Lang;
8
use VojtaSvoboda\Reservations\Facades\ReservationsFacade;
9
use VojtaSvoboda\Reservations\Models\Reservation;
10
use VojtaSvoboda\Reservations\Models\Status;
11
12
class Reservations extends Controller
13
{
14
    public $implement = [
15
        'Backend\Behaviors\ListController',
16
        'Backend\Behaviors\FormController',
17
        'Backend\Behaviors\ImportExportController',
18
    ];
19
20
    public $listConfig = 'config_list.yaml';
21
    public $formConfig = 'config_form.yaml';
22
    public $importExportConfig = 'config_import_export.yaml';
23
24
    private $stateColors;
25
    private $stateNames;
26
27
    public $requiredPermissions = [
28
        'vojtasvoboda.reservations.reservations',
29
    ];
30
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        $action = $this->action == 'export' ? 'export' : 'reservations';
36
        BackendMenu::setContext('VojtaSvoboda.Reservations', 'reservations', $action);
37
    }
38
39
    /**
40
     * Extend controller listing.
41
     */
42
    public function index()
43
    {
44
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/bulk-actions.js');
45
        $this->asExtension('ListController')->index();
46
    }
47
48
    /**
49
     * Override displaying reservation listing.
50
     *
51
     * @param Reservation $record
52
     * @param string $columnName
53
     *
54
     * @return string
55
     */
56
    public function listOverrideColumnValue($record, $columnName)
57
    {
58
        $statusStyle = "display:inline-block;width:13px;height:14px;position:relative;top:2px;margin-right:4px;color:#fff;font-size:11px;padding-left:3px;";
59
60
        if ($columnName == 'status_id') {
61
            $color = $this->getStateColor($record->status_id);
62
            $name = $this->getStateName($record->status_id);
63
64
            return '<div><span style="' . $statusStyle . 'background-color:' . $color . '"></span> ' . $name . '</div>';
65
        }
66
67
        if ($columnName == 'returning') {
68
            return $this->isReturning($record->email) ? '<i class=" icon-star"></i>' : '';
69
        }
70
    }
71
72
    /**
73
     * Bulk change state and bulk delete.
74
     *
75
     * @return mixed
76
     */
77
    public function index_onBulkAction()
78
    {
79
        if (($bulkAction = post('action')) && ($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
80
            if ($bulkAction == 'delete') {
81
                $this->getFacade()->bulkDelete($checkedIds);
82
            } else {
83
                $this->getFacade()->bulkStateChange($checkedIds, $bulkAction);
84
            }
85
86
            $msg = Lang::get('vojtasvoboda.reservations::lang.reservations.change_status_success');
87
            Flash::success($msg);
88
        }
89
90
        return $this->listRefresh();
91
    }
92
93
    /**
94
     * Return if User is returning.
95
     *
96
     * @param string $email Users email.
97
     *
98
     * @return bool
99
     */
100
    private function isReturning($email)
101
    {
102
        return $this->getFacade()->isUserReturning($email);
103
    }
104
105
    /**
106
     * Get facade providing all plugin logic.
107
     *
108
     * @return ReservationsFacade
109
     */
110
    private function getFacade()
111
    {
112
        return App::make(ReservationsFacade::class);
113
    }
114
115
    /**
116
     * Get color by state ident.
117
     *
118
     * @param string $ident
119
     *
120
     * @return string|null
121
     */
122 View Code Duplication
    private function getStateColor($ident)
0 ignored issues
show
Duplication introduced by
This method 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...
123
    {
124
        if ($this->stateColors === null) {
125
            $this->stateColors = Status::lists('color', 'ident');
126
        }
127
128
        return isset($this->stateColors[$ident]) ? $this->stateColors[$ident] : null;
129
    }
130
131
    /**
132
     * Get name by state ident.
133
     *
134
     * @param string $ident
135
     *
136
     * @return string|null
137
     */
138 View Code Duplication
    private function getStateName($ident)
0 ignored issues
show
Duplication introduced by
This method 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...
139
    {
140
        if ($this->stateNames === null) {
141
            $this->stateNames = Status::lists('name', 'ident');
142
        }
143
144
        return isset($this->stateNames[$ident]) ? $this->stateNames[$ident] : null;
145
    }
146
}
147