Completed
Push — master ( 8e987d...ff1c4b )
by Harry
04:10
created

HttpsAuthMiddleware::getSecret()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 GuzzleHttp\Psr7\Uri;
18
use Psr\Http\Message\RequestInterface;
19
20
class HttpsAuthMiddleware
21
{
22
    const AUTH_NAME = 'gigya';
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 View Code Duplication
    public function __construct(callable $nextHandler, $apiKey, $secret, $userKey = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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 the https auth into the query string
59
     *
60
     * @param RequestInterface $request
61
     * @param array            $options
62
     *
63
     * @return Closure
64
     */
65 11
    public function __invoke(RequestInterface $request, array $options)
66
    {
67 11
        if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) {
68 9
            $uri = Uri::withQueryValue($request->getUri(), 'apiKey', $this->apiKey);
69 9
            $uri = Uri::withQueryValue($uri, 'secret', $this->secret);
70 9
            if ((bool) $this->userKey) {
71 2
                $uri = Uri::withQueryValue($uri, 'userKey', $this->userKey);
72 2
            }
73 9
            $request = $request->withUri($uri);
74 9
        }
75 11
        $fn = $this->nextHandler;
76 11
        return $fn($request, $options);
77
    }
78
79
    /**
80
     * @return string
81
     */
82 1
    public function getApiKey()
83
    {
84 1
        return $this->apiKey;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 1
    public function getSecret()
91
    {
92 1
        return $this->secret;
93
    }
94
95
    /**
96
     * @return string|null
97
     */
98 1
    public function getUserKey()
99
    {
100 1
        return $this->userKey;
101
    }
102
103
    /**
104
     * Return a middleware handler function for https Authentication
105
     *
106
     * @param string      $apiKey
107
     * @param string      $secret
108
     * @param string|null $userKey
109
     *
110
     * @return Closure
111
     */
112
    public static function middleware($apiKey, $secret, $userKey = null)
113
    {
114 53
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
115 11
            return new static($handler, $apiKey, $secret, $userKey);
116 53
        };
117
    }
118
}
119