1 | <?php |
||
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) |
||
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) |
||
124 | } |
||
125 |