Completed
Push — master ( 375de1...c13e1e )
by Andrii
13:21
created

CommonBehavior   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 34.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 2
dl 25
loc 72
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 7 1
A onBeforeAction() 0 4 1
A onAfterAction() 0 4 1
A runRequests() 11 11 4
B normalizeTasks() 14 14 6
A isNotOk() 0 4 2
A runRequest() 0 9 2

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
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\controllers;
12
13
use Yii;
14
use yii\console\Request;
15
16
/**
17
 * Common controller behavior.
18
 */
19
class CommonBehavior extends \yii\base\Behavior
20
{
21
    public function events()
22
    {
23
        return [
24
            CommonController::EVENT_BEFORE_ACTION => 'onBeforeAction',
25
            CommonController::EVENT_AFTER_ACTION  => 'onAfterAction',
26
        ];
27
    }
28
29
    public function onBeforeAction($event)
30
    {
31
        $this->runRequests($event->sender->before);
32
    }
33
34
    public function onAfterAction($event)
35
    {
36
        $this->runRequests($event->sender->after);
37
    }
38
39 View Code Duplication
    public function runRequests($requests)
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...
40
    {
41
        foreach ($this->normalizeTasks($requests) as $request => $enabled) {
42
            if ($enabled) {
43
                $response = $this->runRequest($request);
44
                if ($this->isNotOk($response)) {
45
                    return $response;
46
                }
47
            }
48
        }
49
    }
50
51 View Code Duplication
    public function normalizeTasks($tasks)
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...
52
    {
53
        if (!$tasks) {
54
            return [];
55
        } elseif (!is_array($tasks)) {
56
            $tasks = [(string) $tasks => 1];
57
        }
58
        $res = [];
59
        foreach ($tasks as $dep => $enabled) {
60
            $res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled);
61
        }
62
63
        return $res;
64
    }
65
66
    /**
67
     * Is response NOT Ok.
68
     * @param Response|int $response
69
     * @return bool
70
     */
71
    public function isNotOk($response)
72
    {
73
        return is_object($response) ? $response->exitStatus : $response;
74
    }
75
76
    /**
77
     * Run request.
78
     * @param string|array $query
79
     * @return Response
80
     */
81
    public function runRequest($query)
82
    {
83
        $request = Yii::createObject([
84
            'class'  => Request::class,
85
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
86
        ]);
87
88
        return Yii::$app->handleRequest($request);
89
    }
90
}
91