Passed
Branch feature/second-release (e9200f)
by Andrea Marco
13:37
created

LazyJsonPagesServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 34
ccs 9
cts 9
cp 1
rs 10
c 2
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
A onError() 0 3 1
A onRequest() 0 3 1
A onResponse() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LazyJsonPages\Providers;
6
7
use Cerbero\LazyJsonPages\LazyJsonPages;
8
use Cerbero\LazyJsonPages\Middleware\Tap;
9
use Illuminate\Http\Client\Events\ConnectionFailed;
10
use Illuminate\Http\Client\Events\RequestSending;
11
use Illuminate\Http\Client\Events\ResponseReceived;
12
use Illuminate\Http\Client\Request;
13
use Illuminate\Http\Client\Response;
14
use Illuminate\Support\ServiceProvider;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Throwable;
18
19
/**
20
 * The service provider to integrate with Laravel.
21
 *
22
 * @property-read array{events: \Illuminate\Contracts\Events\Dispatcher} $app
23
 */
24
final class LazyJsonPagesServiceProvider extends ServiceProvider
25
{
26
    /**
27
     * Bootstrap the services.
28
     */
29 2
    public function boot(): void
30
    {
31 2
        $fireEvents = Tap::once($this->onRequest(...), $this->onResponse(...), $this->onError(...));
32
33 2
        LazyJsonPages::globalMiddleware('laravel_events', $fireEvents);
34
    }
35
36
    /**
37
     * Handle HTTP requests before they are sent.
38
     */
39 2
    private function onRequest(RequestInterface $request): void
0 ignored issues
show
Unused Code introduced by
The method onRequest() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
40
    {
41 2
        $this->app['events']->dispatch(new RequestSending(new Request($request)));
42
    }
43
44
    /**
45
     * Handle HTTP responses after they are received.
46
     */
47 1
    private function onResponse(ResponseInterface $response, RequestInterface $request): void
0 ignored issues
show
Unused Code introduced by
The method onResponse() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
48
    {
49 1
        $this->app['events']->dispatch(new ResponseReceived(new Request($request), new Response($response)));
50
    }
51
52
    /**
53
     * Handle a transaction error.
54
     */
55 1
    private function onError(Throwable $e, RequestInterface $request): void
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    private function onError(/** @scrutinizer ignore-unused */ Throwable $e, RequestInterface $request): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method onError() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
56
    {
57 1
        $this->app['events']->dispatch(new ConnectionFailed(new Request($request)));
58
    }
59
}
60