Completed
Pull Request — master (#27)
by Harry
06:09
created

src/Auth/HttpsAuthMiddleware.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
            $params = array_merge(
68 9
                \GuzzleHttp\Psr7\parse_query($request->getBody()),
69
                [
70 9
                    'apiKey' => $this->apiKey,
71 9
                    'secret' => $this->secret,
72
                ]
73 9
            );
74
75 9
            if ((bool)$this->userKey) {
76 2
                $params['userKey'] = $this->userKey;
77 2
            }
78
            $request = $request
79 9
                ->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($params)));
80 9
        }
81 11
        $fn = $this->nextHandler;
82 11
        return $fn($request, $options);
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    public function getApiKey()
89
    {
90 1
        return $this->apiKey;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 1
    public function getSecret()
97
    {
98 1
        return $this->secret;
99
    }
100
101
    /**
102
     * @return string|null
103
     */
104 1
    public function getUserKey()
105
    {
106 1
        return $this->userKey;
107
    }
108
109
    /**
110
     * Return a middleware handler function for https Authentication
111
     *
112
     * @param string      $apiKey
113
     * @param string      $secret
114
     * @param string|null $userKey
115
     *
116
     * @return Closure
117
     */
118
    public static function middleware($apiKey, $secret, $userKey = null)
119
    {
120 53
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
121 11
            return new static($handler, $apiKey, $secret, $userKey);
122 53
        };
123
    }
124
}
125