MediaRecognitionManager::getDefaultDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Meema\MediaRecognition;
4
5
use Aws\Credentials\Credentials;
6
use Aws\Rekognition\RekognitionClient;
7
use Exception;
8
use Illuminate\Support\Manager;
9
use Meema\MediaRecognition\Recognizers\Rekognition;
10
11
class MediaRecognitionManager 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 MediaRecognition Converter instance.
26
     *
27
     * @return \Meema\MediaRecognition\Recognizers\Rekognition
28
     * @throws \Exception
29
     */
30
    public function createMediaRecognitionDriver(): Rekognition
31
    {
32
        $this->ensureAwsSdkIsInstalled();
33
34
        $config = $this->config['media-recognition'];
35
36
        $credentials = $this->getCredentials($config['credentials']);
37
38
        $client = $this->setMediaRecognitionClient($config, $credentials);
39
40
        return new Rekognition($client);
41
    }
42
43
    /**
44
     * Sets the Recognition client.
45
     *
46
     * @param array $config
47
     * @param \Aws\Credentials\Credentials $credentials
48
     * @return \Aws\Rekognition\RekognitionClient
49
     */
50
    protected function setMediaRecognitionClient(array $config, Credentials $credentials): RekognitionClient
51
    {
52
        return new RekognitionClient([
53
            'version' => $config['version'],
54
            'region' => $config['region'],
55
            'credentials' => $credentials,
56
        ]);
57
    }
58
59
    /**
60
     * Get credentials of AWS.
61
     *
62
     * @param array $credentials
63
     * @return \Aws\Credentials\Credentials
64
     */
65
    protected function getCredentials(array $credentials): Credentials
66
    {
67
        return new Credentials($credentials['key'], $credentials['secret']);
68
    }
69
70
    /**
71
     * Ensure the AWS SDK is installed.
72
     *
73
     * @return void
74
     *
75
     * @throws \Exception
76
     */
77
    protected function ensureAwsSdkIsInstalled()
78
    {
79
        if (! class_exists(RekognitionClient::class)) {
80
            throw new Exception('Please install the AWS SDK PHP using `composer require aws/aws-sdk-php`.');
81
        }
82
    }
83
84
    /**
85
     * Get the default media recognition driver name.
86
     *
87
     * @return string
88
     */
89
    public function getDefaultDriver(): string
90
    {
91
        return 'media-recognition';
92
    }
93
}
94