Completed
Push — master ( b3e72f...7bead7 )
by Cees-Jan
10:16
created

AsyncTableRegistry::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
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 bool
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->getRegistryAlias(),
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
66
        return static::$tables[$tableName];
67
    }
68 3
69
    public static function paginate($tableName, $params, $settings)
70 3
    {
71 3
        return Pool::getInstance()->paginate($tableName, $params, $settings);
72 3
    }
73
74
    public static function getInstance()
75 3
    {
76
        if (null === self::$instance || self::$reset) {
77
            self::$instance = new static();
78 25
            self::$reset = false;
79
        }
80 25
81 25
        return self::$instance;
82
    }
83
84
    public static function reset()
85
    {
86
        self::$reset = true;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function info()
93
    {
94
        return Pool::getInstance()->info();
95
    }
96
}
97