All::apResolving()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 3
c 1
b 0
f 1
nc 6
nop 3
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * All
4
 * User: moyo
5
 * Date: 04/08/2017
6
 * Time: 11:04 AM
7
 */
8
9
namespace Carno\Promise\Features;
10
11
use Carno\Promise\Promise;
12
use Carno\Promise\Promised;
13
use Carno\Promise\Stacked;
14
15
trait All
16
{
17
    /**
18
     * @var array
19
     */
20
    private $resolved = [];
21
22
    /**
23
     * @param Promised ...$promises
24
     * @return Promised
25
     */
26
    public static function all(Promised ...$promises) : Promised
27
    {
28
        if (empty($promises)) {
29
            return Promise::resolved();
30
        }
31
32
        /**
33
         * @var Promise $all
34
         */
35
36
        $all = Promise::deferred();
37
38
        $sid = Stacked::in(...$promises);
39
40
        foreach ($promises as $pid => $promise) {
41
            $promise->then(static function (...$args) use ($all, $sid, $pid) {
42
                $all->pended() && $all->apResolving($sid, $pid, ...$args);
43
            }, static function (...$args) use ($all) {
44
                $all->pended() && $all->reject(...$args);
45
            });
46
        }
47
48
        $all->then(null, static function (...$args) use ($sid) {
49
            foreach (Stacked::out($sid) as $promise) {
50
                $promise->pended() && $promise->reject(...$args);
51
            }
52
        });
53
54
        return $all;
55
    }
56
57
    /**
58
     * @param int $sid
59
     * @param int $pid
60
     * @param mixed ...$args
61
     */
62
    private function apResolving(int $sid, int $pid, ...$args)
63
    {
64
        $this->resolved[$pid] = count($args) > 1 ? $args : current($args);
65
66
        if (count($this->resolved) === Stacked::num($sid)) {
67
            Stacked::out($sid) && $this->resolve($this->resolved);
0 ignored issues
show
Bug introduced by
It seems like resolve() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            Stacked::out($sid) && $this->/** @scrutinizer ignore-call */ resolve($this->resolved);
Loading history...
68
        }
69
    }
70
}
71