Authentication::__invoke()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 1
nop 1
crap 4
1
<?php
2
/**
3
 * Akamai {OPEN} EdgeGrid Auth Client
4
 *
5
 * @author Davey Shafik <[email protected]>
6
 * @copyright Copyright 2016 Akamai Technologies, Inc. All rights reserved.
7
 * @license Apache 2.0
8
 * @link https://github.com/akamai-open/AkamaiOPEN-edgegrid-php-client
9
 * @link https://developer.akamai.com
10
 * @link https://developer.akamai.com/introduction/Client_Auth.html
11
 */
12
namespace Akamai\Open\EdgeGrid\Handler;
13
14
use Akamai\Open\EdgeGrid\Authentication as Signer;
15
use Akamai\Open\EdgeGrid\Exception\HandlerException;
16
use Psr\Http\Message\RequestInterface;
17
18
/**
19
 * Akamai {OPEN} EdgeGrid Auth Guzzle Middleware Handler
20
 *
21
 * @package Akamai\Open\EdgeGrid\Client
22
 *
23
 * @method $this createAuthHeader()
24
 * @method $this setHttpMethod($method)
25
 * @method $this getHost()
26
 * @method $this setHost($host)
27
 * @method $this setConfig(array $config)
28
 * @method $this setQuery($query, $ensure_encoding = true)
29
 * @method $this setBody($body)
30
 * @method $this setHeaders(array $headers) *
31
 * @method $this setPath($path)
32
 * @method $this setTimestamp($timestamp = null)
33
 * @method $this setNonce($nonce = null)
34
 * @method $this setHeadersToSign($headers_to_sign)
35
 * @method $this setMaxBodySize($max_body_size)
36
 * @method $this setAuth($client_token, $client_secret, $access_token)
37
 */
38
class Authentication
39
{
40
    /**
41
     * @var \Akamai\Open\EdgeGrid\Authentication
42
     */
43
    protected $signer;
44
45
    /**
46
     * Inject signer object
47
     *
48
     * @param Signer|null $auth
49
     */
50 150
    public function setSigner(\Akamai\Open\EdgeGrid\Authentication $auth = null)
51
    {
52 150
        $this->signer = $auth;
53 150
        if ($this->signer === null) {
54 14
            $this->signer = new Signer();
55
        }
56 150
    }
57
58
    /**
59
     * Handler magic invoker
60
     *
61
     * @param callable $handler The next handler in the stack
62
     * @return \Closure
63
     * @throws \Akamai\Open\EdgeGrid\Exception\HandlerException
64
     */
65
    public function __invoke(callable $handler)
66
    {
67 121
        return function (
68
            RequestInterface $request,
69
            array $config
70
        ) use ($handler) {
71 121
            if ($request->getUri()->getScheme() !== 'https' ||
72 121
                strpos($request->getUri()->getHost(), 'akamaiapis.net') === false
73
            ) {
74 64
                return $handler($request, $config);
75
            }
76
77 57
            if (!$this->signer) {
78 1
                throw new HandlerException('Signer not set, make sure to call setSigner first');
79
            }
80
81 56
            $request->getBody()->rewind();
82
83 56
            $this->signer->setHttpMethod($request->getMethod())
84 56
                ->setHost($request->getUri()->getHost())
85 56
                ->setPath($request->getUri()->getPath())
86 56
                ->setQuery($request->getUri()->getQuery())
87 56
                ->setHeaders($request->getHeaders())
88 56
                ->setBody($request->getBody()->getContents());
89
90 56
            $request = $request->withHeader('Authorization', $this->signer->createAuthHeader());
91
92 56
            return $handler($request, $config);
93 121
        };
94
    }
95
96
    /**
97
     * Proxy to the underlying signer object
98
     *
99
     * @param $method
100
     * @param $args
101
     * @return mixed
102
     * @throws \Akamai\Open\EdgeGrid\Exception\HandlerException
103
     */
104 16
    public function __call($method, $args)
105
    {
106 16
        if ($this->signer === null) {
107 1
            throw new HandlerException('Signer not set, make sure to call setSigner first');
108
        }
109
110 15
        $return = call_user_func_array([$this->signer, $method], $args);
111 15
        if ($return == $this->signer) {
112 15
            return $this;
113
        }
114
115 1
        return $return;
116
    }
117
118
    /**
119
     * Create Handler using an .edgerc file
120
     *
121
     * Automatically create a valid authentication handler using
122
     * an .edgerc file
123
     *
124
     * @param string $section
125
     * @param null $file
126
     *
127
     * @return static
128
     */
129 4
    public static function createFromEdgeRcFile($section = 'default', $file = null)
130
    {
131 4
        $signer = Signer::createFromEdgeRcFile($section, $file);
132 4
        $auth = new static();
133 4
        $auth->setSigner($signer);
134
135 4
        return $auth;
136
    }
137
}
138