Passed
Push — master ( 970557...d136fe )
by Amin
04:22
created

GoogleCloudStorage::getBucket()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Streaming\Clouds;
12
13
use Streaming\Exception\InvalidArgumentException;
14
use Streaming\Exception\RuntimeException;
15
16
class GoogleCloudStorage implements CloudInterface
17
{
18
    /** @var \Google\Cloud\Storage\StorageClient*/
19
    private $storage;
20
    /**
21
     * GoogleCloudStorage constructor.
22
     * @param  array $config
23
     */
24
    public function __construct(array $config)
25
    {
26
        if(!class_exists('\Google\Cloud\Storage\StorageClient')){
27
            throw new RuntimeException('Google\Cloud\Storage\StorageClient not found. make sure the package is installed: composer require google/cloud-storage');
28
        }
29
30
        try {
31
            $this->storage = new \Google\Cloud\Storage\StorageClient($config);
32
        } catch (\Exception $e) {
33
            throw new InvalidArgumentException(sprintf("Invalid inputs:\n %s", $e->getMessage()), $e->getCode(), $e);
34
        }
35
    }
36
37
    /**
38
     * @param  string $dir
39
     * @param  array $options
40
     */
41
    public function uploadDirectory(string $dir, array $options): void
42
    {
43
        $bucket = $this->getBucket($options);
44
        unset($options['bucket'], $options['user_project']);
45
46
        try {
47
            foreach (scandir($dir) as $filename) {
48
                $path = $dir . DIRECTORY_SEPARATOR . $filename;
49
50
                if (is_file($path)) {
51
                    $bucket->upload(fopen($path, 'r'), $options);
52
                }
53
            }
54
        } catch (\Exception $e) {
55
            throw new RuntimeException(sprintf("An error occurred while uploading files: %s", $e->getMessage()), $e->getCode(), $e);
56
        }
57
    }
58
59
    /**
60
     * @param  string $save_to
61
     * @param  array $options
62
     */
63
    public function download(string $save_to, array $options): void
64
    {
65
        $bucket = $this->getBucket($options);
66
67
        if(!isset($options['bucket'])){
68
            throw new InvalidArgumentException("You need to set the bucket name in the option array");
69
        }
70
        $name = $options['object_name'];
71
        unset($options['bucket'], $options['user_project'], $options['object_name']);
72
73
        try {
74
            $bucket->object($name, $options)->downloadToFile($save_to);
75
        } catch (\Exception $e) {
76
            throw new RuntimeException(sprintf("An error occurred while downloading the file: %s", $e->getMessage()), $e->getCode(), $e);
77
        }
78
    }
79
80
    /**
81
     * @param array $options
82
     * @return \Google\Cloud\Storage\Bucket
83
     */
84
    private function getBucket(array $options): \Google\Cloud\Storage\Bucket
85
    {
86
        if(!isset($options['bucket'])){
87
            throw new InvalidArgumentException("You need to set the bucket name in the option array");
88
        }
89
        try{
90
            return $this->storage->bucket($options['bucket'], $options['user_project'] ?? false);
91
        }catch (\Exception $e){
92
            throw new RuntimeException(sprintf("An error occurred while opening the bucket: %s", $e->getMessage()), $e->getCode(), $e);
93
        }
94
    }
95
}