Completed
Push — develop ( fc74bf...ff2b27 )
by Risan Bagja
01:47
created

BaseStringBuilder::parseToPsrUri()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Risan\OAuth1\Signature;
4
5
use Risan\OAuth1\Request\UriParserInterface;
6
7
class BaseStringBuilder implements BaseStringBuilderInterface
8
{
9
    /**
10
     * The UriParserInterface instance.
11
     *
12
     * @var \Risan\OAuth1\Request\UriParserInterface
13
     */
14 4
    protected $uriParser;
15
16 4
    public function __construct(UriParserInterface $uriParser)
17
    {
18 4
        $this->uriParser = $uriParser;
19
    }
20 4
21
     /**
22 4
     * {@inheritDoc}
23
     */
24 4
    public function getUriParser()
25
    {
26 4
        return $this->uriParser;
27
    }
28 4
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function build($httpMethod, $uri, array $parameters = [])
33
    {
34 5
        $uri = $this->uriParser->toPsrUri($uri);
35
36 5
        $components = [];
37
38
        $components[] = rawurlencode($this->buildMethodComponent($httpMethod));
39
40
        $components[] = rawurlencode($this->buildUriComponent($uri));
41
42 7
        parse_str($uri->getQuery(), $queryParameters);
43
44 7
        $components[] = rawurlencode($this->buildParametersComponent(array_merge($queryParameters, $parameters)));
45
46 7
        return implode('&', $components);
47 7
    }
48 7
49 7
    /**
50 7
     * {@inheritDoc}
51
     */
52
    public function buildMethodComponent($httpMethod)
53
    {
54
        return strtoupper($httpMethod);
55
    }
56
57 4
    /**
58
     * {@inheritDoc}
59 4
     */
60
    public function buildUriComponent($uri)
61 4
    {
62
        $uri = $this->uriParser->toPsrUri($uri);
63
64
        return $this->uriParser->buildFromParts([
65
            'scheme' => $uri->getScheme(),
66
            'host' => $uri->getHost(),
67
            'port' => $uri->getPort(),
68
            'path' => $uri->getPath(),
69
        ]);
70 6
    }
71
72 6
    /**
73
     * {@inheritDoc}
74
     */
75
    public function buildParametersComponent(array $parameters)
76 6
    {
77 6
        $parameters = $this->normalizeParameters($parameters);
78
79 6
        return $this->buildQueryString($parameters);
80 1
    }
81
82 6
    /**
83
     * Normalize the given request parameters.
84
     *
85
     * @param  array  $parameters
86
     * @return array
87 6
     */
88
    public function normalizeParameters(array $parameters)
89 6
    {
90
        $normalized = [];
91
92
        // [1] Encode both the keys and values.
93
        // Decode it frist, in case the given data is already encoded.
94
        foreach ($parameters as $key => $value) {
95
            $key = rawurlencode(rawurldecode($key));
96
97
            if (is_array($value)) {
98
                $normalized[$key] = $this->normalizeParameters($value);
99
            } else {
100 6
                $normalized[$key] = rawurlencode(rawurldecode($value));
101
            }
102 6
        }
103
104 6
        // [2] Sort by the encoded key.
105 6
        ksort($normalized);
106 1
107
        return $normalized;
108
    }
109 6
110 1
    /**
111
     * Build query string from the given parameters.
112 6
     *
113
     * @param  array  $parameters
114
     * @param  array  $initialQueryParameters
115
     * @param  string $previousKey
116 6
     * @return string
117
     */
118
    public function buildQueryString(array $parameters, array $initialQueryParameters = [], $previousKey = null)
119
    {
120
        $queryParameters = $initialQueryParameters;
121
122
        foreach ($parameters as $key => $value) {
123
            if ($previousKey !== null) {
124
                $key = "{$previousKey}[{$key}]";
125
            }
126 8
127
            if (is_array($value)) {
128 8
                $queryParameters = $this->buildQueryString($value, $queryParameters, $key);
129 6
            } else {
130 7
                $queryParameters[] = "{$key}={$value}";
131 7
            }
132
        }
133
134 1
        return $previousKey !== null ? $queryParameters : implode('&', $queryParameters);
135
    }
136
}
137