ServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
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 59
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 41 3
1
<?php
2
3
namespace GuzzleHttp\Profiling\Clockwork\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\Clockwork\Profiler;
11
use GuzzleHttp\Profiling\Middleware;
12
use Illuminate\Contracts\Support\DeferrableProvider;
13
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
14
use Psr\Http\Client\ClientInterface as PsrClientInterface;
15
use Psr\Log\LoggerInterface;
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(ClientInterface::class, PsrClientInterface::class);
44
45
        // Bind if needed.
46
        $this->app->bindIf(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
            /** @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