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

OVHServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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()) {
32
            return;
33
        }
34
35
        $this->commands([
36
            Commands\SetTempUrlKey::class,
37
        ]);
38
    }
39
40
    /**
41
     * Configures extended filesystem storage for interaction with OVH Object Storage.
42
     */
43
    protected function configureStorage(): void
44
    {
45
        Storage::extend('ovh', function ($app, array $config) {
46
            // Creates a Configuration instance.
47
            $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...
48
49
            $client = $this->makeOpenStackClient();
50
51
            // Get the Object Storage container.
52
            $container = $client->objectStoreV1()->getContainer($this->config->getContainer());
53
54
            return $this->makeFileSystem($container);
55
        });
56
    }
57
58
    /**
59
     * Creates an OpenStack client instance, needed for interaction with OVH OpenStack.
60
     *
61
     * @return OpenStack
62
     */
63
    protected function makeOpenStackClient(): OpenStack
64
    {
65
        return new OpenStack([
66
            'authUrl' => $this->config->getAuthUrl(),
67
            'region' => $this->config->getRegion(),
68
            'user' => [
69
                'name' => $this->config->getUsername(),
70
                'password' => $this->config->getPassword(),
71
                'domain' => [
72
                    'name' => $this->config->getUserDomain(),
73
                ],
74
            ],
75
            'scope' => [
76
                'project' => [
77
                    'id' => $this->config->getProjectId(),
78
                ],
79
            ],
80
        ]);
81
    }
82
83
    /**
84
     * Creates a Filesystem instance for interaction with the Object Storage.
85
     *
86
     * @param Container $container
87
     * @return Filesystem
88
     */
89
    protected function makeFileSystem(Container $container): Filesystem
90
    {
91
        return new Filesystem(
92
            new OVHSwiftAdapter($container, $this->config),
93
            [
94
                'swiftLargeObjectThreshold' => $this->config->getSwiftLargeObjectThreshold(),
95
                'swiftSegmentSize' => $this->config->getSwiftSegmentSize(),
96
                'swiftSegmentContainer' => $this->config->getSwiftSegmentContainer(),
97
            ]
98
        );
99
    }
100
}
101