Passed
Push — develop ( b870d7...5b9e10 )
by
unknown
04:27 queued 11s
created

Embed::getSiteDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
rs 10
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
                'embedDomain' => $this->getSiteDomain(),
96
            ]
97
        );
98
99
        return $html;
100
    }
101
102
    /**
103
     * Renders the responsive Google AMP iframe HTML for the live stream chat
104
     *
105
     * @param int $aspectRatioX
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @param int $aspectRatioY
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     *
108
     * @return \Twig_Markup
109
     */
110
    public function embedChatAmp(int $aspectRatioX = 16, int $aspectRatioY = 9): \Twig_Markup
111
    {
112
        $html = PluginTemplate::renderPluginTemplate(
113
            'embeds/youtube-live-chat-amp.twig',
114
            [
115
                'aspectRatio' => ($aspectRatioY / $aspectRatioX) * 100,
116
                'iframeUrl' => $this->getYoutubeChatUrl(),
117
                'embedDomain' => $this->getSiteDomain(),
118
            ]
119
        );
120
121
        return $html;
122
    }
123
124
    /**
125
     * Sets the YouTube Channel ID to $channelId
126
     *
127
     * @param string $channelId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129
    public function setChannelId(string $channelId)
130
    {
131
        YoutubeLiveEmbed::$youtubeChannelId = $channelId;
132
    }
133
134
    /**
135
     * Returns whether the stream is currently live
136
     *
137
     * @return bool
138
     */
139
    public function isLive(): bool
140
    {
141
        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...
142
    }
143
144
    // Protected Methods
145
    // =========================================================================
146
147
    /**
148
     * Returns the URL to the live video YouTube page
149
     *
150
     * @return string
151
     */
152
    protected function getYoutubeStreamUrl(): string
153
    {
154
        $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...
155
            'channel' => YoutubeLiveEmbed::$youtubeChannelId,
156
        ]);
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...
157
        return $url;
158
    }
159
160
    /**
161
     * Returns the URL to the live chat YouTube page
162
     *
163
     * @return string
164
     */
165
    protected function getYoutubeChatUrl(): string
166
    {
167
        $url = '';
168
        $videoId = $this->getVideoIdFromLiveStream();
169
        if ($videoId) {
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' => $this->getSiteDomain(),
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
    /**
181
     * Returns the domain of the host site
182
     *
183
     * @return string
184
     */
185
    protected function getSiteDomain()
186
    {
187
        $site = Craft::$app->getSites()->currentSite;
188
        $domain = parse_url($site->baseUrl, PHP_URL_HOST);
189
        return $domain;
190
    }
191
192
    /**
193
     * Extracts the Video ID of the current live stream video
194
     *
195
     * @return null|string
196
     */
197
    protected function getVideoIdFromLiveStream()
198
    {
199
        $videoId = null;
200
        $liveUrl = $this->getYoutubeStreamUrl();
201
        // Fetch the livestream page
202
        if ($data = @file_get_contents($liveUrl)) {
203
            // Find the video ID in there
204
            if (preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches)) {
205
                $videoId = $matches[1];
206
            }
207
        }
208
209
        return $videoId;
210
    }
211
}
212