Completed
Push — master ( ead051...fb750b )
by Rigel Kent
19:53 queued 13s
created

TextToSpeechManager::getCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Cion\TextToSpeech;
4
5
use Aws\Credentials\Credentials;
6
use Aws\Polly\PollyClient;
7
use Cion\TextToSpeech\Converters\NullConverter;
8
use Cion\TextToSpeech\Converters\PollyConverter;
9
use Exception;
10
use Illuminate\Support\Manager;
11
12
class TextToSpeechManager extends Manager
13
{
14
    /**
15
     * Get a driver instance.
16
     *
17
     * @param  string|null  $name
18
     * @return mixed
19
     */
20
    public function engine($name = null)
21
    {
22
        return $this->driver($name);
23
    }
24
25
    /**
26
     * Create an Amazon Polly Converter instance.
27
     *
28
     * @return \Cion\TextToSpeech\Converters\PollyConverter
29
     */
30
    public function createPollyDriver()
31
    {
32
        $this->ensureAwsSdkIsInstalled();
33
34
        $config = $this->config['tts.services.polly'];
35
36
        $credentials = $this->getCredentials($config['credentials']);
37
38
        $client = $this->setPollyClient($config, $credentials);
39
40
        return new PollyConverter(
41
           $client
42
       );
43
    }
44
45
    /**
46
     * Sets the polly client
47
     *
48
     * @param array $config
49
     * @param Credentials $credentials
50
     * @return PollyClient
51
     */
52
    protected function setPollyClient(array $config, Credentials $credentials)
53
    {
54
        return new PollyClient([
55
            'version'     => $config['version'],
56
            'region'      => $config['region'],
57
            'credentials' => $credentials,
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)
67
    {
68
        return new Credentials($credentials['key'], $credentials['secret']);
69
    }
70
71
    /**
72
     * Create a Null Converter instance.
73
     *
74
     * @return \Cion\TextToSpeech\Converters\NullConverter
75
     */
76
    public function createNullDriver()
77
    {
78
        return new NullConverter();
79
    }
80
81
    /**
82
     * Ensure the AWS SDK is installed.
83
     *
84
     * @return void
85
     *
86
     * @throws \Exception
87
     */
88
    protected function ensureAwsSdkIsInstalled()
89
    {
90
        if (! class_exists(PollyClient::class)) {
91
            throw new Exception(
92
                'Please install the AWS SDK PHP using `composer require aws/aws-sdk-php`.'
93
            );
94
        }
95
    }
96
97
    /**
98
     * Get the default Text to Speech driver name.
99
     *
100
     * @return string
101
     */
102
    public function getDefaultDriver()
103
    {
104
        $driver = $this->container['config']['tts.driver'];
105
106
        if (is_null($driver)) {
107
            return 'null';
108
        }
109
110
        return $driver;
111
    }
112
}
113