Completed
Push — master ( 0cb84b...609616 )
by Maciej
07:36 queued 01:06
created

Pool::unwrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Created by Maciej Paprocki for Bureau-VA.
4
 * Date: 23/02/2016
5
 * Project Name: MaciekPaprocki\WordpressGuzzle
6
 * Time: 17:38.
7
 */
8
namespace BureauVa\WordpressGuzzle;
9
10
use GuzzleHttp\Promise\Promise;
11
12
/**
13
 * Class Pool.
14
 */
15
class Pool
16
{
17
    /**
18
     * @var array
19
     */
20
    private $promises = [];
21
    /**
22
     * @var array
23
     */
24
    private $transformers = [];
25
26
    /**
27
     * Awaits all the requests.
28
     *
29
     * @return mixed
30
     */
31
    public function unwrap()
32
    {
33
        if (empty($this->getPromises())) {
34
            return [];
35
        }
36
37
        return \GuzzleHttp\Promise\unwrap($this->getPromises());
38
    }
39
40
    /**
41
     * @param $query
42
     *
43
     * @return $this
44
     */
45
    public function addPromise($key, Promise $query)
46
    {
47
        $query = $this->attachTransforms($query);
48
        $this->promises[$key] = $query;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function getPromises()
57
    {
58
        return $this->promises;
59
    }
60
61
    /**
62
     * @param array $promise
0 ignored issues
show
Documentation introduced by
There is no parameter named $promise. Did you maybe mean $promises?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
63
     */
64
    public function setPromises($promises)
65
    {
66
        $this->promises = $promises;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getTransformers()
75
    {
76
        return $this->transformers;
77
    }
78
79
    /**
80
     * @param array $transformers
81
     */
82
    public function setTransformers($transformers)
83
    {
84
        $this->transformers = $transformers;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Adds another transformer to collection of transformers.
91
     *
92
     * @param callable $transformer
93
     *
94
     * @return $this
95
     */
96
    public function addTransformer(callable $transformer)
97
    {
98
        $this->transformers[] = $transformer;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param Promise $promise
105
     */
106
    public function attachTransforms(Promise $promise)
107
    {
108
        $transformers = $this->transformers;
109
110
        return $promise->then(function ($res) use ($transformers) {
111
112
            $data = \json_decode((string) $res->getBody());
113
114
            if ($data instanceof \stdClass) {
115
                foreach ($transformers as $transformer) {
116
                    $data = \call_user_func($transformer, $data);
117
                }
118
            } elseif (is_array($data)) {
119
                foreach ($data as $key => $val) {
120
                    foreach ($transformers as $transformer) {
121
                        $data[$key] = \call_user_func($transformer, $val);
122
                    }
123
                }
124
            }
125
126
            return $data;
127
        });
128
    }
129
}
130