Completed
Push — master ( d56647...5f1d83 )
by Kirill
11s
created

Storage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 107
rs 10
wmc 13
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 17 4
A handle() 0 15 3
B checkMiddlewareGroup() 0 23 5
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @author butschster <[email protected]>
7
 *
8
 * @date 24.09.2015 15:27
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Domains\Middleware;
15
16
use Illuminate\Contracts\Container\Container;
17
use Interfaces\Gitter\Support\PriorityList;
18
19
/**
20
 * Class Storage
21
 */
22
class Storage
23
{
24
    const PRIORITY_MINIMAL = 1; // Lower priority
25
    const PRIORITY_DEFAULT = 2;
26
    const PRIORITY_MAXIMAL = 3; // Maximal priority (?)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
28
29
    // Middleware response for stopping iterations
30
    const SIGNAL_STOP = null;
31
32
33
    /**
34
     * @var MiddlewareInterface[]|PriorityList
35
     */
36
    protected $storage;
37
38
39
    /**
40
     * @var Container
41
     */
42
    protected $container;
43
44
45
    /**
46
     * @param Container $container
47
     */
48
    public function __construct(Container $container)
49
    {
50
        $this->container = $container;
51
        $this->storage = new PriorityList();
52
    }
53
54
    /**
55
     * @param string $class
56
     * @param int $priority
57
     *
58
     * @return Storage
59
     * @throws \Exception
60
     */
61
    public function add($class, $priority = self::PRIORITY_DEFAULT): Storage
62
    {
63
        $this->container->bind($class, $class);
64
        $instance = $this->container->make($class);
65
66
        if (! ($instance instanceof MiddlewareInterface)) {
67
            throw new \Exception("Class [{$class}] must be instance of Interfaces\\Gitter\\Middleware\\MiddlewareInterface");
68
        }
69
70
        if ($instance instanceof MiddlewareGroupableInterface and ! $this->checkMiddlewareGroup($instance)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
71
            return $this;
72
        }
73
74
        $this->storage->insert($instance, $priority);
75
76
        return $this;
77
    }
78
79
80
    /**
81
     * @param Model|mixed $data
82
     * @return Storage
83
     */
84
    public function handle($data): Storage
85
    {
86
        foreach ($this->storage as $middleware) {
87
            $response = $middleware->handle($data);
88
89
            if ($response === static::SIGNAL_STOP) {
90
                return $this;
91
            }
92
93
            // Update data
94
            $data = $response;
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param MiddlewareGroupableInterface $middleware
102
     *
103
     * @return bool
104
     */
105
    protected function checkMiddlewareGroup(MiddlewareGroupableInterface $middleware)
106
    {
107
        $groups = (array) $middleware->getGroup();
108
        $currentGroups = \Config::get('gitter.env');
109
110
        if (is_string($currentGroups)) {
111
            $currentGroups = array_map(function ($item) {
112
                return trim($item);
113
            }, explode(',', $currentGroups));
114
        }
115
116
        if (! is_array($currentGroups)) {
117
            return true;
118
        }
119
120
        foreach ($currentGroups as $group) {
121
            if (in_array($group, $groups)) {
122
                return true;
123
            }
124
        }
125
126
        return false;
127
    }
128
}
129