VideoEndpoint::findAllBy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace Realshadow\Redtube\Endpoints;
4
5
use Realshadow\Redtube\Entities\ActiveVideo;
6
use Realshadow\Redtube\Entities\EmbedVideo;
7
use Realshadow\Redtube\Entities\Paginator;
8
use Realshadow\Redtube\Entities\Video;
9
use Realshadow\Redtube\Filters\VideoFilter;
10
11
12
/**
13
 * Video Endpoint
14
 *
15
 * @package Realshadow\Redtube\Endpoints
16
 * @author Lukáš Homza <[email protected]>
17
 */
18
class VideoEndpoint extends AbstractEndpoint
19
{
20
21
    /**
22
     * Deserialize API response into paginator object
23
     *
24
     * @param string $response
25
     * @param VideoFilter $filter
26
     *
27
     * @return Paginator
28
     */
29
    private function getPaginator($response, VideoFilter $filter)
30
    {
31
        /** @var Paginator $paginator */
32
        $paginator = $this->serializer->deserialize($response, Paginator::class, static::OUTPUT_FORMAT);
33
        $paginator->setPage($filter->getRequestedPage());
34
35
        return $paginator;
36
    }
37
38
    /**
39
     * Find videos by providing a video filter
40
     *
41
     * @param VideoFilter $filter
42
     *
43
     * @return Paginator
44
     * @throws \RuntimeException
45
     */
46
    public function findBy(VideoFilter $filter)
47
    {
48
        $response = $this->call('Videos.searchVideos', $filter);
49
50
        return $this->getPaginator($response, $filter);
51
    }
52
53
    /**
54
     * Allows to specify how many pages of videos should be called at once
55
     *
56
     * It will run in a loop calling page after page until the provided number of pages is met
57
     *
58
     * @param VideoFilter $filter
59
     * @param $pages
60
     *
61
     * @return \Generator
62
     * @throws \RuntimeException
63
     */
64 View Code Duplication
    public function findAllBy(VideoFilter $filter, $pages)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $currentPage = 1;
67
        while ($currentPage <= $pages) {
68
            $paginator = $this->findBy($filter);
69
            $paginator->setPage($currentPage);
70
71
            yield $paginator->items();
72
73
            $currentPage++;
74
        }
75
    }
76
77
    /**
78
     * Get detail of a video
79
     *
80
     * @param $id
81
     *
82
     * @return Video
83
     * @throws \RuntimeException
84
     */
85
    public function findById($id)
86
    {
87
        $response = $this->call('Videos.getVideoById&video_id=' . $id);
88
89
        /** @var Video $video */
90
        $video = $this->serializer->deserialize($response, Video::class, static::OUTPUT_FORMAT);
91
92
        return $video;
93
    }
94
95
    /**
96
     * Finds out if the video is valid
97
     *
98
     * @param $id
99
     *
100
     * @return bool
101
     * @throws \RuntimeException
102
     */
103
    public function isActive($id)
104
    {
105
        $response = $this->call('Videos.isVideoActive&video_id=' . $id);
106
107
        /** @var ActiveVideo $activeVideo */
108
        $activeVideo = $this->serializer->deserialize($response, ActiveVideo::class, static::OUTPUT_FORMAT);
109
110
        return $activeVideo->isActive();
111
    }
112
113
    /**
114
     * Get the embed code of a video
115
     *
116
     * @param $id
117
     *
118
     * @return string
119
     * @throws \RuntimeException
120
     */
121
    public function getEmbedCode($id)
122
    {
123
        $response = $this->call('Videos.getVideoEmbedCode&video_id=' . $id);
124
125
        /** @var EmbedVideo $embedVideo */
126
        $embedVideo = $this->serializer->deserialize($response, EmbedVideo::class, static::OUTPUT_FORMAT);
127
128
        return $embedVideo->getCode();
129
    }
130
131
    /**
132
     * Find all deleted videos by providing a video filter
133
     *
134
     * @param VideoFilter $filter
135
     *
136
     * @return Paginator
137
     * @throws \RuntimeException
138
     */
139
    public function findDeletedBy(VideoFilter $filter)
140
    {
141
        $response = $this->call('Videos.getDeletedVideos', $filter);
142
143
        return $this->getPaginator($response, $filter);
144
    }
145
146
    /**
147
     * Allows to specify how many pages of deleted videos should be called at once
148
     *
149
     * It will run in a loop calling page after page until the provided number of pages is met
150
     *
151
     * @param VideoFilter $filter
152
     * @param $pages
153
     *
154
     * @return \Generator
155
     * @throws \RuntimeException
156
     */
157 View Code Duplication
    public function findAllDeletedBy(VideoFilter $filter, $pages)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $currentPage = 1;
160
        while ($currentPage <= $pages) {
161
            $paginator = $this->findDeletedBy($filter);
162
            $paginator->setPage($currentPage);
163
164
            yield $paginator->items();
165
166
            $currentPage++;
167
        }
168
    }
169
170
}
171