Completed
Push — master ( 728a12...724e10 )
by Hannes
05:15 queued 03:54
created

ServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 8.69%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 0
loc 69
ccs 2
cts 23
cp 0.0869
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 8 1
A register() 0 46 4
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
            // We cannot log with debugbar from the CLI
54
            if ($this->app->runningInConsole()) {
55
                return;
56
            }
57
            
58
            /** @var \DebugBar\DebugBar $debugBar */
59
            $debugBar = $this->app->make('debugbar');
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