MaintenanceComponent   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 18
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
C handle() 18 46 7

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
namespace App\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Core\Configure;
6
use Cake\ORM\TableRegistry;
7
8
class MaintenanceComponent extends Component
9
{
10
    /**
11
     * Controller object
12
     *
13
     * @var \Cake\Controller\Controller
14
     */
15
    protected $_controller;
16
17
    /**
18
     * The allowed routes.
19
     *
20
     * @var array
21
     */
22
    protected $_allowedRoutes = [
23
        'Pages.maintenance',
24
        'Users.login',
25
        'Users.logout'
26
    ];
27
28
    /**
29
     * Initialize properties.
30
     *
31
     * @param array $config The config data.
32
     *
33
     * @return void
34
     */
35
    public function initialize(array $config)
36
    {
37
        $this->_controller = $this->_registry->getController();
38
    }
39
40
    /**
41
     * Handle the maintenance mode.
42
     *
43
     * @return false|void
44
     */
45
    public function handle()
46
    {
47
        if (Configure::read('Site.maintenance') !== true) {
48
            return false;
49
        }
50
51
        $controller = $this->request->getParam('controller');
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
52
        $action = $this->request->getParam('action');
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
53
54
        if ($this->_controller->Auth->user()) {
55
            $this->Users = TableRegistry::get('Users');
0 ignored issues
show
Bug introduced by
The property Users does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
            $user = $this->Users
57
                ->find()
58
                ->contain([
59
                    'Groups' => function ($q) {
60
                        return $q->select(['id', 'is_staff']);
61
                    }
62
                ])
63
                ->where([
64
                    'Users.id' => $this->_controller->Auth->user('id')
65
                ])
66
                ->first();
67
68
            if (!is_null($user) && $user->group->is_staff == true) {
69
                //To prevent multiple flash messages.
70
                $this->_controller->Flash->config(['clear' => true]);
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Core\InstanceConfigTrait::config() has been deprecated with message: 3.4.0 use setConfig()/getConfig() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
                $this->_controller->Flash->error(__("Hello {0}, The website is under maintenance, only you and the staff groups have the access !", h($user->full_name)));
72 View Code Duplication
            } else {
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...
73
                if (!in_array($controller . '.' . $action, $this->_allowedRoutes)) {
74
                    $this->_controller->redirect([
75
                        'controller' => 'pages',
76
                        'action' => 'maintenance',
77
                        'prefix' => false
78
                    ]);
79
                }
80
            }
81 View Code Duplication
        } else {
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...
82
            if (!in_array($controller . '.' . $action, $this->_allowedRoutes)) {
83
                $this->_controller->redirect([
84
                    'controller' => 'pages',
85
                    'action' => 'maintenance',
86
                    'prefix' => false
87
                ]);
88
            }
89
        }
90
    }
91
}
92