Completed
Push — master ( b45c76...c88acf )
by Saurabh
01:03
created

OVHServiceProvider::getLargeObjectConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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 OVHConfiguration $config;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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);
48
49
            $client = $this->makeOpenStackClient();
50
51
            // Get the Object Storage container.
52
            $container = $client->objectStoreV1()->getContainer($this->config->container);
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->authUrl,
67
            'region' => $this->config->region,
68
            'user' => [
69
                'name' => $this->config->username,
70
                'password' => $this->config->password,
71
                'domain' => [
72
                    'name' => $this->config->userDomain,
73
                ],
74
            ],
75
            'scope' => [
76
                'project' => [
77
                    'id' => $this->config->projectId,
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->swiftLargeObjectThreshold,
95
                'swiftSegmentSize' => $this->config->swiftSegmentSize,
96
                'swiftSegmentContainer' => $this->config->swiftSegmentContainer,
97
            ]
98
        );
99
    }
100
}
101