Completed
Pull Request — master (#10)
by
unknown
05:47
created

ServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 8.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 70
ccs 2
cts 24
cp 0.0833
rs 10
c 0
b 0
f 0

2 Methods

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