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

MicrosoftAzure::download()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 4
eloc 7
c 2
b 1
f 1
nc 4
nop 2
dl 0
loc 11
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
12
13
namespace Streaming\Clouds;
14
15
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\Exception\RuntimeException;
18
use Streaming\File;
19
20
class MicrosoftAzure implements CloudInterface
21
{
22
    /** @var \MicrosoftAzure\Storage\Blob\BlobRestProxy*/
23
    private $blobClient;
24
25
    /**
26
     * MicrosoftAzure constructor.
27
     * @param  $connection
28
     * @param array $options
29
     */
30
    public function __construct($connection, array $options = [])
31
    {
32
        if(!class_exists('\MicrosoftAzure\Storage\Blob\BlobRestProxy')){
33
            throw new RuntimeException('MicrosoftAzure\Storage\Blob\BlobRestProxy not found. make sure the package is installed: composer require microsoft/azure-storage-blob');
34
        }
35
36
        $this->blobClient = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($connection, $options);
37
    }
38
39
    /**
40
     * Upload a entire directory to a cloud
41
     * @param  string $dir
42
     * @param  array $options
43
     */
44
    public function uploadDirectory(string $dir, array $options): void
45
    {
46
        if(!isset($options['container'])){
47
            throw new InvalidArgumentException("You should set the container in the array");
48
        }
49
50
        try {
51
            foreach (scandir($dir) as $filename) {
52
                $path = $dir . DIRECTORY_SEPARATOR . $filename;
53
54
                if (is_file($path)) {
55
                    $this->blobClient->createBlockBlob($options['container'], $filename, fopen($path, "r"), $options['CreateBlockBlobOptions'] ?? null);
0 ignored issues
show
Bug introduced by
It seems like fopen($path, 'r') can also be of type false; however, parameter $content of MicrosoftAzure\Storage\B...roxy::createBlockBlob() does only seem to accept Psr\Http\Message\StreamInterface|resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                    $this->blobClient->createBlockBlob($options['container'], $filename, /** @scrutinizer ignore-type */ fopen($path, "r"), $options['CreateBlockBlobOptions'] ?? null);
Loading history...
56
                }
57
            }
58
        } catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
59
            throw new RuntimeException(sprintf("An error occurred while uploading files: %s", $e->getMessage()), $e->getCode(), $e);
60
        }
61
    }
62
63
    /**
64
     * Download a file from a cloud
65
     * @param  string $save_to
66
     * @param  array $options
67
     */
68
    public function download(string $save_to, array $options): void
69
    {
70
        if(!isset($options['container']) ||  !isset($options['blob'])){
71
            throw new InvalidArgumentException("You should set the container and blob in the array");
72
        }
73
74
        try {
75
            $blob = $this->blobClient->getBlob($options['container'], $options['blob']);
76
            File::put($save_to, $blob->getContentStream());
77
        } catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
78
            throw new RuntimeException(sprintf("An error occurred while downloading the file: %s", $e->getMessage()), $e->getCode(), $e);
79
        }
80
    }
81
}