UrlBuilder::makeUserHost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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