Completed
Pull Request — master (#5)
by Cees-Jan
03:18
created

AsyncTableRegistry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 0
loc 80
ccs 8
cts 28
cp 0.2857
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B get() 0 25 3
A getInstance() 0 9 3
A reset() 0 4 1
A info() 0 4 1
1
<?php
2
3
namespace WyriHaximus\React\Cake\Orm;
4
5
use Cake\Core\App;
6
use Cake\Core\Configure;
7
use Cake\ORM\Table;
8
use React\EventLoop\LoopInterface;
9
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface;
10
11
class AsyncTableRegistry implements PoolUtilizerInterface
12
{
13
    /**
14
     * @var AsyncTable[]
15
     */
16
    protected static $tables = [];
17
18
    /**
19
     * @var AsyncTableRegistry
20
     */
21
    protected static $instance = null;
22
23
    /**
24
     * @var boolean
25
     */
26
    protected static $reset = false;
27
28
    /**
29
     * @param LoopInterface $loop
30
     * @param array $config
31
     */
32
    public static function init(LoopInterface $loop, array $config = [])
33
    {
34
        Pool::getInstance($loop, $config);
35
    }
36
37
    /**
38
     * @param Table $table
39
     *
40
     * @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...
41
     */
42
    public static function get(Table $table)
43
    {
44
        $tableName = get_class($table);
45
46
        if (isset(static::$tables[$tableName])) {
47
            return static::$tables[$tableName];
48
        }
49
50
        $asyncTableName = (new AsyncTableGenerator(
51
            Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables')
52
        ))->generate($tableName)->getFQCN();
53
54
        $asyncTable = new $asyncTableName();
55
56
        if ($asyncTable instanceof AsyncTableInterface) {
57
            $asyncTable->setUpAsyncTable(
58
                Pool::getInstance(),
59
                $table->table(),
0 ignored issues
show
Bug introduced by
It seems like $table->table() targeting Cake\ORM\Table::table() can also be of type boolean; however, WyriHaximus\React\Cake\O...face::setUpAsyncTable() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
                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...
61
            );
62
        }
63
64
        static::$tables[$tableName] = $asyncTable;
65
        return static::$tables[$tableName];
66
    }
67
68 3
    public static function getInstance()
69
    {
70 3
        if (null === self::$instance || self::$reset) {
71 3
            self::$instance = new static();
72 3
            self::$reset = false;
73
        }
74
75 3
        return self::$instance;
76
    }
77
78 25
    public static function reset()
79
    {
80 25
        self::$reset = true;
81 25
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function info()
87
    {
88
        return Pool::getInstance()->info();
89
    }
90
}
91