Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

Storage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 9 1
A handle() 0 15 3
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 24.09.2015 15:27
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Interfaces\Gitter\Middleware;
13
14
use Interfaces\Gitter\Support\PriorityList;
15
use Illuminate\Console\OutputStyle;
16
use Illuminate\Contracts\Container\Container;
17
use Symfony\Component\Console\Output\OutputInterface;
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
    /**
56
     * @param string $class
57
     * @param int $priority
58
     * @return Storage
59
     */
60
    public function add($class, $priority = self::PRIORITY_DEFAULT): Storage
61
    {
62
        $this->container->bind($class, $class);
63
        $instance = $this->container->make($class);
64
65
        $this->storage->insert($instance, $priority);
66
67
        return $this;
68
    }
69
70
71
    /**
72
     * @param Model|mixed $data
73
     * @return Storage
74
     */
75
    public function handle($data): Storage
76
    {
77
        foreach ($this->storage as $middleware) {
78
            $response = $middleware->handle($data);
79
80
            if ($response === static::SIGNAL_STOP) {
81
                return $this;
82
            }
83
84
            // Update data
85
            $data = $response;
86
        }
87
88
        return $this;
89
    }
90
}
91