Completed
Pull Request — master (#76)
by Thibaud
02:50
created

Endpoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhraseanetSDK\Http;
4
5
use PhraseanetSDK\ApplicationInterface;
6
7
class Endpoint
8
{
9
10
    /**
11
     * @var string
12
     */
13
    private $url;
14
15
    /**
16
     * @param string $url
17
     */
18
    public function __construct($url)
19
    {
20
        $this->url = $this->normalizeEndpoint($url);
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function getUrl()
27
    {
28
        return $this->url;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function __toString()
35
    {
36
        return $this->getUrl();
37
    }
38
39
    /**
40
     * @param $endpoint
41
     * @return string
42
     */
43
    private function normalizeEndpoint($endpoint)
44
    {
45
        $versionMountPoint = ApplicationInterface::API_MOUNT_POINT;
46
47
        // Test if URL already ends with API_MOUNT_POINT
48
        $mountPoint = substr(trim($endpoint, '/'), -strlen($versionMountPoint));
49
50
        if ($versionMountPoint !== $mountPoint) {
51
            $endpoint = sprintf('%s%s/', trim($endpoint, '/'), $versionMountPoint);
52
        }
53
54
        return $endpoint;
55
    }
56
}
57