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

src/Auth/CredentialsAuthMiddleware.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
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface;
19
20 View Code Duplication
class CredentialsAuthMiddleware
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...
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
            $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 1
                ]
74 2
            );
75 2
            $request = $request->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($params)));
76 2
        }
77 11
        $fn = $this->nextHandler;
78 11
        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 53
        return function (callable $handler) use ($apiKey, $secret, $userKey) {
117 11
            return new static($handler, $apiKey, $secret, $userKey);
118 53
        };
119
    }
120
}
121