StorageConnectManager::isSupportedDriver()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.6111
cc 5
nc 5
nop 1
1
<?php
2
3
namespace STS\StorageConnect;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Manager;
7
use Illuminate\Support\Str;
8
use Illuminate\Http\RedirectResponse;
9
use InvalidArgumentException;
10
use Laravel\Socialite\Two\AbstractProvider;
11
use STS\StorageConnect\Drivers\AbstractAdapter;
12
use STS\StorageConnect\Models\CloudStorage;
13
use STS\StorageConnect\Models\CustomManagedCloudStorage;
14
use UnexpectedValueException;
15
16
/**
17
 * Class StorageConnectManager
18
 * @package STS\StorageConnect
19
 */
20
class StorageConnectManager extends Manager
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $state = [];
26
27
    /**
28
     * @var callable
29
     */
30
    protected $saveCallback;
31
32
    /**
33
     * @var callable
34
     */
35
    protected $loadCallback;
36
37
    /**
38
     * @var string
39
     */
40
    public static $appName = null;
41
42
    /**
43
     * @var array
44
     */
45
    protected $registered = [];
46
47
    /**
48
     * @param $driver
49
     *
50
     * @return AbstractAdapter
51
     */
52
    public function adapter($driver)
53
    {
54
        $this->verifyDriver($driver);
55
56
        return $this->container->make("sts.storage-connect.adapter.$driver");
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getDefaultDriver()
63
    {
64
        return $this->container['config']['storage-connect.default'];
65
    }
66
67
    /**
68
     * @param $driver
69
     *
70
     * @return bool
71
     */
72
    public function isSupportedDriver($driver)
73
    {
74
        return in_array($driver, $this->registered)
75
            && in_array($driver, $this->container['config']['storage-connect.enabled'])
76
            && is_array($this->container['config']["services.$driver"])
77
            && $this->container['config']["services.$driver.client_id"] != null
78
            && $this->container['config']["services.$driver.client_secret"] != null;
79
    }
80
81
    /**
82
     * @param $driver
83
     *
84
     * @return bool
85
     */
86
    public function verifyDriver($driver)
87
    {
88
        if(!$this->isSupportedDriver($driver)) {
89
            throw new InvalidArgumentException("Driver [$driver] not supported.");
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * @param string $driver
97
     *
98
     * @return CloudStorage
99
     */
100
    protected function createDriver($driver)
101
    {
102
        $this->verifyDriver($driver);
103
104
        $attributes = $this->load($driver);
105
106
        if (!is_array($attributes)) {
107
            $attributes = json_decode($attributes, true);
108
        }
109
110
        if (is_array($attributes) && array_key_exists('driver', $attributes)) {
111
            $instance = (new CustomManagedCloudStorage)->restore($attributes, $this->saveCallback);
112
        } else {
113
            $instance = CustomManagedCloudStorage::init($driver, $this->saveCallback);
114
        }
115
116
        return $instance;
117
    }
118
119
    /**
120
     * @param $callback
121
     */
122
    public function loadUsing($callback)
123
    {
124
        $this->loadCallback = $callback;
125
    }
126
127
    /**
128
     * @param $driver
129
     *
130
     * @return mixed
131
     */
132
    protected function load($driver)
133
    {
134
        if(!$this->loadCallback) {
135
            throw new UnexpectedValueException("No callback provided to load storage connection");
136
        }
137
138
        return call_user_func($this->loadCallback, $driver);
139
    }
140
141
    /**
142
     * @param $callback
143
     */
144
    public function saveUsing($callback)
145
    {
146
        $this->saveCallback = $callback;
147
    }
148
149
    /**
150
     * @param array $state
151
     */
152
    public function includeState(array $state)
153
    {
154
        $this->state = array_merge($this->state, $state);
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getState()
161
    {
162
        return $this->state;
163
    }
164
165
    /**
166
     * @param $driver
167
     *
168
     * @return RedirectResponse
169
     */
170
    public function finish($driver)
171
    {
172
        $props = (array) $this->container['request']->session()->pull('storage-connect');
173
174
        $storage = Arr::get($props, 'custom') == true
175
            ? $this->driver($driver)
176
            : CloudStorage::findOrFail(Arr::get($props, 'id'));
177
178
        $this->adapter($driver)->finish($storage);
179
180
        return $this->redirectAfterConnect(Arr::get($props, 'redirect'));
181
    }
182
183
    /**
184
     * @param $redirectUrl
185
     *
186
     * @return RedirectResponse
187
     */
188
    public function redirectAfterConnect($redirectUrl = null)
189
    {
190
        return new RedirectResponse(
191
            $redirectUrl == null
192
                ? $this->container['config']->get('storage-connect.redirect_after_connect')
193
                : $redirectUrl
194
        );
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function appName()
201
    {
202
        if ($appName = self::appName) {
203
            return $appName;
204
        }
205
206
        if ($appName = $this->container['config']->get('storage-connect.app_name')) {
207
            return $appName;
208
        }
209
210
        return $this->container['config']->get('app.name');
211
    }
212
213
    /**
214
     * Dynamically call the default connection instance.
215
     *
216
     * @param  string $method
217
     * @param  array $parameters
218
     *
219
     * @return mixed
220
     */
221
    public function __call($method, $parameters)
222
    {
223
        return $this->driver()->$method(...$parameters);
224
    }
225
226
    /**
227
     * @param $name
228
     * @param $abstractClass
229
     * @param $providerClass
230
     */
231
    public function register($name, $abstractClass, $providerClass)
232
    {
233
        $this->container->bind($abstractClass, function($app) use($abstractClass, $name) {
234
            return new $abstractClass($app['config']->get("services.$name"));
235
        });
236
        $this->container->alias($abstractClass, "sts.storage-connect.adapter.$name");
237
238
        $this->container->bind($providerClass, function($app) use($providerClass, $name) {
239
            return new $providerClass($app['config']->get("services.$name"));
240
        });
241
        $this->container->alias($providerClass, "sts.storage-connect.provider.$name");
242
243
        $this->registered[] = $name;
244
    }
245
}