Passed
Push — master ( 12b4e1...e176ef )
by Lynh
13:05
created

InteractsWithConfiguration::isLazyEvaluable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jenky\Hermes\Concerns;
4
5
use GuzzleHttp\HandlerStack;
6
use Illuminate\Support\Str;
7
use Jenky\Hermes\LazyEvaluation;
8
9
trait InteractsWithConfiguration
10
{
11
    /**
12
     * Make the Guzzle client options from config.
13
     *
14
     * @param  array $config
15
     * @return array
16
     */
17
    public function makeClientOptions(array $config)
18
    {
19
        $options = $config['options'] ?? [];
20
        $options['handler'] = $options['handler'] ?? $this->createHandler($config);
21
22
        return $options;
23
    }
24
25
    /**
26
     * Create the client handler stack instance.
27
     *
28
     * @param  array $config
29
     * @return \GuzzleHttp\HandlerStack
30
     */
31
    public function createHandler(array $config = [])
32
    {
33
        return $this->prepareHandler(
34
            HandlerStack::create($this->handler($config)), $config
35
        );
36
    }
37
38
    /**
39
     * Get the handle stack's handler instance.
40
     *
41
     * @param  array $config
42
     * @return mixed
43
     */
44
    public function handler(array $config)
45
    {
46
        if (empty($config['handler'])) {
47
            return;
48
        }
49
50
        $factory = is_callable($handler = $config['handler'])
51
            ? $handler
52
            : $this->app->make($handler, $config['with'] ?? []);
53
54
        return $factory($config);
55
    }
56
57
    /**
58
     * Prepare handler stack for usage by Guzzle client.
59
     *
60
     * @param  \GuzzleHttp\HandlerStack $handler
61
     * @param  array $config
62
     * @return \GuzzleHttp\HandlerStack
63
     */
64
    protected function prepareHandler(HandlerStack $handler, array $config = [])
65
    {
66
        foreach ($this->interceptors($config) as [$interceptor, $name]) {
67
            $handler->push($interceptor, $name);
68
        }
69
70
        return $this->tap($handler, $config);
71
    }
72
73
    /**
74
     * Apply the configured taps for the handle stack.
75
     *
76
     * @param  \GuzzleHttp\HandlerStack  $handler
77
     * @param  array  $config
78
     * @return \GuzzleHttp\HandlerStack
79
     */
80
    protected function tap(HandlerStack $handler, array $config = [])
81
    {
82
        foreach ($config['tap'] ?? [] as $tap) {
83
            [$class, $arguments] = $this->parseTap($tap);
84
85
            $this->app->make($class)->__invoke($handler, ...explode(',', $arguments));
86
        }
87
88
        return $handler;
89
    }
90
91
    /**
92
     * Parse the given tap class string into a class name and arguments string.
93
     *
94
     * @param  string  $tap
95
     * @return array
96
     */
97
    protected function parseTap($tap)
98
    {
99
        return Str::contains($tap, ':') ? explode(':', $tap, 2) : [$tap, ''];
100
    }
101
102
    /**
103
     * Parse the given class string into a class name and arguments array.
104
     *
105
     * @param  mixed $key
106
     * @param  mixed $value
107
     * @return array
108
     */
109
    protected function parseClassAndArguments($key, $value)
110
    {
111
        if (is_string($key) && is_array($value)) {
112
            return [$key, $value];
113
        }
114
115
        return [$value, []];
116
    }
117
118
    /**
119
     * Get all interceptors that will be pushed to handle stack instance.
120
     *
121
     * @param  array $config
122
     * @return array
123
     */
124
    public function interceptors(array $config)
125
    {
126
        $interceptors = [];
127
128
        foreach ($config['interceptors'] ?? [] as $key => $value) {
129
            $interceptors[] = $this->parseInterceptor($key, $value);
130
        }
131
132
        return $interceptors;
133
    }
134
135
    /**
136
     * Parse the given middleware and create middleware instance with it's parameters.
137
     *
138
     * @param  mixed $key
139
     * @param  mixed $value
140
     * @return array
141
     */
142
    protected function parseInterceptor($key, $value)
143
    {
144
        $name = is_numeric($key) ? '' : $key;
145
146
        if (is_callable($value)) {
147
            // $value = $this->isLazyEvaluable($value) ? $this->app->call($value) : $value;
148
            $value = $this->isLazyEvaluable($value) ? $value() : $value;
149
150
            return [$value, $name];
151
        }
152
153
        [$class, $arguments] = $this->parseClassAndArguments($key, $value);
154
155
        return [$this->app->make($class, $arguments), $class];
156
    }
157
158
    /**
159
     * Determine if the callable is lazy evaluated.
160
     *
161
     * @return bool
162
     */
163
    protected function isLazyEvaluable(callable $callable)
164
    {
165
        return $callable instanceof LazyEvaluation;
166
    }
167
}
168