Completed
Push — master ( 19ca72...4bd58a )
by Kirill
02:15
created

Manager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 18 4
A get() 0 4 1
A getEventLoop() 0 4 1
A connect() 0 4 1
1
<?php
2
/**
3
 * This file is part of Platform package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Karma\Platform;
11
12
use Psr\Log\LoggerInterface;
13
use React\EventLoop\LoopInterface;
14
use React\EventLoop\Factory as EventLoop;
15
use Karma\Platform\Io\SystemInterface;
16
17
/**
18
 * Class Manager
19
 * @package Karma\Platform
20
 */
21
class Manager
22
{
23
    /**
24
     * @var LoopInterface
25
     */
26
    private $loop;
27
28
    /**
29
     * @var LoggerInterface|null
30
     */
31
    private $logger;
32
33
    /**
34
     * @var array|SystemInterface[]
35
     */
36
    private $systems = [];
37
38
    /**
39
     * Factory constructor.
40
     * @param LoopInterface|null $loop
41
     * @param LoggerInterface|null $logger
42
     */
43
    public function __construct(LoopInterface $loop = null, LoggerInterface $logger = null)
44
    {
45
        $this->loop = $loop ?? EventLoop::create();
46
        $this->logger = $logger;
47
    }
48
49
    /**
50
     * @param SystemInterface $system
51
     * @param null|string $alias
52
     * @return Manager
53
     */
54
    public function register(SystemInterface $system, ?string $alias = null): Manager
55
    {
56
        if ($alias !== null) {
57
            $this->systems[$alias] = $system;
58
        }
59
60
        if (!isset($this->systems[$system->getName()])) {
61
            $this->systems[$system->getName()] = $system;
62
        }
63
64
        if (!isset($this->systems[get_class($system)])) {
65
            $this->systems[get_class($system)] = $system;
66
        }
67
68
        $system->onRegister($this->loop, $this->logger);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $system
75
     * @return SystemInterface
76
     */
77
    public function get(string $system): SystemInterface
78
    {
79
        return $this->systems[$system];
80
    }
81
82
    /**
83
     * @return LoopInterface
84
     */
85
    public function getEventLoop(): LoopInterface
86
    {
87
        return $this->loop;
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    public function connect(): void
94
    {
95
        $this->loop->run();
96
    }
97
}
98