Completed
Push — master ( 9e9fea...cb71ba )
by Riccardo
10s
created

ClientBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 78
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 30 1
A getStack() 0 6 1
A addHeaderMiddlewareToStack() 0 15 1
A addRetryMiddlewareToStack() 0 15 1
A mergeOptions() 0 5 1
1
<?php
2
3
namespace Softonic\OAuth2\Guzzle\Middleware;
4
5
use GuzzleHttp\HandlerStack as Stack;
6
use League\OAuth2\Client\Provider\AbstractProvider as OAuth2Provider;
7
use Psr\Cache\CacheItemPoolInterface as Cache;
8
9
class ClientBuilder
10
{
11 4
    public static function build(
12
        OAuth2Provider $oauthProvider,
13
        array $tokenOptions,
14
        Cache $cache,
15
        array $guzzleOptions = null
16
    ): \GuzzleHttp\Client {
17 4
        $cacheHandler = new AccessTokenCacheHandler($cache);
18
19 4
        $stack = static::getStack();
0 ignored issues
show
Bug introduced by
Since getStack() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getStack() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
20
21 4
        $stack = static::addHeaderMiddlewareToStack(
0 ignored issues
show
Bug introduced by
Since addHeaderMiddlewareToStack() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of addHeaderMiddlewareToStack() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
22 4
            $stack,
23 4
            $oauthProvider,
24 4
            $tokenOptions,
25 4
            $cacheHandler
26
        );
27 4
        $stack = static::addRetryMiddlewareToStack(
0 ignored issues
show
Bug introduced by
Since addRetryMiddlewareToStack() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of addRetryMiddlewareToStack() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
28 4
            $stack,
29 4
            $oauthProvider,
30 4
            $tokenOptions,
31 4
            $cacheHandler
32
        );
33
34
        $defaultOptions = [
35 4
            'handler' => $stack,
36
        ];
37 4
        $guzzleOptions = static::mergeOptions($defaultOptions, $guzzleOptions);
0 ignored issues
show
Bug introduced by
Since mergeOptions() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of mergeOptions() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
38
39 4
        return new \GuzzleHttp\Client($guzzleOptions);
40
    }
41
42 4
    private static function getStack(): Stack
43
    {
44 4
        $stack = new Stack();
45 4
        $stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());
46 4
        return $stack;
47
    }
48
49 4
    private static function addHeaderMiddlewareToStack(
50
        Stack $stack,
51
        OAuth2Provider $oauthProvider,
52
        array $tokenOptions,
53
        AccessTokenCacheHandler $cacheHandler
54
    ): Stack {
55 4
        $addAuthorizationHeader = new AddAuthorizationHeader(
56 4
            $oauthProvider,
57 4
            $tokenOptions,
58 4
            $cacheHandler
59
        );
60
61 4
        $stack->push(\GuzzleHttp\Middleware::mapRequest($addAuthorizationHeader));
62 4
        return $stack;
63
    }
64
65 4
    private static function addRetryMiddlewareToStack(
66
        Stack $stack,
67
        OAuth2Provider $oauthProvider,
68
        array $tokenOptions,
69
        AccessTokenCacheHandler $cacheHandler
70
    ): Stack {
71 4
        $retryOnAuthorizationError = new RetryOnAuthorizationError(
72 4
            $oauthProvider,
73 4
            $tokenOptions,
74 4
            $cacheHandler
75
        );
76
77 4
        $stack->push(\GuzzleHttp\Middleware::retry($retryOnAuthorizationError));
78 4
        return $stack;
79
    }
80
81 4
    private static function mergeOptions(array $defaultOptions, array $options = null): array
82
    {
83 4
        $options = $options ?? [];
84 4
        return array_merge($options, $defaultOptions);
85
    }
86
}
87