Completed
Pull Request — master (#46)
by
unknown
01:13
created

OVHServiceProvider::checkConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\LaravelOvh;
4
5
use Illuminate\Support\Facades\Storage;
6
use Illuminate\Support\ServiceProvider;
7
use League\Flysystem\Filesystem;
8
use OpenStack\ObjectStore\v1\Models\Container;
9
use OpenStack\OpenStack;
10
11
class OVHServiceProvider extends ServiceProvider
12
{
13
    /** @var OVHConfiguration */
14
    private $config;
15
16
    /**
17
     * Bootstrap the application services.
18
     */
19
    public function boot(): void
20
    {
21
        $this->configureCommands();
22
23
        $this->configureStorage();
24
    }
25
26
    /**
27
     * Configures available commands.
28
     */
29
    protected function configureCommands(): void
30
    {
31
        if (!$this->app->runningInConsole()) return;
32
33
        $this->commands([
34
            Commands\SetTempUrlKey::class,
35
        ]);
36
    }
37
38
    /**
39
     * Configures extended filesystem storage for interaction with OVH Object Storage.
40
     */
41
    protected function configureStorage(): void
42
    {
43
        Storage::extend('ovh', function ($app, array $config) {
44
            // Creates a Configuration instance.
45
            $this->config = OVHConfiguration::make($config);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Sausin\LaravelOvh\OVHConfiguration::make($config) of type object<self> is incompatible with the declared type object<Sausin\LaravelOvh\OVHConfiguration> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
47
            $client = $this->makeOpenStackClient();
48
49
            // Get the Object Storage container.
50
            $container = $client->objectStoreV1()->getContainer($this->config->getContainer());
51
52
            return $this->makeFileSystem($container);
53
        });
54
    }
55
56
    /**
57
     * Creates an OpenStack client instance, needed for interaction with OVH OpenStack.
58
     *
59
     * @return OpenStack
60
     */
61
    protected function makeOpenStackClient(): OpenStack
62
    {
63
        return new OpenStack([
64
            'authUrl' => $this->config->getAuthUrl(),
65
            'region' => $this->config->getRegion(),
66
            'user' => [
67
                'name' => $this->config->getUsername(),
68
                'password' => $this->config->getPassword(),
69
                'domain' => [
70
                    'name' => $this->config->getUserDomain(),
71
                ],
72
            ],
73
            'scope' => [
74
                'project' => [
75
                    'id' => $this->config->getProjectId(),
76
                ],
77
            ],
78
        ]);
79
    }
80
81
    /**
82
     * Creates a Filesystem instance for interaction with the Object Storage
83
     *
84
     * @param Container $container
85
     * @return Filesystem
86
     */
87
    protected function makeFileSystem(Container $container): Filesystem
88
    {
89
        return new Filesystem(
90
            new OVHSwiftAdapter($container, $this->config),
91
            [
92
                'swiftLargeObjectThreshold' => $this->config->getSwiftLargeObjectThreshold(),
93
                'swiftSegmentSize' => $this->config->getSwiftSegmentSize(),
94
                'swiftSegmentContainer' => $this->config->getSwiftSegmentContainer(),
95
            ]
96
        );
97
    }
98
}
99