Completed
Push — master ( b96d25...64cc65 )
by Amin
03:00
created

GoogleCloudStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * This file is part of the ******* 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
12
13
namespace Streaming;
14
15
use Google\Cloud\Storage\StorageClient;
16
17
class GoogleCloudStorage
18
{
19
    /**
20
     * @var \Google\Cloud\Storage\Bucket $bucket
21
     */
22
    private $bucket;
23
24
    /**
25
     * GoogleCloudStorage constructor.
26
     * @param string $bucket
27
     * @param bool $userProject
28
     * @param array $config
29
     */
30
    public function __construct(array $config, string $bucket, $userProject = false)
31
    {
32
        $storage = new StorageClient($config);
33
        $this->bucket = $storage->bucket($bucket, $userProject);
34
    }
35
36
    /**
37
     * @param string $dir
38
     * @param array $options
39
     */
40
    public function uploadDirectory(string $dir, array $options = [])
41
    {
42
        foreach (scandir($dir) as $key => $filename) {
43
            $path = $dir . DIRECTORY_SEPARATOR . $filename;
44
45
            if (is_file($path)) {
46
                $this->bucket->upload(fopen($path, 'r'), $options);
47
            }
48
        }
49
    }
50
51
    public function download(string $name, string $save_to)
52
    {
53
        return $this->bucket->object($name)
54
            ->downloadToFile($save_to);
55
    }
56
}