Completed
Pull Request — master (#5)
by Cees-Jan
02:01
created

AsyncTableRegistry::get()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 7
cts 12
cp 0.5833
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 1
crap 5.1576
1
<?php
2
3
namespace WyriHaximus\React\Cake\Orm;
4
5
use Cake\Core\App;
6
use React\EventLoop\LoopInterface;
7
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface;
8
9
class AsyncTableRegistry implements PoolUtilizerInterface
10
{
11
    /**
12
     * @var AsyncTable[]
13
     */
14
    protected static $tables = [];
15
16
    /**
17
     * @var AsyncTableRegistry
18
     */
19
    protected static $instance = null;
20
21
    /**
22
     * @var boolean
23
     */
24
    protected static $reset = false;
25
26
    /**
27
     * @param LoopInterface $loop
28
     * @param array $config
29
     */
30 2
    public static function init(LoopInterface $loop, array $config = [])
31
    {
32 2
        Pool::getInstance($loop, $config);
33 2
    }
34
35
    /**
36
     * @param $tableName
37
     *
38
     * @return AsyncTable
0 ignored issues
show
Comprehensibility Bug introduced by
The return type AsyncTable is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
39
     */
40 1
    public static function get($tableName)
41
    {
42 1
        if (is_array($tableName)) {
43
            $tableName = $tableName['class'];
44
        }
45
46 1
        if (isset(static::$tables[$tableName])) {
47
            return static::$tables[$tableName];
48
        }
49
50 1
        $table = new $tableName();
51
52 1
        if ($table instanceof AsyncTableInterface) {
53
            $table->setUpAsyncTable(
54
                Pool::getInstance(),
55
                $tableName,
56
                App::className($tableName, 'Model/Table', 'Table')
0 ignored issues
show
Security Bug introduced by
It seems like \Cake\Core\App::classNam...'Model/Table', 'Table') targeting Cake\Core\App::className() can also be of type false; however, WyriHaximus\React\Cake\O...face::setUpAsyncTable() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
57
            );
58
        }
59
60 1
        static::$tables[$tableName] = $table;
61 1
        return static::$tables[$tableName];
62
    }
63
64 4
    public static function getInstance()
65
    {
66 4
        if (null === self::$instance || self::$reset) {
67 4
            self::$instance = new static();
68 4
            self::$reset = false;
69
        }
70
71 4
        return self::$instance;
72
    }
73
74 24
    public static function reset()
75
    {
76 24
        self::$reset = true;
77 24
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82 1
    public function info()
83
    {
84 1
        return Pool::getInstance()->info();
85
    }
86
}
87