Authorization   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 47
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3
ccs 0
cts 26
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 2
A __invoke() 0 18 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/relay-hubspot/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/relay-hubspot
7
 */
8
9
namespace Flipbox\Relay\HubSpot\Middleware;
10
11
use Flipbox\Relay\HubSpot\AuthorizationInterface;
12
use Flipbox\Relay\Middleware\AbstractMiddleware;
13
use Flipbox\Skeleton\Exceptions\InvalidConfigurationException;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class Authorization extends AbstractMiddleware
22
{
23
    /**
24
     * @var AuthorizationInterface
25
     */
26
    public $authorization;
27
28
    /**
29
     * @inheritdoc
30
     * @throws InvalidConfigurationException
31
     */
32
    public function init()
33
    {
34
        parent::init();
35
36
        if (!$this->authorization instanceof AuthorizationInterface) {
37
            throw new InvalidConfigurationException(sprintf(
38
                "The class '%s' requires an authorization class that is an instance of '%s', '%s' given.",
39
                get_class($this),
40
                AuthorizationInterface::class,
41
                get_class($this->authorization)
42
            ));
43
        }
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function __invoke(
50
        RequestInterface $request,
51
        ResponseInterface $response,
52
        callable $next = null
53
    ) {
54
        parent::__invoke($request, $response, $next);
55
56
        $request = $this->authorization->prepareAuthorizationRequest($request);
57
58
        // Clone in case we need to refresh and resend
59
        $runner = clone $next;
60
61
        return $this->authorization->handleAuthorizationResponse(
62
            $next($request, $response),
63
            $request,
64
            $runner
65
        );
66
    }
67
}
68