Passed
Push — develop ( 36c731...cb55ce )
by Andrew
02:56
created

UrlBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
dl 0
loc 53
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A makeUserHost() 0 3 1
A __construct() 0 4 1
B buildMethodUrl() 0 19 5
1
<?php
2
3
4
namespace ddlzz\AmoAPI\Request;
5
6
use ddlzz\AmoAPI\Exceptions\InvalidArgumentException;
7
use ddlzz\AmoAPI\SettingsStorage;
8
9
10
/**
11
 * Class UrlBuilder
12
 * @package ddlzz\AmoAPI\Request
13
 * @author ddlzz
14
 */
15
class UrlBuilder
16
{
17
    /** @var SettingsStorage */
18
    private $settings;
19
20
    /** @var string */
21
    private $subdomain;
22
23
    /**
24
     * UrlBuilder constructor.
25
     * @param SettingsStorage $settings
26
     * @param string $subdomain
27
     */
28 9
    public function __construct(SettingsStorage $settings, $subdomain)
29
    {
30 9
        $this->settings = $settings;
31 9
        $this->subdomain = $subdomain;
32 9
    }
33
34
    /**
35
     * @param string $code
36
     * @param array $params
37
     * @return string
38
     * @throws InvalidArgumentException
39
     */
40 8
    public function buildMethodUrl($code, $params = [])
41
    {
42 8
        $host = $this->makeUserHost($this->subdomain);
43 8
        $path = $this->settings->getMethodPath($code);
44
45 8
        if (!empty($params)) {
46 2
            $query = '?';
47 2
            foreach ($params as $key => $param) {
48 2
                $query .= $key . '=' . $param;
49
50 2
                if ($param !== end($params)) {
51 2
                    $query .= '&';
52
                }
53
            }
54
        }
55
56 8
        $result = isset($query) ? $host . $path . $query : $host . $path;
57
58 8
        return $result;
59
    }
60
61
    /**
62
     * @param string $subdomain
63
     * @return string
64
     */
65 8
    private function makeUserHost($subdomain)
66
    {
67 8
        return $this->settings->getScheme() . '://' . $subdomain . '.' . $this->settings->getDomain();
68
    }
69
}