Completed
Push — master ( a4d4bb...44d25f )
by Saurabh
01:07
created

OVHServiceProvider::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        if ($this->app->runningInConsole()) {
37
            $this->commands([
38
                Commands\SetTempUrlKey::class
39
            ]);
40
        }
41
    }
42
43
    /**
44
     * Check that the config is properly setup.
45
     *
46
     * @param  array $config
47
     * @return void|BadMethodCallException
48
     */
49
    protected function checkConfig($config)
50
    {
51
        // needed keys
52
        $needKeys = ['server', 'region', 'user', 'pass', 'userDomain', 'projectId', 'container'];
53
54
        if (count(array_intersect($needKeys, array_keys($config))) === count($needKeys)) {
55
            return;
56
        }
57
58
        // if the configuration wasn't complete, throw an exception
59
        throw new BadMethodCallException('Need following keys ' . implode(', ', $needKeys));
60
    }
61
62
    /**
63
     * Make the client needed for interaction with OVH OpenStack.
64
     *
65
     * @param  array $config
66
     * @return \OpenStack\OpenStack
67
     */
68
    protected function makeClient($config)
69
    {
70
        // setup the client for OpenStack
71
        return new OpenStack([
72
            'authUrl' => $config['server'],
73
            'region' => $config['region'],
74
            'user' => [
75
                'name' => $config['user'],
76
                'password' => $config['pass'],
77
                'domain' => [
78
                    'name' => $config['userDomain'],
79
                ],
80
            ],
81
            'scope' => [
82
                'project' => [
83
                    'id' => $config['projectId'],
84
                ],
85
            ],
86
        ]);
87
    }
88
89
    /**
90
     * Return the config variables required by the adapter.
91
     *
92
     * @param  array &$config
93
     * @return array
94
     */
95
    protected function getVars(&$config)
96
    {
97
        return [
98
            'region' => $config['region'],
99
            'projectId' => $config['projectId'],
100
            'container' => $config['container'],
101
            'urlKey' => isset($config['urlKey']) ? $config['urlKey'] : null,
102
            'endpoint' => isset($config['endpoint']) ? $config['endpoint'] : null,
103
        ];
104
    }
105
106
    /**
107
     * Return the config variables required for large object.
108
     *
109
     * @param  array &$config
110
     * @return array
111
     */
112
    protected function getLargeObjectConfig(&$config)
113
    {
114
        $largeObjConfig = [];
115
116
        $largeObjVars = [
117
            'swiftLargeObjectThreshold',
118
            'swiftSegmentSize',
119
            'swiftSegmentContainer',
120
        ];
121
122
        foreach ($largeObjVars as $key) {
123
            if (isset($config[$key])) {
124
                $largeObjConfig[$key] = $config[$key];
125
            }
126
        }
127
128
        return $largeObjConfig;
129
    }
130
}
131