Passed
Push — develop ( 532116...e284ea )
by Andrew
06:11
created

Embed::liveViewers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Embed::getYoutubeStreamUrl() 0 6 1
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
        return YoutubeLiveEmbed::getInstance()->settings->isLive;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist on nystudio107\youtubeliveembed\YoutubeLiveEmbed. Since you implemented __get, consider adding a @property annotation.
Loading history...
140
    }
141
142
    // Protected Methods
143
    // =========================================================================
144
145
    /**
146
     * Returns the URL to the live video YouTube page
147
     *
148
     * @return string
149
     */
150
    protected function getYoutubeStreamUrl(): string
151
    {
152
        $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...
153
            'channel' => YoutubeLiveEmbed::$youtubeChannelId,
154
        ]);
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...
155
        return $url;
156
    }
157
158
    /**
159
     * Returns the URL to the live chat YouTube page
160
     *
161
     * @return string
162
     */
163
    protected function getYoutubeChatUrl(): string
164
    {
165
        $url = '';
166
        $videoId = $this->getVideoIdFromLiveStream();
167
        if ($videoId) {
168
            $site = Craft::$app->getSites()->currentSite;
169
            $domain = parse_url($site->baseUrl, PHP_URL_HOST);
170
            $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...
171
                'v' => $this->getVideoIdFromLiveStream(),
172
                'embed_domain' => $domain
173
            ]);
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...
174
        }
175
176
        return $url;
177
    }
178
179
    /**
180
     * Extracts the Video ID of the current live stream video
181
     *
182
     * @return null|string
183
     */
184
    protected function getVideoIdFromLiveStream()
185
    {
186
        $videoId = null;
187
        $liveUrl = $this->getYoutubeStreamUrl();
188
        // Fetch the livestream page
189
        if ($data = @file_get_contents($liveUrl)) {
190
            // Find the video ID in there
191
            if (preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches)) {
192
                $videoId = $matches[1];
193
            }
194
        }
195
196
        return $videoId;
197
    }
198
}
199