Passed
Push — develop ( 486cb4...479909 )
by Benjamin
05:35
created

VideosHelper::getDuration8601()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9332
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) 2020, Dukt
5
 * @license   https://github.com/dukt/videos/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\videos\helpers;
9
10
use Craft;
11
use craft\helpers\FileHelper;
12
use dukt\videos\Plugin;
13
14
/**
15
 * Videos helper
16
 */
17
class VideosHelper
18
{
19
    // Public Methods
20
    // =========================================================================
21
22
    /**
23
     * Formats seconds to hh:mm:ss.
24
     *
25
     * @param $seconds
26
     *
27
     * @return string
28
     */
29
    public static function getDuration($seconds): string
30
    {
31
        $hours = (int)((int)$seconds / 3600);
32
        $minutes = (($seconds / 60) % 60);
33
        $seconds %= 60;
34
35
        $hms = '';
36
37
        if ($hours > 0) {
38
            $hms .= str_pad($hours, 2, '0', STR_PAD_LEFT).':';
39
        }
40
41
        $hms .= str_pad($minutes, 2, '0', STR_PAD_LEFT).':';
42
43
        $hms .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
44
45
        return $hms;
46
    }
47
48
    /**
49
     * Formats seconds to ISO 8601 duration
50
     *
51
     * @param $seconds
52
     *
53
     * @return string
54
     */
55
    public static function getDuration8601($seconds): string
56
    {
57
        $hours = (int)((int)$seconds / 3600);
58
        $minutes = (($seconds / 60) % 60);
59
        $seconds %= 60;
60
61
        $iso8601 = 'PT';
62
63
        if ($hours > 0) {
64
            $iso8601 .= "{$hours}H";
65
        }
66
67
        if ($minutes > 0) {
68
            $iso8601 .= "{$minutes}M";
69
        }
70
71
        $iso8601 .= "{$seconds}S";
72
73
        return $iso8601;
74
    }
75
76
    /**
77
     * Returns a video thumbnail’s published URL.
78
     *
79
     * @param $gatewayHandle
80
     * @param $videoId
81
     * @param $size
82
     *
83
     * @return null|string
84
     * @throws \GuzzleHttp\Exception\GuzzleException
85
     * @throws \craft\errors\ImageException
86
     * @throws \yii\base\Exception
87
     * @throws \yii\base\InvalidConfigException
88
     */
89
    public static function getVideoThumbnail($gatewayHandle, $videoId, $size)
90
    {
91
        $baseDir = Craft::$app->getPath()->getRuntimePath().DIRECTORY_SEPARATOR.'videos'.DIRECTORY_SEPARATOR.'thumbnails'.DIRECTORY_SEPARATOR.$gatewayHandle.DIRECTORY_SEPARATOR.$videoId;
92
        $originalDir = $baseDir.DIRECTORY_SEPARATOR.'original';
93
        $dir = $baseDir.DIRECTORY_SEPARATOR.$size;
94
95
        $file = self::getThumbnailFile($dir);
96
97
        if (!$file) {
98
            // Retrieve original image
99
            $originalPath = null;
100
101
            if (is_dir($originalDir)) {
102
                $originalFiles = FileHelper::findFiles($originalDir);
103
104
                if (\count($originalFiles) > 0) {
105
                    $originalPath = $originalFiles[0];
106
                }
107
            }
108
109
            if (!$originalPath) {
110
                $video = Plugin::$plugin->getVideos()->getVideoById($gatewayHandle, $videoId);
111
                $url = $video->thumbnailSource;
112
113
                $name = pathinfo($url, PATHINFO_BASENAME);
114
                $originalPath = $originalDir.DIRECTORY_SEPARATOR.$name;
115
116
                FileHelper::createDirectory($originalDir);
117
                $client = new \GuzzleHttp\Client();
118
                $response = $client->request('GET', $url, [
119
                    'save_to' => $originalPath,
120
                ]);
121
122
                if ($response->getStatusCode() !== 200) {
123
                    return null;
124
                }
125
            } else {
126
                $name = pathinfo($originalPath, PATHINFO_BASENAME);
127
            }
128
129
            // Generate the thumb
130
            $path = $dir.DIRECTORY_SEPARATOR.$name;
131
            FileHelper::createDirectory($dir);
132
            Craft::$app->getImages()->loadImage($originalPath, false, $size)
133
                ->scaleToFit($size, $size)
134
                ->saveAs(parse_url($path, PHP_URL_PATH));
135
        } else {
136
            $name = pathinfo($file, PATHINFO_BASENAME);
137
        }
138
139
        return Craft::$app->getAssetManager()->getPublishedUrl($dir, true)."/{$name}";
0 ignored issues
show
Bug introduced by
Are you sure Craft::app->getAssetMana...ublishedUrl($dir, true) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        return /** @scrutinizer ignore-type */ Craft::$app->getAssetManager()->getPublishedUrl($dir, true)."/{$name}";
Loading history...
140
    }
141
142
    // Private Methods
143
    // =========================================================================
144
145
    /**
146
     * Get thumbnail file.
147
     *
148
     * @param $dir
149
     *
150
     * @return null|string
151
     */
152
    private static function getThumbnailFile($dir)
153
    {
154
        if (!is_dir($dir)) {
155
            return null;
156
        }
157
158
        $files = FileHelper::findFiles($dir);
159
160
        if (\count($files) === 0) {
161
            return null;
162
        }
163
164
        return $files[0];
165
    }
166
}
167