Completed
Push — master ( ebfc59...a4d4bb )
by Saurabh
16s queued 10s
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
    /**
132
     * Expose the container to allow for modification to metadata
133
     *
134
     * @return \OpenStack\ObjectStore\v1\Models\Container;
0 ignored issues
show
Documentation introduced by
The doc-type \OpenStack\ObjectStore\v1\Models\Container; could not be parsed: Expected "|" or "end of type", but got ";" at position 42. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
     */
136
    public function getContainer()
137
    {
138
        return $this->container;
0 ignored issues
show
Bug introduced by
The property container 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...
139
    }
140
}
141