Completed
Push — master ( 298e09...565401 )
by Ivannis Suárez
02:36
created

DeferredProxy::__construct()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 16
nc 9
nop 5
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Async\Promise;
13
14
use Cubiche\Core\Delegate\Delegate;
15
16
/**
17
 * Deferred Proxy class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class DeferredProxy implements DeferredInterface
22
{
23
    /**
24
     * @var DeferredInterface
25
     */
26
    protected $deferred;
27
28
    /**
29
     * @var Delegate
30
     */
31
    protected $onFulfilled;
32
33
    /**
34
     * @var Delegate
35
     */
36
    protected $onRejected;
37
38
    /**
39
     * @var Delegate
40
     */
41
    protected $onNotify;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $notifyPropagation;
47
48
    /**
49
     * @param DeferredInterface $deferred
50
     * @param callable          $onFulfilled
51
     * @param callable          $onRejected
52
     * @param callable          $onNotify
53
     * @param bool              $notifyPropagation
54
     *
55
     * @throws \InvalidArgumentException
56
     */
57
    public function __construct(
58
        DeferredInterface $deferred,
59
        callable $onFulfilled = null,
60
        callable $onRejected = null,
61
        callable $onNotify = null,
62
        $notifyPropagation = true
63
    ) {
64
        if (!$deferred->promise()->state()->equals(State::PENDING())) {
65
            throw new \InvalidArgumentException('The deferred target must be unresolved');
66
        }
67
68
        $this->deferred = $deferred;
69
        if ($onFulfilled !== null) {
70
            $this->onFulfilled = new Delegate($onFulfilled);
71
        }
72
        if ($onRejected !== null) {
73
            $this->onRejected = new Delegate($onRejected);
74
        }
75
        if ($onNotify !== null) {
76
            $this->onNotify = new Delegate($onNotify);
77
        }
78
79
        $this->notifyPropagation = $notifyPropagation;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function promise()
86
    {
87
        return $this->deferred->promise();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function resolve($value = null)
94
    {
95
        try {
96
            $actual = $value;
97
            if ($this->onFulfilled !== null) {
98
                $actual = $this->onFulfilled->__invoke($value);
99
            }
100
101
            $this->deferred->resolve($actual !== null ? $actual : $value);
102
        } catch (\Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
103
            $this->reject($e);
104
        } catch (\Exception $e) {
105
            $this->reject($e);
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function reject($reason = null)
113
    {
114
        try {
115
            if ($this->onRejected !== null) {
116
                $this->onRejected->__invoke($reason);
117
            }
118
        } catch (\Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
            $reason = $e;
120
        } catch (\Exception $e) {
121
            $reason = $e;
122
        }
123
124
        $this->deferred->reject($reason);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function notify($state = null)
131
    {
132
        try {
133
            if ($this->onNotify !== null) {
134
                $this->onNotify->__invoke($state);
135
            }
136
            if ($this->notifyPropagation) {
137
                $this->deferred->notify($state);
138
            }
139
        } catch (\Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
140
            $this->reject($e);
141
        } catch (\Exception $e) {
142
            $this->reject($e);
143
        }
144
    }
145
}
146