Passed
Pull Request — v1 (#1)
by
unknown
06:09
created

Embed::embedChat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * YouTube Live Embed plugin for Craft CMS 3.x
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 nystudio107\youtubeliveembed\YoutubeLiveEmbed;
14
use nystudio107\youtubeliveembed\helpers\PluginTemplate;
15
16
use Craft;
17
use craft\base\Component;
18
use craft\helpers\UrlHelper;
19
20
/** @noinspection MissingPropertyAnnotationsInspection */
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...
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @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...
25
 * @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...
26
 */
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...
27
class Embed extends Component
28
{
29
    // Constants
30
    // =========================================================================
31
32
    const YOUTUBE_STREAM_URL = 'https://www.youtube.com/embed/live_stream';
33
    const YOUTUBE_CHAT_URL = 'https://www.youtube.com/live_chat';
34
35
    // Public Methods
36
    // =========================================================================
37
38
    /**
39
     * Renders the responsive iframe for the live stream video
40
     *
41
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
42
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     *
44
     * @return \Twig_Markup
45
     */
46
    public function embedStream(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup
47
    {
48
        $html = PluginTemplate::renderPluginTemplate(
49
            'embeds/youtube-live-stream.twig',
50
            [
51
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
52
                'iframeUrl' => $this->getYoutubeStreamUrl(),
53
            ]
54
        );
55
56
        return $html;
57
    }
58
59
    /**
60
     * Renders the responsive Google AMP iframe for the live stream video
61
     *
62
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     *
65
     * @return \Twig_Markup
66
     */
67
    public function embedStreamAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup
68
    {
69
        $html = PluginTemplate::renderPluginTemplate(
70
            'embeds/youtube-live-stream-amp.twig',
71
            [
72
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
73
                'iframeUrl' => $this->getYoutubeStreamUrl(),
74
            ]
75
        );
76
77
        return $html;
78
    }
79
80
    /**
81
     * Renders the responsive iframe HTML for the live stream chat
82
     *
83
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     *
86
     * @return \Twig_Markup
87
     */
88
    public function embedChat(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup
89
    {
90
        $html = PluginTemplate::renderPluginTemplate(
91
            'embeds/youtube-live-chat.twig',
92
            [
93
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
94
                'iframeUrl' => $this->getYoutubeChatUrl(),
95
            ]
96
        );
97
98
        return $html;
99
    }
100
101
    /**
102
     * Renders the responsive Google AMP iframe HTML for the live stream chat
103
     *
104
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     *
107
     * @return \Twig_Markup
108
     */
109
    public function embedChatAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup
110
    {
111
        $html = PluginTemplate::renderPluginTemplate(
112
            'embeds/youtube-live-chat-amp.twig',
113
            [
114
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
115
                'iframeUrl' => $this->getYoutubeChatUrl(),
116
            ]
117
        );
118
119
        return $html;
120
    }
121
122
    /**
123
     * Sets the YouTube Channel ID to $channelId
124
     *
125
     * @param string $channelId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
126
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127
    public function setChannelId(string $channelId)
128
    {
129
        YoutubeLiveEmbed::$youtubeChannelId = $channelId;
130
    }
131
132
    /**
133
     * Returns whether the stream is currently live
134
     *
135
     * @return bool
136
     */
137
    public function isLive(): bool
138
    {
139
        $videoId = $this->getVideoIdFromLiveStream();
140
        if ($videoId) {
141
            return true;
142
        }
143
144
        return false;
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
161
        return $url;
162
    }
163
164
    /**
165
     * Returns the URL to the live chat YouTube page
166
     *
167
     * @return string
168
     */
169
    protected function getYoutubeChatUrl(): string
170
    {
171
        $url = '';
172
        $videoId = $this->getVideoIdFromLiveStream();
173
        if ($videoId) {
174
            $site = Craft::$app->getSites()->currentSite;
175
            $domain = parse_url($site->baseUrl, PHP_URL_HOST);
176
            $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...
177
                'v' => $this->getVideoIdFromLiveStream(),
178
                'embed_domain' => $domain
179
            ]);
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...
180
        }
181
182
        return $url;
183
    }
184
185
    /**
186
     * Extracts the Video ID of the current live stream video
187
     *
188
     * @return null|string
189
     */
190
    protected function getVideoIdFromLiveStream()
191
    {
192
        $videoId = null;
193
        $liveUrl = $this->getYoutubeStreamUrl();
194
        // Fetch the livestream page
195
        if ($data = @file_get_contents($liveUrl)) {
196
            // Find the video ID in there
197
            if (preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches)) {
198
                $videoId = $matches[1];
199
            }
200
        }
201
202
        return $videoId;
203
    }
204
}
205