Completed
Push — master ( 97f3ba...baa78e )
by Kamil
03:56
created

DriverEio::callDelayed()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 3
nop 2
crap 20
1
<?php
2
3
namespace Dazzle\Filesystem\Driver;
4
5
use Dazzle\Filesystem\Invoker\InvokerInterface;
6
use Dazzle\Filesystem\Invoker\InvokerStandard;
7
use Dazzle\Loop\LoopInterface;
8
use Dazzle\Promise\Promise;
9
use Dazzle\Promise\PromiseInterface;
10
use Dazzle\Throwable\Exception\Runtime\ExecutionException;
11
use Dazzle\Throwable\Exception\Runtime\UnexpectedValueException;
12
13
class DriverEio extends DriverAbstract implements DriverInterface
14
{
15
    /**
16
     * @var InvokerInterface
17
     */
18
    protected $invoker;
19
20
    /**
21
     * @var resource
22
     */
23
    protected $stream;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $active = false;
29
30
    /**
31
     * @param LoopInterface $loop
32
     * @param array $options
33
     */
34
    public function __construct(LoopInterface $loop, $options = [])
35
    {
36
        eio_init();
37
        $this->loop = $loop;
38
        $this->options = $this->createConfiguration($options);
39
        $this->invoker = $this->createInvoker();
40
        $this->stream = eio_get_event_stream();
41
    }
42
43
    /**
44
     * @override
45
     * @inheritDoc
46
     */
47
    public function stat($path)
48
    {
49
        return $this->invoker
50
            ->call('eio_lstat', [ $this->getPath($path) ])
51
            ->then([ $this, 'handleStat' ]);
52
    }
53
54
    /**
55
     * @override
56
     * @inheritDoc
57
     */
58
    public function chmod($path, $mode)
59
    {
60
        return $this->invoker
61
            ->call('eio_chmod', [ $this->getPath($path), decoct($mode) ])
62
            ->then([ $this, 'handleChmod' ]);
63
    }
64
65
    /**
66
     * @override
67
     * @inheritDoc
68
     */
69
    public function chown($path, $uid = -1, $gid = -1)
70
    {
71
        return $this->invoker
72
            ->call('eio_chown', [ $this->getPath($path), $uid, $gid ])
73
            ->then([ $this, 'handleChown' ]);
74
    }
75
76
    /**
77
     * @internal
78
     * @override
79
     * @inheritDoc
80
     */
81
    public function call($func, $args = [])
82
    {
83
        $loop = $this->loop;
84
85
        if ($loop->isRunning())
86
        {
87
            return $this->callDelayed($func, $args);
88
        }
89
90
        $promise = new Promise();
91
        $loop->onTick(function() use($func, $args, $promise) {
92
            $this
93
                ->callDelayed($func, $args)
94
                ->then(function($result) use($promise) {
95
                    return $promise->resolve($result);
96
                })
97
                ->failure(function($ex) use($promise) {
98
                    return $promise->reject($ex);
99
                });
100
        });
101
102
        return $promise;
103
    }
104
105
    /**
106
     * @param string $func
107
     * @param array $args
108
     * @return PromiseInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PromiseInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
109
     */
110
    protected function callDelayed($func, $args = [])
111
    {
112
        $this->register();
113
114
        $promise = new Promise();
115
116
        $args[] = EIO_PRI_DEFAULT;
117
        $args[] = function($data, $result, $req) use($func, $args, $promise) {
118
119
            if ($result == -1)
120
            {
121
                $ex = new UnexpectedValueException(@eio_get_last_error($req));
122
                $ex->setContext($args);
123
                return $promise->reject($ex);
124
            }
125
126
            return $promise->resolve($result);
127
        };
128
129
        if (!$func(...$args))
130
        {
131
            $name = is_string($func) ? $func : get_class($func);
132
            $ex = new ExecutionException('Unknown error in response for "' . $name . '"');
133
            $ex->setContext($args);
134
            return $promise->reject($ex);
135
        };
136
137
        return $promise;
138
    }
139
140
    protected function register()
141
    {
142
        if ($this->active) {
143
            return;
144
        }
145
146
        $this->active = true;
147
        $this->loop->addReadStream($this->stream, [ $this, 'handleEvent' ]);
148
    }
149
150
    protected function unregister()
151
    {
152
        if (!$this->active) {
153
            return;
154
        }
155
156
        $this->active = false;
157
        $this->loop->removeReadStream($this->stream);
158
    }
159
160
    public function handleEvent()
161
    {
162
        if ($this->workPendingCount() == 0)
163
        {
164
            return;
165
        }
166
167
        while (eio_npending())
168
        {
169
            eio_poll();
170
        }
171
172
        if ($this->workPendingCount() == 0)
173
        {
174
            $this->unregister();
175
        }
176
    }
177
178
    public function workPendingCount()
179
    {
180
        return eio_nreqs() + eio_npending() + eio_nready();
181
    }
182
183
    /**
184
     * Get path.
185
     *
186
     * @param string $path
187
     * @return string
188
     */
189
    protected function getPath($path)
190
    {
191
        return $this->options['root'] . '/' . ltrim($path, '/');
192
    }
193
194
    /**
195
     * Create valid configuration for the driver.
196
     *
197
     * @param array $options
198
     * @return array
199
     */
200 View Code Duplication
    protected function createConfiguration($options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        return array_merge([
203
            'root' => '',
204
            'invoker.class' => InvokerStandard::class,
205
            'output.control' => false,
206
        ], $options);
207
    }
208
209
    /**
210
     * Create invoker for the driver.
211
     *
212
     * @return InvokerInterface
213
     */
214 View Code Duplication
    protected function createInvoker()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $invoker = class_exists($this->options['invoker.class'])
217
            ? $this->options['invoker.class']
218
            : InvokerStandard::class
219
        ;
220
        return new $invoker($this);
221
    }
222
}
223