AbstractAdapter::setService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace STS\StorageConnect\Drivers;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\RedirectResponse;
7
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
8
use STS\StorageConnect\Events\CloudStorageSetup;
9
use STS\StorageConnect\Models\CloudStorage;
10
use STS\StorageConnect\Models\CustomManagedCloudStorage;
11
use STS\StorageConnect\Models\Quota;
12
use STS\StorageConnect\UploadRequest;
13
use STS\StorageConnect\UploadResponse;
14
15
abstract class AbstractAdapter
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $driver;
21
22
    /**
23
     * @var string
24
     */
25
    protected $providerClass;
26
27
    /**
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * @var AbstractProvider
34
     */
35
    protected $provider;
36
37
    /**
38
     * @var mixed
39
     */
40
    protected $service;
41
42
    /**
43
     * @var array
44
     */
45
    protected $token;
46
47
    /**
48
     * @var callable
49
     */
50
    protected $tokenUpdateCallback;
51
52
    /**
53
     * DropboxAdapter constructor.
54
     *
55
     * @param array $config
56
     */
57
    public function __construct(array $config)
58
    {
59
        $this->config = $config;
60
    }
61
62
    /**
63
     * @param $token
64
     * @param null $callback
65
     *
66
     * @return $this
67
     * @paran $callback
68
     *
69
     */
70
    public function setToken($token, $callback = null)
71
    {
72
        $this->token = $token;
73
        $this->tokenUpdateCallback = $callback;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param $token
80
     */
81
    protected function updateToken($token)
82
    {
83
        if ($this->tokenUpdateCallback) {
84
            call_user_func($this->tokenUpdateCallback, $token);
85
        }
86
    }
87
88
    /**
89
     * @param CloudStorage $storage
90
     * @param null $redirectUrl
91
     *
92
     * @return RedirectResponse
93
     */
94
    public function authorize(CloudStorage $storage, $redirectUrl = null)
95
    {
96
        if (!$storage->exists) {
97
            $storage->save();
98
        }
99
100
        if ($storage instanceof CustomManagedCloudStorage) {
101
            $this->provider()->session()->put('storage-connect.custom', true);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SocialiteProviders\Manager\OAuth2\AbstractProvider as the method session() does only exist in the following sub-classes of SocialiteProviders\Manager\OAuth2\AbstractProvider: STS\StorageConnect\Drivers\Dropbox\Provider, STS\StorageConnect\Drivers\Google\Provider. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
102
        } else {
103
            $this->provider()->session()->put('storage-connect.id', $storage->id);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SocialiteProviders\Manager\OAuth2\AbstractProvider as the method session() does only exist in the following sub-classes of SocialiteProviders\Manager\OAuth2\AbstractProvider: STS\StorageConnect\Drivers\Dropbox\Provider, STS\StorageConnect\Drivers\Google\Provider. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
104
        }
105
106
        if ($redirectUrl != null) {
107
            $this->provider()->session()->put('storage-connect.redirect', $redirectUrl);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SocialiteProviders\Manager\OAuth2\AbstractProvider as the method session() does only exist in the following sub-classes of SocialiteProviders\Manager\OAuth2\AbstractProvider: STS\StorageConnect\Drivers\Dropbox\Provider, STS\StorageConnect\Drivers\Google\Provider. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
108
        }
109
110
        return $this->provider()->redirect();
111
    }
112
113
    /**
114
     * @param CloudStorage $storage
115
     *
116
     * @return void
117
     */
118
    public function finish(CloudStorage $storage)
119
    {
120
        $this->setToken($this->provider()->user()->accessTokenResponseBody);
121
122
        $storage->update(array_merge(
123
            [
124
                'token'     => $this->provider()->user()->accessTokenResponseBody,
125
                'connected' => 1,
126
                'enabled'   => 1,
127
                'enabled_at' => Carbon::now()
128
            ],
129
            $this->mapUserDetails($this->provider()->user())
130
        ));
131
        $storage->updateQuota($this->getQuota());
132
133
        event(new CloudStorageSetup($storage));
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function driver()
140
    {
141
        return $this->driver;
142
    }
143
144
    /**
145
     * @return AbstractProvider
146
     */
147
    public function provider()
148
    {
149
        if (!$this->provider) {
150
            $this->setProvider($this->makeProvider());
151
        }
152
153
        return $this->provider;
154
    }
155
156
    public function setProvider(AbstractProvider $provider)
157
    {
158
        $this->provider = $provider;
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function service()
165
    {
166
        if (!$this->service) {
167
            $this->setService($this->makeService());
168
        }
169
170
        return $this->service;
171
    }
172
173
    /**
174
     * @param $service
175
     *
176
     * @return AbstractAdapter
177
     */
178
    public function setService($service)
179
    {
180
        $this->service = $service;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return AbstractProvider
187
     */
188
    protected function makeProvider() {
189
        return app($this->providerClass);
190
    }
191
192
    /**
193
     * @return mixed
194
     */
195
    abstract protected function makeService();
196
197
    /**
198
     * @return Quota
199
     */
200
    abstract function getQuota();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
201
202
    /**
203
     * @param $user
204
     *
205
     * @return array
206
     */
207
    abstract protected function mapUserDetails($user);
208
209
    /**
210
     * @param UploadRequest $request
211
     *
212
     * @return UploadResponse
213
     */
214
    abstract function upload(UploadRequest $request);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
215
216
    /**
217
     * @param UploadResponse $response
218
     *
219
     * @return UploadResponse
220
     */
221
    abstract function checkUploadStatus(UploadResponse $response);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
222
223
    /**
224
     * @param string $remotePath
225
     *
226
     * @return bool
227
     */
228
    abstract function pathExists($remotePath);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
229
}