Completed
Pull Request — master (#4)
by Risan Bagja
02:18 queued 51s
created

BaseStringBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUriParser() 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
    public function __construct(UriParserInterface $uriParser)
17
    {
18
        $this->uriParser = $uriParser;
19
    }
20
21
     /**
22
     * {@inheritDoc}
23
     */
24
    public function getUriParser()
25
    {
26
        return $this->uriParser;
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function build($httpMethod, $uri, array $parameters = [])
33
    {
34
        $uri = $this->uriParser->toPsrUri($uri);
35
36
        $components = [];
37
38
        $components[] = rawurlencode($this->buildMethodComponent($httpMethod));
39
40
        $components[] = rawurlencode($this->buildUriComponent($uri));
41
42
        parse_str($uri->getQuery(), $queryParameters);
43
44
        $components[] = rawurlencode($this->buildParametersComponent(array_merge($queryParameters, $parameters)));
45
46
        return implode('&', $components);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function buildMethodComponent($httpMethod)
53
    {
54
        return strtoupper($httpMethod);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function buildUriComponent($uri)
61
    {
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
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function buildParametersComponent(array $parameters)
76
    {
77
        $parameters = $this->normalizeParameters($parameters);
78
79
        return $this->buildQueryString($parameters);
80
    }
81
82
    /**
83
     * Normalize the given request parameters.
84
     *
85
     * @param  array  $parameters
86
     * @return array
87
     */
88
    public function normalizeParameters(array $parameters)
89
    {
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
                $normalized[$key] = rawurlencode(rawurldecode($value));
101
            }
102
        }
103
104
        // [2] Sort by the encoded key.
105
        ksort($normalized);
106
107
        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
    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
127
            if (is_array($value)) {
128
                $queryParameters = $this->buildQueryString($value, $queryParameters, $key);
129
            } else {
130
                $queryParameters[] = "{$key}={$value}";
131
            }
132
        }
133
134
        return $previousKey !== null ? $queryParameters : implode('&', $queryParameters);
135
    }
136
}
137