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

HttpsAuthMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 100
loc 100
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A getApiKey() 4 4 1
A getSecret() 4 4 1
A getUserKey() 4 4 1
A middleware() 6 6 1
B __invoke() 14 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
19 View Code Duplication
class HttpsAuthMiddleware
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...
20
{
21
    const AUTH_NAME = 'gigya';
22
23
    /**
24
     * @var string
25
     */
26
    private $apiKey;
27
28
    /**
29
     * @var string
30
     */
31
    private $secret;
32
33
    /**
34
     * @var null|string
35
     */
36
    private $userKey;
37
    /**
38
     * @var callable
39
     */
40
    private $nextHandler;
41
42
    /**
43
     * @param callable    $nextHandler
44
     * @param string      $apiKey
45
     * @param string      $secret
46
     * @param string|null $userKey
47
     */
48 12
    public function __construct(callable $nextHandler, $apiKey, $secret, $userKey = null)
49
    {
50 12
        $this->nextHandler = $nextHandler;
51 12
        $this->apiKey = $apiKey;
52 12
        $this->secret = $secret;
53 12
        $this->userKey = $userKey;
54 12
    }
55
56
    /**
57
     * Inject the https auth into the query string
58
     *
59
     * @param RequestInterface $request
60
     * @param array            $options
61
     *
62
     * @return Closure
63
     */
64 11
    public function __invoke(RequestInterface $request, array $options)
65
    {
66 11
        if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) {
67 9
            $query = isset($options['query']) ? $options['query'] : [];
68 9
            $query['apiKey'] = $this->apiKey;
69 9
            $query['secret'] = $this->secret;
70 9
            if ((bool) $this->userKey) {
71 2
                $query['userKey'] = $this->userKey;
72 2
            }
73 9
            $options['query'] = $query;
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 52
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
115 11
            return new static($handler, $apiKey, $secret, $userKey);
116 52
        };
117
    }
118
}
119