Completed
Push — master ( cb5fa1...8201f5 )
by Vojta
06:45
created

Reservations::index_onBulkAction()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 3
nop 0
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\Status;
10
11
class Reservations extends Controller
12
{
13
    public $implement = [
14
        'Backend\Behaviors\ListController',
15
        'Backend\Behaviors\FormController',
16
        'Backend\Behaviors\ImportExportController',
17
    ];
18
19
    public $listConfig = 'config_list.yaml';
20
    public $formConfig = 'config_form.yaml';
21
    public $importExportConfig = 'config_import_export.yaml';
22
23
    private $stateColors;
24
    private $stateNames;
25
26
    public $requiredPermissions = [
27
        'vojtasvoboda.reservations.reservations',
28
    ];
29
30
    public function __construct()
31
    {
32
        parent::__construct();
33
34
        $action = $this->action == 'export' ? 'export' : 'reservations';
35
        BackendMenu::setContext('VojtaSvoboda.Reservations', 'reservations', $action);
36
    }
37
38
    public function index()
39
    {
40
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/bulk-actions.js');
41
42
        $this->asExtension('ListController')->index();
43
    }
44
45
    /**
46
     * Override displaying reservation status.
47
     *
48
     * @param $record
49
     * @param $columnName
50
     * @param null $definition
51
     *
52
     * @return string
53
     */
54
    public function listOverrideColumnValue($record, $columnName, $definition = null)
55
    {
56
        $statusStyle = "display:inline-block;width:13px;height:14px;position:relative;top:2px;margin-right:4px;color:#fff;font-size:11px;padding-left:3px;";
57
58
        if ($columnName == 'status_id') {
59
            $color = $this->getStateColor($record->status_id);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $color is correct as $this->getStateColor($record->status_id) (which targets VojtaSvoboda\Reservation...ations::getStateColor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
            $name = $this->getStateName($record->status_id);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $name is correct as $this->getStateName($record->status_id) (which targets VojtaSvoboda\Reservation...vations::getStateName()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
62
            return '<div><span style="' . $statusStyle . 'background-color:' . $color . '"></span> ' . $name . '</div>';
63
64
        } elseif ($columnName == 'returning') {
65
            return $this->isReturning($record->email) ? '<i class=" icon-star"></i>' : '';
66
        }
67
    }
68
69
    /**
70
     * Bulk actions
71
     *
72
     * @return mixed
73
     */
74
    public function index_onBulkAction()
75
    {
76
        if (($bulkAction = post('action')) && ($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds))
77
        {
78
            /** @var ReservationsFacade $facade */
79
            $facade = App::make('vojtasvoboda.reservations.facade');
80
81
            if ($bulkAction == 'delete') {
82
                $facade->bulkDelete($checkedIds);
83
            } else {
84
                $facade->bulkStateChange($checkedIds, $bulkAction);
85
            }
86
87
            $msg = Lang::get('vojtasvoboda.reservations::lang.reservations.change_status_success');
88
            Flash::success($msg);
89
        }
90
91
        return $this->listRefresh();
92
    }
93
94
    /**
95
     * Return if User is returning.
96
     *
97
     * @param string $email Users email
98
     *
99
     * @return bool
100
     */
101
    private function isReturning($email)
102
    {
103
        /** @var ReservationsFacade $facade */
104
        $facade = App::make('vojtasvoboda.reservations.facade');
105
106
        return $facade->isUserReturning($email);
107
    }
108
109
    /**
110
     * Get color by state ident.
111
     *
112
     * @param $ident
113
     *
114
     * @return null
115
     */
116 View Code Duplication
    private function getStateColor($ident)
1 ignored issue
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...
117
    {
118
        if ($this->stateColors === null) {
119
            $this->stateColors = Status::lists('color', 'ident');
120
        }
121
122
        return isset($this->stateColors[$ident]) ? $this->stateColors[$ident] : null;
123
    }
124
125
    /**
126
     * Get name by state ident.
127
     *
128
     * @param $ident
129
     *
130
     * @return null
131
     */
132 View Code Duplication
    private function getStateName($ident)
1 ignored issue
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...
133
    {
134
        if ($this->stateNames === null) {
135
            $this->stateNames = Status::lists('name', 'ident');
136
        }
137
138
        return isset($this->stateNames[$ident]) ? $this->stateNames[$ident] : null;
139
    }
140
}
141