ServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
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 65
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 47 4
1
<?php
2
3
namespace GuzzleHttp\Profiling\Debugbar\Support\Laravel;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\MessageFormatter;
9
use GuzzleHttp\Middleware as GuzzleMiddleware;
10
use GuzzleHttp\Profiling\Debugbar\ExceptionMiddleware;
11
use GuzzleHttp\Profiling\Debugbar\Profiler;
12
use GuzzleHttp\Profiling\Middleware;
13
use Illuminate\Contracts\Support\DeferrableProvider;
14
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
15
use Psr\Http\Client\ClientInterface as PsrClientInterface;
16
17
class ServiceProvider extends BaseServiceProvider implements DeferrableProvider
18
{
19
    /**
20
     * @return array
21
     */
22 1
    public function provides(): array
23
    {
24
        return [
25 1
            Client::class,
26
            ClientInterface::class,
27
            HandlerStack::class,
28
        ];
29
    }
30
31
    /**
32
     * Register method.
33
     */
34
    public function register(): void
35
    {
36
        // Configuring all guzzle clients.
37
        $this->app->bind(ClientInterface::class, function(): PsrClientInterface {
38
            // Guzzle client
39
            return new Client(['handler' => $this->app->make(HandlerStack::class)]);
40
        });
41
42
        $this->app->alias(ClientInterface::class, Client::class);
43
        $this->app->alias(PsrClientInterface::class, ClientInterface::class);
44
45
        // Bind if needed.
46
        $this->app->bind(HandlerStack::class, function(): HandlerStack {
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): void {
52
            // We cannot log with debugbar from the CLI
53
            if ($this->app->runningInConsole()) {
54
                return;
55
            }
56
57
            /** @var \DebugBar\DebugBar $debugBar */
58
            $debugBar = $this->app->make('debugbar');
59
60
            $stack->push(new Middleware(new Profiler($timeline = $debugBar->getCollector('time'))));
61
            $stack->unshift(new ExceptionMiddleware($debugBar->getCollector('exceptions')));
62
63
            /** @var \GuzzleHttp\MessageFormatter $formatter */
64
            $formatter = $this->app->make(MessageFormatter::class);
65
            $stack->unshift(GuzzleMiddleware::log($debugBar->getCollector('messages'), $formatter));
66
67
            // Also log to the default PSR logger.
68
            if ($this->app->bound(LoggerInterface::class)) {
69
                $logger = $this->app->make(LoggerInterface::class);
70
71
                // Don't log to the same logger twice.
72
                if ($logger === $debugBar->getCollector('messages')) {
73
                    return;
74
                }
75
76
                // Push the middleware on the stack.
77
                $stack->unshift(GuzzleMiddleware::log($logger, $formatter));
78
            }
79
        });
80
    }
81
}
82