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 (#110)
by Jan
07:49
created

Request::perform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 2 Features 1
Metric Value
c 9
b 2
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
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\Rest;
19
20
use ApaiIO\ApaiIO;
21
use ApaiIO\Configuration\ConfigurationInterface;
22
use ApaiIO\Operations\OperationInterface;
23
use ApaiIO\Request\RequestInterface;
24
use ApaiIO\Request\Util;
25
use GuzzleHttp\ClientInterface;
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 Request implements RequestInterface
34
{
35
    /**
36
     * The requestscheme
37
     *
38
     * @var string
39
     */
40
    private $requestScheme = "http://webservices.amazon.%s/onca/xml?%s";
41
42
    /**
43
     * @var ClientInterface
44
     */
45
    private $client;
46
47
    /**
48
     * Initialize instance
49
     *
50
     * @param ClientInterface $client
51
     */
52
    public function __construct(ClientInterface $client)
53
    {
54
        $this->client = $client;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function perform(OperationInterface $operation, ConfigurationInterface $configuration)
61
    {
62
        $preparedRequestParams = $this->prepareRequestParams($operation, $configuration);
63
        $queryString = $this->buildQueryString($preparedRequestParams, $configuration);
64
65
        $uri = sprintf($this->requestScheme, $configuration->getCountry(), $queryString);
66
        $request = new \GuzzleHttp\Psr7\Request('GET', $uri, [
67
            'User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']'
68
        ]);
69
70
        $result = $this->client->send($request);
71
72
        return $result->getBody()->getContents();
73
    }
74
75
    /**
76
     * Prepares the parameters for the request
77
     *
78
     * @param OperationInterface     $operation
79
     * @param ConfigurationInterface $configuration
80
     *
81
     * @return array
82
     */
83
    protected function prepareRequestParams(OperationInterface $operation, ConfigurationInterface $configuration)
84
    {
85
        $baseRequestParams = [
86
            'Service'        => 'AWSECommerceService',
87
            'AWSAccessKeyId' => $configuration->getAccessKey(),
88
            'AssociateTag'   => $configuration->getAssociateTag(),
89
            'Operation'      => $operation->getName(),
90
            'Version'        => '2011-08-01',
91
            'Timestamp'      => Util::getTimeStamp()
92
        ];
93
94
        $operationParams = $operation->getOperationParameter();
95
96
        foreach ($operationParams as $key => $value) {
97
            if (true === is_array($value)) {
98
                $operationParams[$key] = implode(',', $value);
99
            }
100
        }
101
102
        $fullParameterList = array_merge($baseRequestParams, $operationParams);
103
        ksort($fullParameterList);
104
105
        return $fullParameterList;
106
    }
107
108
    /**
109
     * Builds the final querystring including the signature
110
     *
111
     * @param array                  $params
112
     * @param ConfigurationInterface $configuration
113
     *
114
     * @return string
115
     */
116
    protected function buildQueryString(array $params, ConfigurationInterface $configuration)
117
    {
118
        $parameterList = [];
119
        foreach ($params as $key => $value) {
120
            $parameterList[] = sprintf('%s=%s', $key, rawurlencode($value));
121
        }
122
123
        $parameterList[] = 'Signature=' . rawurlencode(
124
            $this->buildSignature($parameterList, $configuration->getCountry(), $configuration->getSecretKey())
125
        );
126
127
        return implode("&", $parameterList);
128
    }
129
130
    /**
131
     * Calculates the signature for the request
132
     *
133
     * @param array  $params
134
     * @param string $country
135
     * @param string $secret
136
     *
137
     * @return string
138
     */
139
    protected function buildSignature(array $params, $country, $secret)
140
    {
141
        return Util::buildSignature(
142
            sprintf(
143
                "GET\nwebservices.amazon.%s\n/onca/xml\n%s",
144
                $country,
145
                implode('&', $params)
146
            ),
147
            $secret
148
        );
149
    }
150
}
151