Completed
Pull Request — master (#21)
by Harry
02:25
created

CredentialsAuthMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 1
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 View Code Duplication
class CredentialsAuthMiddleware
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 12
    public function __construct(callable $nextHandler, $apiKey, $secret, $userKey = null)
50
    {
51 12
        $this->nextHandler = $nextHandler;
52 12
        $this->apiKey = $apiKey;
53 12
        $this->secret = $secret;
54 12
        $this->userKey = $userKey;
55 12
    }
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 11
    public function __invoke(RequestInterface $request, array $options)
66
    {
67 11
        if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) {
68 2
            $query = isset($options['query']) ? $options['query'] : [];
69 2
            $query['client_id'] = $this->userKey ?: $this->apiKey;
70 2
            $query['client_secret'] = $this->secret;
71 2
            $options['query'] = $query;
72 3
        }
73 11
        $fn = $this->nextHandler;
74 11
        return $fn($request, $options);
75
    }
76
77
    /**
78
     * @return string
79
     */
80 1
    public function getApiKey()
81
    {
82 1
        return $this->apiKey;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function getSecret()
89
    {
90 1
        return $this->secret;
91
    }
92
93
    /**
94
     * @return string|null
95
     */
96 1
    public function getUserKey()
97
    {
98 1
        return $this->userKey;
99
    }
100
101
    /**
102
     * Return a middleware handler function for this Authentication
103
     *
104
     * @param string      $apiKey
105
     * @param string      $secret
106
     * @param string|null $userKey
107
     *
108
     * @return Closure
109
     */
110
    public static function middleware($apiKey, $secret, $userKey = null)
111
    {
112 52
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
113 11
            return new static($handler, $apiKey, $secret, $userKey);
114 52
        };
115
    }
116
}
117