CredentialsAuthMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Auth;
15
16
use Closure;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface;
19
20
class CredentialsAuthMiddleware
21
{
22
    const AUTH_NAME = 'credentials';
23
24
    /**
25
     * @var string
26
     */
27
    private $apiKey;
28
29
    /**
30
     * @var string
31
     */
32
    private $secret;
33
34
    /**
35
     * @var null|string
36
     */
37
    private $userKey;
38
    /**
39
     * @var callable
40
     */
41
    private $nextHandler;
42
43
    /**
44
     * @param callable    $nextHandler
45
     * @param string      $apiKey
46
     * @param string      $secret
47
     * @param string|null $userKey
48
     */
49 13
    public function __construct(callable $nextHandler, $apiKey, $secret, $userKey = null)
50
    {
51 13
        $this->nextHandler = $nextHandler;
52 13
        $this->apiKey = $apiKey;
53 13
        $this->secret = $secret;
54 13
        $this->userKey = $userKey;
55 13
    }
56
57
    /**
58
     * Inject credentials information into the query parameters
59
     *
60
     * @param RequestInterface $request
61
     * @param array            $options
62
     *
63
     * @return GuzzleResponseInterface
64
     */
65 12
    public function __invoke(RequestInterface $request, array $options)
66
    {
67 12
        if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) {
68 2
            $params = array_merge(
69 2
                \GuzzleHttp\Psr7\parse_query($request->getBody()),
70
                [
71 2
                    'client_id'     => $this->userKey ?: $this->apiKey,
72 2
                    'client_secret' => $this->secret,
73
                ]
74
            );
75 2
            $request = $request->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($params)));
76
        }
77 12
        $fn = $this->nextHandler;
78 12
        return $fn($request, $options);
79
    }
80
81
    /**
82
     * @return string
83
     */
84 1
    public function getApiKey()
85
    {
86 1
        return $this->apiKey;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 1
    public function getSecret()
93
    {
94 1
        return $this->secret;
95
    }
96
97
    /**
98
     * @return string|null
99
     */
100 1
    public function getUserKey()
101
    {
102 1
        return $this->userKey;
103
    }
104
105
    /**
106
     * Return a middleware handler function for this Authentication
107
     *
108
     * @param string      $apiKey
109
     * @param string      $secret
110
     * @param string|null $userKey
111
     *
112
     * @return Closure
113
     */
114
    public static function middleware($apiKey, $secret, $userKey = null)
115
    {
116 54
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
117 12
            return new static($handler, $apiKey, $secret, $userKey);
118 54
        };
119
    }
120
}
121