Completed
Push — master ( b72795...847518 )
by Ivannis Suárez
02:24
created

ThenablePromisor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A promise() 0 4 1
A deferred() 0 8 2
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
/**
15
 * Thenable Promisor class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class ThenablePromisor implements PromisorInterface
20
{
21
    /**
22
     * @var DeferredInterface
23
     */
24
    protected $deferred = null;
25
26
    /*
27
     * @param ThenableInterface $thenable
28
     */
29
    public function __construct(ThenableInterface $thenable)
30
    {
31
        $thenable->then(function ($value) {
32
            $this->deferred()->resolve($value);
33
        }, function ($reason) {
34
            $this->deferred()->reject($reason);
35
        }, function ($state) {
36
            $this->deferred()->notify($state);
37
        });
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function promise()
44
    {
45
        return $this->deferred()->promise();
46
    }
47
48
    /**
49
     * @return \Cubiche\Core\Async\Promise\DeferredInterface
50
     */
51
    protected function deferred()
52
    {
53
        if ($this->deferred === null) {
54
            $this->deferred = new Deferred();
55
        }
56
57
        return $this->deferred;
58
    }
59
}
60