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 (?) |
|
|
|
|
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)) { |
|
|
|
|
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
|
|
|
|
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.