Completed
Push — master ( 907d49...2bf803 )
by Rigel Kent
04:29
created

Storable::hasExtension()   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\Traits;
4
5
use Illuminate\Support\Facades\Storage;
6
7
trait Storable
8
{
9
    /**
10
     * Determines where to save the converted file.
11
     *
12
     * @var string
13
     */
14
    protected $disk;
15
16
    /**
17
     * Determines the path where to save the converted file.
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
    /**
24
     * Set where to store the converted file.
25
     *
26
     * @param string $disk
27
     * @return $this
28
     */
29
    public function disk(string $disk)
30
    {
31
        $this->disk = $disk;
32
33
        return $this;
34
    }
35
36
    /**
37
     * Set path to where to store the converted file.
38
     *
39
     * @param string $path
40
     * @return $this
41
     */
42
    public function saveTo(string $path)
43
    {
44
        $this->path = $path;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Execute the store.
51
     *
52
     * @param mixed $resultContent
53
     * @return string
54
     */
55
    protected function store($resultContent)
56
    {
57
        $this->ensurePathIsNotNull();
58
59
        $storage = Storage::disk($this->disk ?: config('tts.disk'));
60
61
        $storage->put($this->path, $resultContent);
62
63
        return $this->path;
64
    }
65
66
    /**
67
     * Ensures the path not to be null if it is null it will set a default path.
68
     *
69
     * @return void
70
     */
71
    protected function ensurePathIsNotNull()
72
    {
73
        $filename = $this->path ?: 'TTS/'.now()->timestamp;
74
75
        if (!$this->hasExtension($filename)) {
76
            $filename .= '.'.$this->getExtension();
77
        }
78
79
        $this->path = $filename;
80
    }
81
82
    /**
83
     * Determine if filename includes file extension.
84
     *
85
     * @param  string  $filename
86
     * @return boolean
87
     */
88
    protected function hasExtension($filename)
89
    {
90
        return (bool) pathinfo($filename, PATHINFO_EXTENSION);
91
    }
92
93
    /**
94
     * Get audio file extension.
95
     *
96
     * @return string
97
     */
98
    protected function getExtension()
99
    {
100
        return config('tts.output_format');
101
    }
102
}
103