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

Cloud::sendRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.9666
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 GuzzleHttp\Client;
17
use GuzzleHttp\Exception\GuzzleException;
18
use Streaming\Exception\RuntimeException;
19
20
class Cloud implements CloudInterface
21
{
22
    private $client;
23
    /**
24
     * @var string
25
     */
26
    private $url;
27
    /**
28
     * @var string
29
     */
30
    private $method;
31
    /**
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * @param string $url
38
     * @param string $method
39
     * @param array $options
40
     */
41
    public function __construct(string $url, string $method = "GET", $options = [])
42
    {
43
        $this->client = new Client();
44
        $this->url = $url;
45
        $this->method = $method;
46
        $this->options = $options;
47
    }
48
49
    /**
50
     * @param string $save_to
51
     * @param array $options
52
     */
53
    public function download(string $save_to, array $options = []): void
54
    {
55
        $this->sendRequest(array_merge($this->options, ['sink' => $save_to]));
56
    }
57
58
    /**
59
     * @param string $dir
60
     * @param array $options
61
     */
62
    public function uploadDirectory(string $dir, array $options): void
63
    {
64
        $multipart = [];
65
66
        $name = $options['name'];
67
        $headers = isset($options['headers']) ? $options['headers'] : [];
68
69
        foreach (scandir($dir) as $key => $filename) {
70
            $path = $dir . DIRECTORY_SEPARATOR . $filename;
71
72
            if (is_file($path)) {
73
                $multipart[$key]['name'] = $name;
74
                $multipart[$key]['contents'] = fopen($path, 'r');
75
                if (!empty($headers)) {
76
                    $multipart[$key]['headers'] = $headers;
77
                }
78
79
                $multipart[$key]['filename'] = $filename;
80
            }
81
        }
82
83
        $this->sendRequest(array_merge($this->options, ['multipart' => array_values($multipart)]));
84
    }
85
86
    /**
87
     * @param array $options
88
     * @throws RuntimeException
89
     */
90
    private function sendRequest(array $options): void
91
    {
92
        try {
93
            $this->client->request($this->method, $this->url, $options);
94
        } catch (GuzzleException $e) {
95
96
            $error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s",
97
                $this->url,
98
                $e->getCode(),
99
                $e->getMessage(),
100
                (method_exists($e->getResponse(), 'getBody')) ? $e->getResponse()->getBody()->getContents() : ""
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on GuzzleHttp\Exception\GuzzleException. It seems like you code against a sub-type of GuzzleHttp\Exception\GuzzleException such as GuzzleHttp\Exception\RequestException. ( Ignorable by Annotation )

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

100
                (method_exists($e->/** @scrutinizer ignore-call */ getResponse(), 'getBody')) ? $e->getResponse()->getBody()->getContents() : ""
Loading history...
101
            );
102
103
            throw new RuntimeException($error, $e->getCode(), $e);
104
        }
105
    }
106
}