Completed
Push — master ( b8c738...af698e )
by Saurabh
23s
created

OVHSwiftAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 136
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUrl() 0 6 1
A getUrlConfirm() 0 13 2
A getTemporaryUrl() 0 32 3
A getEndpoint() 0 18 2
A checkParams() 0 6 3
1
<?php
2
3
namespace Sausin\LaravelOvh;
4
5
use Carbon\Carbon;
6
use BadMethodCallException;
7
use OpenStack\ObjectStore\v1\Service;
8
use OpenStack\Common\Error\BadResponseError;
9
use OpenStack\ObjectStore\v1\Models\Container;
10
use Nimbusoft\Flysystem\OpenStack\SwiftAdapter;
11
12
class OVHSwiftAdapter extends SwiftAdapter
13
{
14
    /**
15
     * URL base path variables for OVH service
16
     * the HTTPS url is typically of the format
17
     * https://storage.[REGION].cloud.ovh.net/v1/AUTH_[PROJECT_ID]/[CONTAINER_NAME].
18
     * @var array
19
     */
20
    protected $urlVars;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param Container $container
26
     * @param array     $urlVars
27
     * @param string    $prefix
28
     */
29
    public function __construct(Container $container, $urlVars = [], $prefix = null)
30
    {
31
        parent::__construct($container, $prefix);
32
33
        $this->urlVars = $urlVars;
34
    }
35
36
    /**
37
     * Custom function to comply with the Storage::url() function in laravel
38
     * without checking the existence of a file (faster).
39
     *
40
     * @param  string $path
41
     * @return string
42
     */
43
    public function getUrl($path)
44
    {
45
        $this->checkParams();
46
47
        return $this->getEndpoint().$path;
48
    }
49
50
    /**
51
     * Custom function to get a url with confirmed file existence.
52
     *
53
     * @param  string $path
54
     * @return string
55
     */
56
    public function getUrlConfirm($path)
57
    {
58
        // check if object exists
59
        try {
60
            $this->getTimestamp($path);
61
        } catch (BadResponseError $e) {
62
            throw $e;
63
        }
64
65
        $this->checkParams();
66
67
        return $this->getEndpoint().$path;
68
    }
69
70
    /**
71
     * Generate a temporary URL for private containers.
72
     *
73
     * @param  string   $path
74
     * @param  Carbon   $expiration
75
     * @param  array    $options
76
     * @return string
77
     */
78
    public function getTemporaryUrl($path, $expiration, $options = [])
79
    {
80
        $this->checkParams();
81
82
        // expiry is relative to current time
83
        $expiresAt = $expiration instanceof Carbon ? $expiration->timestamp : (int) (time() + 60 * 60);
84
85
        // get the method
86
        $method = isset($options['method']) ? $options['method'] : 'GET';
87
88
        // the url on the OVH host
89
        $codePath = sprintf(
90
            '/v1/AUTH_%s/%s/%s',
91
            $this->urlVars['projectId'],
92
            $this->urlVars['container'],
93
            $path
94
        );
95
96
        // body for the HMAC hash
97
        $body = sprintf("%s\n%s\n%s", $method, $expiresAt, $codePath);
98
99
        // the actual hash signature
100
        $signature = hash_hmac('sha1', $body, $this->urlVars['urlKey']);
101
102
        // return the url
103
        return sprintf(
104
            '%s?temp_url_sig=%s&temp_url_expires=%s',
105
            $this->getEndpoint().$path,
106
            $signature,
107
            $expiresAt
108
        );
109
    }
110
111
    /**
112
     * Gets the endpoint url of the bucket.
113
     *
114
     * @return string
115
     */
116
    protected function getEndpoint()
117
    {
118
        $this->checkParams();
119
120
        $endpoint = isset($this->urlVars['endpoint'])
121
            // allows assigning custom endpoint url
122
            ? $this->urlVars['endpoint']
123
            // if no custom endpoint assigned, use traditional swift v1 endpoint
124
            : sprintf(
125
                'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/',
126
                $this->urlVars['region'],
127
                $this->urlVars['projectId'],
128
                $this->urlVars['container']
129
            );
130
131
        // ensures there's one trailing slash for endpoint
132
        return rtrim($endpoint, '/').'/';
133
    }
134
135
    /**
136
     * Check if the url support variables have
137
     * been correctly defined.
138
     *
139
     * @return void|BadMethodCallException
140
     */
141
    protected function checkParams()
142
    {
143
        if (! is_array($this->urlVars) || count($this->urlVars) !== 5) {
144
            throw new BadMethodCallException('Insufficient Url Params', 1);
145
        }
146
    }
147
}
148