MediaConverterManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 1
b 0
f 0
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A engine() 0 3 1
A getCredentials() 0 3 1
A getDefaultDriver() 0 3 1
A setMediaConvertClient() 0 6 1
A createMediaConvertDriver() 0 11 1
A ensureAwsSdkIsInstalled() 0 4 2
1
<?php
2
3
namespace Meema\MediaConverter;
4
5
use Aws\Credentials\Credentials;
6
use Aws\MediaConvert\MediaConvertClient;
7
use Exception;
8
use Illuminate\Support\Manager;
9
use Meema\MediaConverter\Converters\MediaConvert;
10
11
class MediaConverterManager extends Manager
12
{
13
    /**
14
     * Get a driver instance.
15
     *
16
     * @param  string|null  $name
17
     * @return mixed
18
     */
19
    public function engine($name = null)
20
    {
21
        return $this->driver($name);
22
    }
23
24
    /**
25
     * Create an Amazon MediaConvert Converter instance.
26
     *
27
     * @return \Meema\MediaConverter\Converters\MediaConvert
28
     *
29
     * @throws \Exception
30
     */
31
    public function createMediaConvertDriver(): MediaConvert
32
    {
33
        $this->ensureAwsSdkIsInstalled();
34
35
        $config = $this->config['media-converter'];
36
37
        $credentials = $this->getCredentials($config['credentials']);
38
39
        $client = $this->setMediaConvertClient($config, $credentials);
40
41
        return new MediaConvert($client);
42
    }
43
44
    /**
45
     * Sets the MediaConvert client.
46
     *
47
     * @param  array  $config
48
     * @param  Credentials  $credentials
49
     * @return \Aws\MediaConvert\MediaConvertClient
50
     */
51
    protected function setMediaConvertClient(array $config, Credentials $credentials): MediaConvertClient
52
    {
53
        return new MediaConvertClient([
54
            'version' => $config['version'],
55
            'region' => $config['region'],
56
            'credentials' => $credentials,
57
        ]);
58
    }
59
60
    /**
61
     * Get credentials of AWS.
62
     *
63
     * @param  array  $credentials
64
     * @return \Aws\Credentials\Credentials
65
     */
66
    protected function getCredentials(array $credentials): Credentials
67
    {
68
        return new Credentials($credentials['key'], $credentials['secret']);
69
    }
70
71
    /**
72
     * Ensure the AWS SDK is installed.
73
     *
74
     * @return void
75
     *
76
     * @throws \Exception
77
     */
78
    protected function ensureAwsSdkIsInstalled()
79
    {
80
        if (! class_exists(MediaConvertClient::class)) {
81
            throw new Exception('Please install the AWS SDK PHP using `composer require aws/aws-sdk-php`.');
82
        }
83
    }
84
85
    /**
86
     * Get the default media conversion driver name.
87
     *
88
     * @return string
89
     */
90
    public function getDefaultDriver(): string
91
    {
92
        return 'media-convert';
93
    }
94
}
95