Completed
Push — master ( 97dbf8...909da3 )
by Gino
02:47
created

TtsResponse::fillFilePathParts()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 8
nop 3
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace GinoPane\PHPolyglot\API\Response\TTS;
4
5
use GinoPane\PHPolyglot\Exception\InvalidIoException;
6
use GinoPane\PHPolyglot\API\Factory\TTS\TtsApiFactory;
7
use GinoPane\PHPolyglot\Exception\InvalidPathException;
8
use GinoPane\PHPolyglot\API\Response\ApiResponseAbstract;
9
use GinoPane\PHPolyglot\API\Supplemental\TTS\TtsAudioFormat;
10
11
/**
12
 * Class TtsResponse
13
 *
14
 * @author Sergey <Gino Pane> Karavay
15
 */
16
class TtsResponse extends ApiResponseAbstract
17
{
18
    /**
19
     * Response audio format
20
     *
21
     * @var TtsAudioFormat
22
     */
23
    private $format = null;
24
25
    /**
26
     * The text that was used for generation
27
     *
28
     * @var string
29
     */
30
    private $text = '';
31
32
    /**
33
     * TtsResponse constructor
34
     *
35
     * @param string         $content
36
     * @param TtsAudioFormat $format
37
     * @param string         $text
38
     */
39
    public function __construct(string $content, TtsAudioFormat $format, string $text)
40
    {
41
        $this->format = $format;
42
43
        $this->text = $text;
44
45
        $this->setData($content);
46
    }
47
48
    /**
49
     * Stores TTS data into specified file.
50
     * If no $fileName is specified, md5 of source text is used.
51
     * If no $extension is specified, the default extension for audio format is used.
52
     * If no $directory is specified, the default directory from the config is used.
53
     *
54
     * @param string $fileName Target file name without extension
55
     * @param string $extension Target file extension
56
     * @param string $directory Target directory
57
     *
58
     * @throws InvalidIoException
59
     * @throws InvalidPathException
60
     *
61
     * @return string
62
     */
63
    public function storeFile(string $fileName = '', string $extension = '', string $directory = ''): string
64
    {
65
        list($fileName, $fullFileName, $directory) = $this->fillFilePathParts($fileName, $extension, $directory);
66
67
        if (!$this->filePutContents($directory . DIRECTORY_SEPARATOR . $fullFileName, $this->getData())) {
68
            throw new InvalidIoException(
69
                sprintf(
70
                    'Failed to write the file "%s" to the directory "%s"',
71
                    $fileName, //@codeCoverageIgnore
72
                    $directory
73
                )
74
            );
75
        }
76
77
        return $fullFileName;
78
    }
79
80
    /**
81
     * @param string $fileName
82
     * @param string $data
83
     *
84
     * @return bool
85
     */
86
    protected function filePutContents(string $fileName, string $data): bool
87
    {
88
        return (bool)@file_put_contents($fileName, $data);
89
    }
90
91
    /**
92
     * @codeCoverageIgnore
93
     *
94
     * @return TtsApiFactory
95
     */
96
    protected function getTtsApiFactory(): TtsApiFactory
97
    {
98
        return new TtsApiFactory();
99
    }
100
101
    /**
102
     * @param string $text
103
     *
104
     * @return string
105
     */
106
    private function generateFilename(string $text): string
107
    {
108
        return md5($text);
109
    }
110
111
    /**
112
     * @param string $fileName
113
     * @param string $extension
114
     * @param string $directory
115
     *
116
     * @throws InvalidPathException
117
     * @return array
118
     */
119
    private function fillFilePathParts(string $fileName, string $extension, string $directory): array
120
    {
121
        $fileName = $fileName ? $fileName : $this->generateFilename($this->text);
122
123
        $fullFileName = $extension
124
            ? "$fileName.$extension"
125
            : sprintf("%s.%s", $fileName, $this->format->getFileExtension());
126
127
        $directory = $directory ? $directory : $this->getTtsApiFactory()->getTargetDirectory();
128
129
        return array($fileName, $fullFileName, $directory);
130
    }
131
}
132