TextToSpeechManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 100
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultDriver() 0 9 2
A createNullDriver() 0 3 1
A setPollyClient() 0 6 1
A engine() 0 3 1
A createPollyDriver() 0 12 1
A ensureAwsSdkIsInstalled() 0 5 2
A getCredentials() 0 3 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
    /**
62
     * Get credentials of AWS.
63
     *
64
     * @param array $credentials
65
     * @return \Aws\Credentials\Credentials
66
     */
67
    protected function getCredentials(array $credentials)
68
    {
69
        return new Credentials($credentials['key'], $credentials['secret']);
70
    }
71
72
    /**
73
     * Create a Null Converter instance.
74
     *
75
     * @return \Cion\TextToSpeech\Converters\NullConverter
76
     */
77
    public function createNullDriver()
78
    {
79
        return new NullConverter();
80
    }
81
82
    /**
83
     * Ensure the AWS SDK is installed.
84
     *
85
     * @return void
86
     *
87
     * @throws \Exception
88
     */
89
    protected function ensureAwsSdkIsInstalled()
90
    {
91
        if (! class_exists(PollyClient::class)) {
92
            throw new Exception(
93
                'Please install the AWS SDK PHP using `composer require aws/aws-sdk-php`.'
94
            );
95
        }
96
    }
97
98
    /**
99
     * Get the default Text to Speech driver name.
100
     *
101
     * @return string
102
     */
103
    public function getDefaultDriver()
104
    {
105
        $driver = $this->container['config']['tts.driver'];
106
107
        if (is_null($driver)) {
108
            return 'null';
109
        }
110
111
        return $driver;
112
    }
113
}
114