CallablePromisor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A promise() 0 4 1
A execute() 0 8 2
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
use Cubiche\Core\Delegate\Delegate;
15
16
/**
17
 * Callable Promisor class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class CallablePromisor extends Delegate implements PromisorInterface
22
{
23
    /**
24
     * @var Delegate
25
     */
26
    protected $delegate;
27
28
    /**
29
     * @var DeferredInterface
30
     */
31
    protected $deferred = null;
32
33
    /**
34
     * @param callable $callable
35
     */
36
    public function __construct(callable $callable)
37
    {
38
        parent::__construct(array($this, 'execute'));
39
40
        $this->delegate = new Delegate($callable);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function promise()
47
    {
48
        return $this->deferred()->promise();
49
    }
50
51
    protected function execute()
52
    {
53
        try {
54
            $this->deferred()->resolve($this->delegate->invokeWith(\func_get_args()));
55
        } catch (\Exception $e) {
56
            $this->deferred()->reject($e);
57
        }
58
    }
59
60
    /**
61
     * @return \Cubiche\Core\Async\Promise\DeferredInterface
62
     */
63
    protected function deferred()
64
    {
65
        if ($this->deferred === null) {
66
            $this->deferred = new Deferred();
67
        }
68
69
        return $this->deferred;
70
    }
71
}
72