AbstractService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace OAuth\Common\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Exception\Exception;
7
use OAuth\Common\Http\Client\ClientInterface;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
12
/**
13
 * Abstract OAuth service, version-agnostic.
14
 */
15
abstract class AbstractService implements ServiceInterface
16
{
17
    /** @var CredentialsInterface */
18
    protected $credentials;
19
20
    /** @var ClientInterface */
21
    protected $httpClient;
22
23
    /** @var TokenStorageInterface */
24
    protected $storage;
25
26
    public function __construct(
27
        CredentialsInterface $credentials,
28
        ClientInterface $httpClient,
29
        TokenStorageInterface $storage
30
    ) {
31
        $this->credentials = $credentials;
32
        $this->httpClient = $httpClient;
33
        $this->storage = $storage;
34
    }
35
36
    /**
37
     * @param string|UriInterface $path
38
     * @param UriInterface        $baseApiUri
39
     *
40
     * @return UriInterface
41
     */
42
    protected function determineRequestUriFromPath($path, ?UriInterface $baseApiUri = null)
43
    {
44
        if ($path instanceof UriInterface) {
45
            $uri = $path;
46
        } elseif (stripos($path, 'http://') === 0 || stripos($path, 'https://') === 0) {
47
            $uri = new Uri($path);
48
        } else {
49
            if (null === $baseApiUri) {
50
                throw new Exception(
51
                    'An absolute URI must be passed to ServiceInterface::request as no baseApiUri is set.'
52
                );
53
            }
54
55
            $uri = clone $baseApiUri;
56
            if (false !== strpos($path, '?')) {
57
                $parts = explode('?', $path, 2);
58
                $path = $parts[0];
59
                $query = $parts[1];
60
                $uri->setQuery($query);
61
            }
62
63
            if ($path[0] === '/') {
64
                $path = substr($path, 1);
65
            }
66
67
            $uri->setPath($uri->getPath() . $path);
68
        }
69
70
        return $uri;
71
    }
72
73
    /**
74
     * Accessor to the storage adapter to be able to retrieve tokens.
75
     *
76
     * @return TokenStorageInterface
77
     */
78
    public function getStorage()
79
    {
80
        return $this->storage;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function service()
87
    {
88
        // get class name without backslashes
89
        $classname = get_class($this);
90
91
        return preg_replace('/^.*\\\\/', '', $classname);
92
    }
93
}
94