Completed
Push — develop ( 02a307...97cdae )
by Risan Bagja
01:44
created

BaseStringBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 130
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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