Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

AbstractService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 3
dl 0
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
C determineRequestUriFromPath() 0 30 7
A getStorage() 0 4 1
A service() 0 7 1
A account() 0 4 1
1
<?php
2
3
namespace OAuth\Common\Service;
4
5
use OAuth\Common\Consumer\Credentials;
6
use OAuth\Common\Consumer\CredentialsInterface;
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\Exception\Exception;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
13
/**
14
 * Abstract OAuth service, version-agnostic
15
 */
16
abstract class AbstractService implements ServiceInterface
17
{
18
    /** @var Credentials */
19
    protected $credentials;
20
21
    /** @var ClientInterface */
22
    protected $httpClient;
23
24
    /** @var TokenStorageInterface */
25
    protected $storage;
26
27
    /** @var null|string */
28
    protected $account = null;
29
30
    /**
31
     * @param CredentialsInterface  $credentials
32
     * @param ClientInterface       $httpClient
33
     * @param TokenStorageInterface $storage
34
     * @param string                $account
35
     *
36
     */
37
    public function __construct(
38
        CredentialsInterface $credentials,
39
        ClientInterface $httpClient,
40
        TokenStorageInterface $storage,
41
        $account = null
42
    ) {
43
        $this->credentials = $credentials;
0 ignored issues
show
Documentation Bug introduced by
$credentials is of type object<OAuth\Common\Cons...r\CredentialsInterface>, but the property $credentials was declared to be of type object<OAuth\Common\Consumer\Credentials>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
44
        $this->httpClient = $httpClient;
45
        $this->storage = $storage;
46
        $this->account = $account;
47
    }
48
49
    /**
50
     * @param UriInterface|string $path
51
     * @param UriInterface        $baseApiUri
52
     *
53
     * @return UriInterface
54
     *
55
     * @throws Exception
56
     */
57
    protected function determineRequestUriFromPath($path, UriInterface $baseApiUri = null)
58
    {
59
        if ($path instanceof UriInterface) {
60
            $uri = $path;
61
        } elseif (stripos($path, 'http://') === 0 || stripos($path, 'https://') === 0) {
62
            $uri = new Uri($path);
63
        } else {
64
            if (null === $baseApiUri) {
65
                throw new Exception(
66
                    'An absolute URI must be passed to ServiceInterface::request as no baseApiUri is set.'
67
                );
68
            }
69
70
            $uri = clone $baseApiUri;
71
            if (false !== strpos($path, '?')) {
72
                $parts = explode('?', $path, 2);
73
                $path = $parts[0];
74
                $query = $parts[1];
75
                $uri->setQuery($query);
76
            }
77
78
            if ($path[0] === '/') {
79
                $path = substr($path, 1);
80
            }
81
82
            $uri->setPath($uri->getPath() . $path);
83
        }
84
85
        return $uri;
86
    }
87
88
    /**
89
     * Accessor to the storage adapter to be able to retrieve tokens
90
     *
91
     * @return TokenStorageInterface
92
     */
93
    public function getStorage()
94
    {
95
        return $this->storage;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function service()
102
    {
103
        // get class name without backslashes
104
        $classname = get_class($this);
105
106
        return preg_replace('/^.*\\\\/', '', $classname);
107
    }
108
109
    /**
110
     * @return null|string
111
     */
112
    public function account()
113
    {
114
        return $this->account;
115
    }
116
}
117