Completed
Push — master ( baa637...9b813d )
by Amin
04:08 queued 11s
created

MicrosoftAzure::uploadDirectory()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 4
eloc 8
nc 8
nop 2
dl 0
loc 14
rs 10
c 1
b 1
f 1
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 MicrosoftAzure\Storage\Blob\BlobRestProxy;
17
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
18
use Streaming\Exception\RuntimeException;
19
20
class MicrosoftAzure implements CloudInterface
21
{
22
    private $blobClient;
23
24
    /**
25
     * MicrosoftAzure constructor.
26
     * @param $connectionString
27
     */
28
    public function __construct($connectionString)
29
    {
30
        $this->blobClient = BlobRestProxy::createBlobService($connectionString);
31
    }
32
33
    /**
34
     * Upload a entire directory to a cloud
35
     * @param string $dir
36
     * @param array $options
37
     */
38
    public function uploadDirectory(string $dir, array $options): void
39
    {
40
        $container = $options['container'];
41
42
        try {
43
            foreach (scandir($dir) as $filename) {
44
                $path = $dir . DIRECTORY_SEPARATOR . $filename;
45
46
                if (is_file($path)) {
47
                    $this->blobClient->createBlockBlob($container, $filename, fopen($path, "r"));
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

47
                    $this->blobClient->createBlockBlob($container, $filename, /** @scrutinizer ignore-type */ fopen($path, "r"));
Loading history...
48
                }
49
            }
50
        } catch (ServiceException $e) {
51
            throw new RuntimeException(sprintf("There was an error during uploading files:\n %s", $e->getMessage()), $e->getCode(), $e);
52
        }
53
    }
54
55
    /**
56
     * Download a file from a cloud
57
     * @param string $save_to
58
     * @param array $options
59
     */
60
    public function download(string $save_to, array $options): void
61
    {
62
        $container = $options['container'];
63
        $blob = $options['blob'];
64
65
        try {
66
            $getBlobResult = $this->blobClient->getBlob($container, $blob);
67
        } catch (ServiceException $e) {
68
            throw new RuntimeException(sprintf("There was an error during uploading files:\n %s", $e->getMessage()), $e->getCode(), $e);
69
        }
70
71
        file_put_contents($save_to, $getBlobResult->getContentStream());
72
    }
73
}