YouTubeService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 1
f 0
dl 0
loc 73
ccs 15
cts 26
cp 0.5769
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A searchVideosRelatedToSong() 0 10 3
A enabled() 0 3 1
A getEndpoint() 0 3 1
A search() 0 21 3
A getKey() 0 3 1
A getSecret() 0 3 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Song;
6
use Exception;
7
8
class YouTubeService extends AbstractApiClient implements ApiConsumerInterface
9
{
10
    /**
11
     * Determine if our application is using YouTube.
12
     */
13 132
    public function enabled(): bool
14
    {
15 132
        return (bool) $this->getKey();
16
    }
17
18
    /**
19
     * Search for YouTube videos related to a song.
20
     *
21
     * @return mixed|null
22
     */
23
    public function searchVideosRelatedToSong(Song $song, string $pageToken = '')
24
    {
25
        $q = $song->title;
26
27
        // If the artist is worth noticing, include them into the search.
28
        if (!$song->artist->is_unknown && !$song->artist->is_various) {
29
            $q .= " {$song->artist->name}";
30
        }
31
32
        return $this->search($q, $pageToken);
33
    }
34
35
    /**
36
     * Search for YouTube videos by a query string.
37
     *
38
     * @param string $q         The query string
39
     * @param string $pageToken YouTube page token (e.g. for next/previous page)
40
     * @param int    $perPage   Number of results per page
41
     *
42
     * @return mixed|null
43
     */
44 1
    public function search(string $q, string $pageToken = '', int $perPage = 10)
45
    {
46 1
        if (!$this->enabled()) {
47
            return null;
48
        }
49
50 1
        $uri = sprintf(
51 1
            'search?part=snippet&type=video&maxResults=%s&pageToken=%s&q=%s',
52 1
            $perPage,
53 1
            urlencode($pageToken),
54 1
            urlencode($q)
55
        );
56
57
        try {
58
            return $this->cache->remember(md5("youtube_$uri"), 60 * 24 * 7, function () use ($uri) {
59 1
                return $this->get($uri);
60 1
            });
61
        } catch (Exception $e) {
62
            $this->logger->error($e);
63
64
            return null;
65
        }
66
    }
67
68 1
    public function getEndpoint(): ?string
69
    {
70 1
        return config('koel.youtube.endpoint');
71
    }
72
73 132
    public function getKey(): ?string
74
    {
75 132
        return config('koel.youtube.key');
76
    }
77
78
    public function getSecret(): ?string
79
    {
80
        return null;
81
    }
82
}
83