Completed
Push — develop ( 0a4bcc...0131cc )
by Kirill
25:10
created

Container::fire()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
namespace App\Gitter\Extensions\Middlewares;
3
4
use App\Room;
5
use App\Gitter\Response;
6
use App\Message;
7
use Gitter\Client;
8
use Gitter\Models\Room as GitterRoom;
9
use Illuminate\Contracts\Container\Container as Ioc;
10
11
/**
12
 * Class Container
13
 * @package App\Gitter\Extensions\Middlewares
14
 */
15
class Container implements ContainerInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $items = [];
21
22
    /**
23
     * @var Ioc
24
     */
25
    protected $container;
26
27
    /**
28
     * @var GitterRoom
29
     */
30
    protected $room;
31
32
    /**
33
     * @var Room
34
     */
35
    protected $roomObject;
36
37
    /**
38
     * @var MessageBus
39
     */
40
    protected $messageBus;
41
42
    /**
43
     * Repository constructor.
44
     * @param GitterRoom $room
45
     * @param Ioc $container
46
     */
47
    public function __construct(GitterRoom $room, Ioc $container)
48
    {
49
        $this->room         = $room;
50
        $this->roomObject   = Room::make($this->room);
51
        $this->container    = $container;
52
        $this->messageBus   = new MessageBus;
53
    }
54
55
    /**
56
     * @param $middleware
57
     * @return $this
58
     */
59
    public function register(string $middleware)
60
    {
61
        $this->items[] = $this->container->make($middleware, [
62
            'room'   => $this->roomObject,
63
            'gitter' => $this->room,
64
        ]);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param Message $message
71
     * @return mixed
72
     */
73
    public function fire(Message $message)
74
    {
75
        if (count($this->items)) {
76
            $response = new Response($this->fireMiddleware(0, $message));
77
            if (!$response->isEmpty()) {
78
                if (app(Client::class)->isFallbackMode()) {
79
                    $response->after(
80
                        '_Gitter Stream API недоступен. ' .
81
                        'Я работаю с некоторой задержкой, простите =(_', true);
82
                }
83
84
                $this->messageBus->send($this->room, $response->getContent());
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param int $number
91
     * @param Message $message
92
     * @return mixed
93
     */
94
    protected function fireMiddleware(int $number, Message $message)
95
    {
96
        if (!array_key_exists($number, $this->items)) {
97
            return new Response();
98
        }
99
100
        $middleware = $this->items[$number];
101
102
        return $this->container->call([$middleware, 'handle'], [
103
            'message' => $message,
104
            'next'    => function (Message $message) use ($number) {
105
                $result = $this->fireMiddleware(++$number, $message);
106
107
                if (!($result instanceof Response)) {
108
                    return new Response($result);
109
                }
110
111
                return $result;
112
            },
113
        ]);
114
    }
115
}
116