GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#178)
by
unknown
03:42
created

GuzzleRequest::buildSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
/*
3
 * Copyright 2016 Jan Eichhorn <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ApaiIO\Request;
19
20
use ApaiIO\ApaiIO;
21
use ApaiIO\Configuration\ConfigurationInterface;
22
use ApaiIO\Operations\OperationInterface;
23
use GuzzleHttp\ClientInterface;
24
use GuzzleHttp\Exception\RequestException;
25
use GuzzleHttp\Psr7\Uri;
26
27
/**
28
 * Basic implementation of the rest request
29
 *
30
 * @see    http://docs.aws.amazon.com/AWSECommerceService/2011-08-01/DG/AnatomyOfaRESTRequest.html
31
 * @author Jan Eichhorn <[email protected]>
32
 */
33
class GuzzleRequest implements RequestInterface
34
{
35
    /**
36
     * The requestscheme
37
     *
38
     * @var string
39
     */
40
    private $requestTemplate = "//webservices.amazon.%s/onca/xml?%s";
41
42
    /**
43
     * The scheme for the uri. E.g. http or https.
44
     *
45
     * @var string
46
     */
47
    private $scheme = 'http';
48
49
    /**
50
     * @var ClientInterface
51
     */
52
    private $client;
53
54
    /**
55
     * Initialize instance
56
     *
57
     * @param ClientInterface $client
58
     */
59 4
    public function __construct(ClientInterface $client)
60
    {
61 4
        $this->client = $client;
62 4
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function perform(OperationInterface $operation, ConfigurationInterface $configuration)
68
    {
69 2
        $preparedRequestParams = $this->prepareRequestParams($operation, $configuration);
70 2
        $queryString = $this->buildQueryString($preparedRequestParams, $configuration);
71
72 2
        $uri = new Uri(sprintf($this->requestTemplate, $configuration->getCountry(), $queryString));
73 2
        $request = new \GuzzleHttp\Psr7\Request('GET', $uri->withScheme($this->scheme), [
74 2
            'User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']'
75
        ]);
76
        try {
77 2
            $result = $this->client->send($request);
78
        }
79
        catch (RequestException $e) {
80
            return NULL;
81
        }
82
83 2
        return $result->getBody()->getContents();
84
    }
85
86
    /**
87
     * Sets the scheme.
88
     *
89
     * @param string $scheme
90
     */
91 2
    public function setScheme($scheme)
92
    {
93 2
        if (!in_array($scheme = strtolower($scheme), ['http', 'https'])) {
94 1
            throw new \InvalidArgumentException('The scheme can only be http or https.');
95
        }
96
97 1
        $this->scheme = $scheme;
98 1
    }
99
100
    /**
101
     * Prepares the parameters for the request
102
     *
103
     * @param OperationInterface     $operation
104
     * @param ConfigurationInterface $configuration
105
     *
106
     * @return array
107
     */
108 2
    protected function prepareRequestParams(OperationInterface $operation, ConfigurationInterface $configuration)
109
    {
110
        $baseRequestParams = [
111 2
            'Service'        => 'AWSECommerceService',
112 2
            'AWSAccessKeyId' => $configuration->getAccessKey(),
113 2
            'AssociateTag'   => $configuration->getAssociateTag(),
114 2
            'Operation'      => $operation->getName(),
115 2
            'Version'        => '2013-08-01',
116 2
            'Timestamp'      => Util::getTimeStamp()
117
        ];
118
119 2
        $operationParams = $operation->getOperationParameter();
120
121 2
        foreach ($operationParams as $key => $value) {
122 2
            if (true === is_array($value)) {
123 2
                $operationParams[$key] = implode(',', $value);
124
            }
125
        }
126
127 2
        $fullParameterList = array_merge($baseRequestParams, $operationParams);
128 2
        ksort($fullParameterList);
129
130 2
        return $fullParameterList;
131
    }
132
133
    /**
134
     * Builds the final querystring including the signature
135
     *
136
     * @param array                  $params
137
     * @param ConfigurationInterface $configuration
138
     *
139
     * @return string
140
     */
141 2
    protected function buildQueryString(array $params, ConfigurationInterface $configuration)
142
    {
143 2
        $parameterList = [];
144 2
        foreach ($params as $key => $value) {
145 2
            $parameterList[] = sprintf('%s=%s', $key, rawurlencode($value));
146
        }
147
148 2
        $parameterList[] = 'Signature=' . rawurlencode(
149 2
            $this->buildSignature($parameterList, $configuration->getCountry(), $configuration->getSecretKey())
150
        );
151
152 2
        return implode("&", $parameterList);
153
    }
154
155
    /**
156
     * Calculates the signature for the request
157
     *
158
     * @param array  $params
159
     * @param string $country
160
     * @param string $secret
161
     *
162
     * @return string
163
     */
164 2
    protected function buildSignature(array $params, $country, $secret)
165
    {
166 2
        return Util::buildSignature(
167 2
            sprintf(
168 2
                "GET\nwebservices.amazon.%s\n/onca/xml\n%s",
169 2
                $country,
170 2
                implode('&', $params)
171
            ),
172 2
            $secret
173
        );
174
    }
175
}
176