Completed
Push — master ( 8dbfca...31162d )
by Hannes
02:51
created

ServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 10%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 63
ccs 2
cts 20
cp 0.1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 8 1
A register() 0 40 3
1
<?php
2
namespace GuzzleHttp\Profiling\Clockwork\Support\Laravel;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\MessageFormatter;
8
use GuzzleHttp\Middleware as GuzzleMiddleware;
9
use GuzzleHttp\Profiling\Clockwork\Profiler;
10
use GuzzleHttp\Profiling\Middleware;
11
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
12
13
class ServiceProvider extends BaseServiceProvider
14
{
15
    /**
16
     * @var bool
17
     */
18
    protected $defer = true;
19
20
    /**
21
     * @return array
22
     */
23 1
    public function provides()
24
    {
25
        return [
26 1
            Client::class,
27
            ClientInterface::class,
28
            HandlerStack::class,
29
        ];
30
    }
31
32
    /**
33
     * Register method.
34
     */
35
    public function register()
36
    {
37
        // Configuring all guzzle clients.
38
        $this->app->bind(ClientInterface::class, function () {
39
            // Guzzle client
40
            return new Client(['handler' => $this->app->make(HandlerStack::class)]);
41
        });
42
43
        $this->app->alias(ClientInterface::class, Client::class);
44
45
        // Bind if needed.
46
        $this->app->bindIf(HandlerStack::class, function () {
47
            return HandlerStack::create();
48
        });
49
50
        // If resolved, by this SP or another, add some layers.
51
        $this->app->resolving(HandlerStack::class, function (HandlerStack $stack) {
52
            /** @var \Clockwork\Clockwork $clockwork */
53
            $clockwork = $this->app->make('clockwork');
54
55
            $stack->push(new Middleware(new Profiler($clockwork->getTimeline())));
56
57
            /** @var \GuzzleHttp\MessageFormatter $formatter */
58
            $formatter = $this->app->make(MessageFormatter::class);
59
            $stack->unshift(GuzzleMiddleware::log($clockwork->getLog(), $formatter));
60
61
            // Also log to the default PSR logger.
62
            if ($this->app->bound(LoggerInterface::class)) {
63
                $logger = $this->app->make(LoggerInterface::class);
64
65
                // Don't log to the same logger twice.
66
                if ($logger === $clockwork->getLog()) {
67
                    return;
68
                }
69
70
                // Push the middleware on the stack.
71
                $stack->unshift(GuzzleMiddleware::log($logger, $formatter));
72
            }
73
        });
74
    }
75
}
76