Issues (47)

src/services/Videos.php (4 issues)

1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/videos/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\videos\services;
9
10
use Craft;
11
use dukt\videos\models\Video;
12
use yii\base\Component;
13
use dukt\videos\Plugin as VideosPlugin;
14
15
/**
16
 * Class Videos service.
17
 *
18
 * An instance of the Videos service is globally accessible via [[Plugin::videos `Videos::$plugin->getVideos()`]].
19
 *
20
 * @author Dukt <[email protected]>
21
 * @since  2.0
22
 */
23
class Videos extends Component
24
{
25
    // Public Properties
26
    // =========================================================================
27
28
    /**
29
     * @var bool Whether the devServer should be used
30
     */
31
    public $useDevServer = false;
32
33
    /**
34
     * {@inheritdoc}
35
     * @var bool
36
     */
37
    public $pluginDevMode = false;
38
39
    // Public Methods
40
    // =========================================================================
41
42
    /**
43
     * Returns the HTML of the embed from a video URL.
44
     *
45
     * @param       $videoUrl
46
     * @param array $embedOptions
47
     *
48
     * @return null
49
     * @throws \yii\base\InvalidConfigException
50
     */
51
    public function getEmbed($videoUrl, array $embedOptions = []): ?\Twig_Markup
52
    {
53
        $video = $this->getVideoByUrl($videoUrl);
54
55
        if ($video !== null) {
56
            return $video->getEmbed($embedOptions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $video->getEmbed($embedOptions) returns the type Twig_Markup which is incompatible with the documented return type null.
Loading history...
57
        }
58
59
        return null;
60
    }
61
62
    /**
63
     * Get video by ID.
64
     *
65
     * @param $gateway
66
     * @param $id
67
     *
68
     * @return mixed|null|string
69
     * @throws \yii\base\InvalidConfigException
70
     */
71
    public function getVideoById($gateway, $id)
72
    {
73
        $video = $this->requestVideoById($gateway, $id);
74
75
        if ($video) {
76
            return $video;
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * Get video by URL.
84
     *
85
     * @param      $videoUrl
86
     * @param bool $enableCache
87
     * @param int  $cacheExpiry
88
     *
89
     * @return Video|null
90
     * @throws \yii\base\InvalidConfigException
91
     */
92
    public function getVideoByUrl($videoUrl, bool $enableCache = true, int $cacheExpiry = null)
93
    {
94
        $video = $this->requestVideoByUrl($videoUrl, $enableCache, $cacheExpiry);
95
96
        if ($video) {
97
            return $video;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $video also could return the type true which is incompatible with the documented return type dukt\videos\models\Video|null.
Loading history...
98
        }
99
100
        return null;
101
    }
102
103
    // Private Methods
104
    // =========================================================================
105
    /**
106
     * Request video by ID.
107
     *
108
     * @param      $gatewayHandle
109
     * @param      $id
110
     * @param bool $enableCache
111
     * @param int  $cacheExpiry
112
     *
113
     * @return \dukt\videos\models\Video|mixed
114
     * @throws \yii\base\InvalidConfigException
115
     */
116
    private function requestVideoById($gatewayHandle, $id, bool $enableCache = true, int $cacheExpiry = null)
117
    {
118
        $enableCache = VideosPlugin::$plugin->getSettings()->enableCache === false ? false : $enableCache;
119
120
        if ($enableCache) {
121
            $key = 'videos.video.' . $gatewayHandle . '.' . md5($id);
122
123
            $response = VideosPlugin::$plugin->getCache()->get([$key]);
124
125
            if ($response) {
126
                return $response;
127
            }
128
        }
129
130
        $gateway = VideosPlugin::$plugin->getGateways()->getGateway($gatewayHandle);
131
132
        $response = $gateway->getVideoById($id);
133
134
        if ($enableCache) {
135
            VideosPlugin::$plugin->getCache()->set([$key], $response, $cacheExpiry);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
It seems like $cacheExpiry can also be of type integer; however, parameter $expire of dukt\videos\services\Cache::set() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

135
            VideosPlugin::$plugin->getCache()->set([$key], $response, /** @scrutinizer ignore-type */ $cacheExpiry);
Loading history...
136
        }
137
138
        return $response;
139
    }
140
141
    /**
142
     * Request video by URL.
143
     *
144
     * @param      $videoUrl
145
     * @param bool $enableCache
146
     * @param int  $cacheExpiry
147
     *
148
     * @return bool|mixed
149
     * @throws \yii\base\InvalidConfigException
150
     */
151
    private function requestVideoByUrl($videoUrl, bool $enableCache = true, int $cacheExpiry = null)
152
    {
153
        $key = 'videos.video.' . md5($videoUrl);
154
        $enableCache = VideosPlugin::$plugin->getSettings()->enableCache === false ? false : $enableCache;
155
156
        if ($enableCache) {
157
            $response = VideosPlugin::$plugin->getCache()->get([$key]);
158
159
            if ($response) {
160
                return $response;
161
            }
162
        }
163
164
        return $this->findVideoByUrl($videoUrl, $enableCache, $key, $cacheExpiry);
165
    }
166
167
    /**
168
     * Find video by URL, by looping through all video gateways until a video if found.
169
     *
170
     * @param $videoUrl
171
     * @param $enableCache
172
     * @param $key
173
     * @param $cacheExpiry
174
     *
175
     * @return bool|mixed
176
     * @throws \yii\base\InvalidConfigException
177
     */
178
    private function findVideoByUrl($videoUrl, $enableCache, $key, $cacheExpiry)
179
    {
180
        foreach (VideosPlugin::$plugin->getGateways()->getGateways() as $gateway) {
181
            $params = [
182
                'url' => $videoUrl
183
            ];
184
185
            try {
186
                $video = $gateway->getVideoByUrl($params);
187
188
                if ($video) {
189
                    if ($enableCache) {
190
                        VideosPlugin::$plugin->getCache()->set([$key], $video, $cacheExpiry);
191
                    }
192
193
                    return $video;
194
                }
195
            } catch (\Exception $exception) {
196
                Craft::info('Couldn’t get video: ' . $exception->getMessage(), __METHOD__);
197
            }
198
        }
199
200
        return false;
201
    }
202
}
203