Embed::getYoutubeChatUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * YouTube Live Embed plugin for Craft CMS
4
 *
5
 * This plugin allows you to embed a YouTube live stream and/or live chat on your webpage
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2019 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\youtubeliveembed\services;
12
13
use Craft;
14
use craft\base\Component;
15
use craft\helpers\UrlHelper;
16
use nystudio107\youtubeliveembed\helpers\PluginTemplate;
17
use nystudio107\youtubeliveembed\models\Settings;
18
use nystudio107\youtubeliveembed\YoutubeLiveEmbed;
19
use Twig\Markup;
20
21
/** @noinspection MissingPropertyAnnotationsInspection */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
25
 * @package   YoutubeLiveEmbed
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
27
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
28
class Embed extends Component
29
{
30
    // Constants
31
    // =========================================================================
32
33
    const YOUTUBE_STREAM_URL = 'https://www.youtube.com/embed/live_stream';
34
    const YOUTUBE_CHAT_URL = 'https://www.youtube.com/live_chat';
35
36
    // Public Methods
37
    // =========================================================================
38
39
    /**
40
     * Renders the responsive iframe for the live stream video
41
     *
42
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     *
45
     * @return Markup
46
     */
47
    public function embedStream(int $aspectRatioX = 16, int $aspectRatioY = 9): Markup
48
    {
49
        $html = PluginTemplate::renderPluginTemplate(
50
            'embeds/youtube-live-stream.twig',
51
            [
52
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
53
                'iframeUrl' => $this->getYoutubeStreamUrl(),
54
            ]
55
        );
56
57
        return $html;
58
    }
59
60
    /**
61
     * Renders the responsive Google AMP iframe for the live stream video
62
     *
63
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
65
     *
66
     * @return Markup
67
     */
68
    public function embedStreamAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): Markup
69
    {
70
        $html = PluginTemplate::renderPluginTemplate(
71
            'embeds/youtube-live-stream-amp.twig',
72
            [
73
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
74
                'iframeUrl' => $this->getYoutubeStreamUrl(),
75
            ]
76
        );
77
78
        return $html;
79
    }
80
81
    /**
82
     * Renders the responsive iframe HTML for the live stream chat
83
     *
84
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     *
87
     * @return Markup
88
     */
89
    public function embedChat(int $aspectRatioX = 16, int $aspectRatioY = 9): Markup
90
    {
91
        $html = PluginTemplate::renderPluginTemplate(
92
            'embeds/youtube-live-chat.twig',
93
            [
94
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
95
                'iframeUrl' => $this->getYoutubeChatUrl(),
96
                'embedDomain' => $this->getSiteDomain(),
97
            ]
98
        );
99
100
        return $html;
101
    }
102
103
    /**
104
     * Renders the responsive Google AMP iframe HTML for the live stream chat
105
     *
106
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
     *
109
     * @return Markup
110
     */
111
    public function embedChatAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): Markup
112
    {
113
        $html = PluginTemplate::renderPluginTemplate(
114
            'embeds/youtube-live-chat-amp.twig',
115
            [
116
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
117
                'iframeUrl' => $this->getYoutubeChatUrl(),
118
                'embedDomain' => $this->getSiteDomain(),
119
            ]
120
        );
121
122
        return $html;
123
    }
124
125
    /**
126
     * Sets the YouTube Channel ID to $channelId
127
     *
128
     * @param string $channelId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
130
    public function setChannelId(string $channelId)
131
    {
132
        YoutubeLiveEmbed::$youtubeChannelId = $channelId;
133
    }
134
135
    /**
136
     * Returns whether the stream is currently live
137
     *
138
     * @return bool
139
     */
140
    public function isLive(): bool
141
    {
142
        /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
143
        $settings = YoutubeLiveEmbed::$plugin->getSettings();
144
        return $settings->isLive;
145
    }
146
147
    // Protected Methods
148
    // =========================================================================
149
150
    /**
151
     * Returns the URL to the live video YouTube page
152
     *
153
     * @return string
154
     */
155
    protected function getYoutubeStreamUrl(): string
156
    {
157
        $url = UrlHelper::urlWithParams(self::YOUTUBE_STREAM_URL, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
158
            'channel' => YoutubeLiveEmbed::$youtubeChannelId,
159
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
160
        return $url;
161
    }
162
163
    /**
164
     * Returns the URL to the live chat YouTube page
165
     *
166
     * @return string
167
     */
168
    protected function getYoutubeChatUrl(): string
169
    {
170
        $url = '';
171
        $videoId = $this->getVideoIdFromLiveStream();
172
        if ($videoId) {
173
            $url = UrlHelper::urlWithParams(self::YOUTUBE_CHAT_URL, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
174
                'v' => $this->getVideoIdFromLiveStream(),
175
                'embed_domain' => $this->getSiteDomain(),
176
            ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
177
        }
178
179
        return $url;
180
    }
181
182
183
    /**
184
     * Returns the domain of the host site
185
     *
186
     * @return string
187
     */
188
    protected function getSiteDomain()
189
    {
190
        $site = Craft::$app->getSites()->currentSite;
191
        $request = Craft::$app->getRequest();
192
        $domain = parse_url($site->getBaseUrl(), PHP_URL_HOST);
193
        return $domain ?? $request->getHostName();
194
    }
195
196
    /**
197
     * Extracts the Video ID of the current live stream video
198
     *
199
     * @return null|string
200
     */
201
    protected function getVideoIdFromLiveStream()
202
    {
203
        $videoId = null;
204
        $liveUrl = $this->getYoutubeStreamUrl();
205
        // Fetch the livestream page
206
        if ($data = @file_get_contents($liveUrl)) {
207
            // Find the video ID in there
208
            if (preg_match('/\"VIDEO_ID\":\"(.*?)\"/', $data, $matches)) {
209
                $videoId = $matches[1];
210
            }
211
        }
212
213
        return $videoId;
214
    }
215
}
216