Completed
Push — master ( 94b63d...c6afaf )
by Saurabh
15s queued 12s
created

OVHServiceProvider::getLargeObjectConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Sausin\LaravelOvh;
4
5
use BadMethodCallException;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\ServiceProvider;
8
use League\Flysystem\Filesystem;
9
use OpenStack\OpenStack;
10
11
class OVHServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.
15
     *
16
     * @return void
17
     */
18
    public function boot()
19
    {
20
        Storage::extend('ovh', function ($app, $config) {
21
            // check if the config is complete
22
            $this->checkConfig($config);
23
24
            // create the client
25
            $client = $this->makeClient($config);
26
27
            // get the container
28
            $container = $client->objectStoreV1()->getContainer($config['container']);
29
30
            return new Filesystem(
31
                new OVHSwiftAdapter($container, $this->getVars($config)),
32
                $this->getLargeObjectConfig($config)
33
            );
34
        });
35
    }
36
37
    /**
38
     * Check that the config is properly setup.
39
     *
40
     * @param  array $config
41
     * @return void|BadMethodCallException
42
     */
43
    protected function checkConfig($config)
44
    {
45
        // needed keys
46
        $needKeys = ['server', 'region', 'user', 'pass', 'userDomain', 'projectId', 'container'];
47
48
        if (count(array_intersect($needKeys, array_keys($config))) === count($needKeys)) {
49
            return;
50
        }
51
52
        // if the configuration wasn't complete, throw an exception
53
        throw new BadMethodCallException('Need following keys '.implode(', ', $needKeys));
54
    }
55
56
    /**
57
     * Make the client needed for interaction with OVH OpenStack.
58
     *
59
     * @param  array $config
60
     * @return \OpenStack\OpenStack
61
     */
62
    protected function makeClient($config)
63
    {
64
        // setup the client for OpenStack
65
        return new OpenStack([
66
            'authUrl' => $config['server'],
67
            'region' => $config['region'],
68
            'user' => [
69
                'name' => $config['user'],
70
                'password' => $config['pass'],
71
                'domain' => [
72
                    'name' => $config['userDomain'],
73
                ],
74
            ],
75
            'scope' => [
76
                'project' => [
77
                    'id' => $config['projectId'],
78
                ],
79
            ],
80
        ]);
81
    }
82
83
    /**
84
     * Return the config variables required by the adapter.
85
     *
86
     * @param  array &$config
87
     * @return array
88
     */
89
    protected function getVars(&$config)
90
    {
91
        return [
92
            'region' => $config['region'],
93
            'projectId' => $config['projectId'],
94
            'container' => $config['container'],
95
            'urlKey' => isset($config['urlKey']) ? $config['urlKey'] : null,
96
            'endpoint' => isset($config['endpoint']) ? $config['endpoint'] : null,
97
        ];
98
    }
99
100
    /**
101
     * Return the config variables required for large object.
102
     *
103
     * @param  array &$config
104
     * @return array
105
     */
106
    protected function getLargeObjectConfig(&$config)
107
    {
108
        $largeObjConfig = [];
109
110
        $largeObjVars = [
111
            'swiftLargeObjectThreshold',
112
            'swiftSegmentSize',
113
            'swiftSegmentContainer',
114
        ];
115
116
        foreach ($largeObjVars as $key) {
117
            if (isset($config[$key])) {
118
                $largeObjConfig[$key] = $config[$key];
119
            }
120
        }
121
122
        return $largeObjConfig;
123
    }
124
}
125