Completed
Pull Request — master (#6)
by Cees-Jan
06:19
created

WorkerChild   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 9
dl 0
loc 72
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A create() 0 11 1
A handleTableCall() 0 29 3
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Cake\Orm;
4
5
use Cake\Cache\Cache;
6
use Cake\Core\Configure;
7
use Cake\Datasource\ConnectionManager;
8
use Cake\ORM\Query;
9
use Cake\ORM\TableRegistry;
10
use React\EventLoop\LoopInterface;
11
use React\Promise\Deferred;
12
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface;
13
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
14
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
15
16
final class WorkerChild implements ChildInterface
17
{
18
    private $messenger;
19
    private $loop;
20
21
    /**
22
     * WorkerChild constructor.
23
     */
24
    public function __construct(Messenger $messenger, LoopInterface $loop)
25
    {
26
        $this->messenger = $messenger;
27
        $this->loop = $loop;
28
29
        $this->messenger->registerRpc('table.call', function (Payload $payload) {
30
            $deferred = new Deferred();
31
            $this->loop->futureTick(function () use ($payload, $deferred) {
32
                $this->handleTableCall($payload, $deferred);
33
            });
34
35
            return $deferred->promise();
36
        });
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public static function create(Messenger $messenger, LoopInterface $loop)
43
    {
44
        require dirname(dirname(dirname(dirname(__DIR__)))) . '/config/paths.php';
45
        require CORE_PATH . 'config' . DS . 'bootstrap.php';
46
        Configure::config('default', new Configure\Engine\PhpConfig());
47
        Configure::load('app', 'default', false);
48
        Cache::setConfig(Configure::consume('Cache'));
0 ignored issues
show
Bug introduced by
It seems like \Cake\Core\Configure::consume('Cache') targeting Cake\Core\Configure::consume() can also be of type null; however, Cake\Core\StaticConfigTrait::setConfig() does only seem to accept string|array, 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...
49
        ConnectionManager::setConfig(Configure::consume('Datasources'));
0 ignored issues
show
Bug introduced by
It seems like \Cake\Core\Configure::consume('Datasources') targeting Cake\Core\Configure::consume() can also be of type null; however, Cake\Datasource\ConnectionManager::setConfig() does only seem to accept string|array, 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...
50
51
        return new self($messenger, $loop);
52
    }
53
54
    /**
55
     * @param Payload  $payload
56
     * @param Deferred $deferred
57
     */
58
    protected function handleTableCall(Payload $payload, Deferred $deferred)
59
    {
60
        $result = call_user_func_array([
61
            TableRegistry::get(
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
                $payload['table']/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
                [
64
                    'className' => $payload['className'],
65
                    'table' => $payload['table'],
66
                ]*/
67
            ),
68
            $payload['function'],
69
        ], unserialize($payload['arguments']));
70
71
        if (!($result instanceof Query)) {
72
            $deferred->resolve([
73
                'result' => serialize($result),
74
            ]);
75
76
            return;
77
        }
78
79
        foreach ($result->all() as $row) {
80
            $deferred->notify([
0 ignored issues
show
Deprecated Code introduced by
The method React\Promise\Deferred::notify() has been deprecated with message: 2.6.0 Progress support is deprecated and should not be used anymore.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
                'row' => $row,
82
            ]);
83
        }
84
85
        $deferred->resolve();
86
    }
87
}
88