OAuthBehavior   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A callbackUrl() 0 8 3
A session() 0 4 1
A getState() 0 7 1
A user() 0 8 2
A name() 0 4 1
1
<?php
2
3
namespace STS\StorageConnect\Drivers;
4
5
use Illuminate\Session\Store;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use SocialiteProviders\Manager\OAuth2\User;
9
use StorageConnect;
10
11
trait OAuthBehavior
12
{
13
    /**
14
     * @var User
15
     */
16
    protected $user;
17
18
    /**
19
     * @var array
20
     */
21
    protected $token;
22
23
    /**
24
     * EnhancedProvider constructor.
25
     *
26
     * @param array $config
27
     */
28
    public function __construct(array $config)
29
    {
30
        parent::__construct(
31
            app('request'), $config['client_id'],
32
            $config['client_secret'], $this->callbackUrl($config, app('request')),
33
            Arr::get($config, 'guzzle', [])
34
        );
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function callbackUrl($config, $request)
41
    {
42
        return sprintf("https://%s/%s/callback/%s",
43
            Arr::get($config, 'callback_domain') ?: config('storage-connect.callback_domain') ?: $request->getHost(),
44
            config('storage-connect.path'),
45
            $this->name()
46
        );
47
    }
48
49
    /**
50
     * @return Store
51
     */
52
    public function session()
53
    {
54
        return $this->request->session();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    protected function getState()
61
    {
62
        return base64_encode(json_encode(array_merge(
63
            ['csrf' => Str::random(40)],
64
            StorageConnect::getState()
65
        )));
66
    }
67
68
    /**
69
     * @return User
70
     */
71
    public function user()
72
    {
73
        if (!$this->user) {
74
            $this->user = parent::user();
75
        }
76
77
        return $this->user;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function name()
84
    {
85
        return strtolower(self::IDENTIFIER);
86
    }
87
}