VideoService::fetchLatest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Article\Service;
6
7
use Admin\Mapper\AdminUsersMapper;
8
use Article\Entity\ArticleType;
9
use Article\Filter\ArticleFilter;
10
use Article\Filter\VideoFilter;
11
use Article\Mapper\ArticleMapper;
12
use Article\Mapper\ArticleVideosMapper;
13
use Category\Mapper\CategoryMapper;
14
use MysqlUuid\Formats\Binary;
15
use MysqlUuid\Uuid as MysqlUuid;
16
use Ramsey\Uuid\Uuid;
17
use Std\FilterException;
18
use UploadHelper\Upload;
19
20
class VideoService extends ArticleService
21
{
22
    /**
23
     * @var ArticleMapper
24
     */
25
    private $articleMapper;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    /**
28
     * @var ArticleVideosMapper
29
     */
30
    private $articleVideosMapper;
31
32
    /**
33
     * @var ArticleFilter
34
     */
35
    private $articleFilter;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
36
37
    /**
38
     * @var VideoFilter
39
     */
40
    private $videosFilter;
41
42
    /**
43
     * @var CategoryMapper
44
     */
45
    private $categoryMapper;
46
47
    /**
48
     * @var Upload
49
     */
50
    private $upload;
51
52
    /**
53
     * @var AdminUsersMapper
54
     */
55
    private $adminUsersMapper;
56
57
    /**
58
     * VideosService constructor.
59
     *
60
     * @param ArticleMapper       $articleMapper
61
     * @param ArticleVideosMapper $articleVideosMapper
62
     * @param ArticleFilter       $articleFilter
63
     * @param VideoFilter         $videosFilter
64
     * @param CategoryMapper      $categoryMapper
65
     * @param Upload              $upload
66
     * @param AdminUsersMapper    $adminUsersMapper
67
     */
68 View Code Duplication
    public function __construct(
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...
69
        ArticleMapper $articleMapper,
70
        ArticleVideosMapper $articleVideosMapper,
71
        ArticleFilter $articleFilter,
72
        VideoFilter $videosFilter,
73
        CategoryMapper $categoryMapper,
74
        Upload $upload,
75
        AdminUsersMapper $adminUsersMapper
76
    ) {
77
        parent::__construct($articleMapper, $articleFilter);
78
79
        $this->articleMapper = $articleMapper;
80
        $this->articleVideosMapper = $articleVideosMapper;
81
        $this->articleFilter = $articleFilter;
82
        $this->videosFilter = $videosFilter;
83
        $this->categoryMapper = $categoryMapper;
84
        $this->upload = $upload;
85
        $this->adminUsersMapper = $adminUsersMapper;
86
    }
87
88
    public function fetchAllArticles($page, $limit)
89
    {
90
        $select = $this->articleVideosMapper->getPaginationSelect();
91
92
        return $this->getPagination($select, $page, $limit);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPaginat...select, $page, $limit); (Zend\Paginator\Paginator) is incompatible with the return type declared by the interface Article\Service\ArticleS...rface::fetchAllArticles of type ArrayObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93
    }
94
95
    public function fetchWebArticles($page = 1, $limit = 10)
96
    {
97
        $select = $this->articleVideosMapper->getPaginationSelect(true);
98
99
        return $this->getPagination($select, $page, $limit);
100
    }
101
102
    public function fetchLatest($limit)
103
    {
104
        return $this->articleVideosMapper->getLatest($limit);
105
    }
106
107
    public function fetchVideoBySlug($slug)
108
    {
109
        return $this->articleVideosMapper->getBySlug($slug);
110
    }
111
112
    public function fetchSingleArticle($articleId)
113
    {
114
        return $this->articleVideosMapper->get($articleId);
115
    }
116
117 View Code Duplication
    public function createArticle($user, $data)
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...
118
    {
119
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
120
        $videosFilter = $this->videosFilter->getInputFilter()->setData($data);
121
122
        if (!$articleFilter->isValid() || !$videosFilter->isValid()) {
123
            throw new FilterException($articleFilter->getMessages() + $videosFilter->getMessages());
124
        }
125
126
        $id = Uuid::uuid1()->toString();
127
        $uuId = (new MysqlUuid($id))->toFormat(new Binary());
128
129
        $article = $articleFilter->getValues();
130
        $article += [
131
            'admin_user_uuid' => $this->adminUsersMapper->getUuid($article['admin_user_id']),
132
            'type'            => ArticleType::VIDEO,
133
            'article_id'      => $id,
134
            'article_uuid'    => $uuId,
135
        ];
136
137
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
138
        unset($article['category_id'], $article['admin_user_id']);
139
140
        $videos = $videosFilter->getValues() + [
141
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
142
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
143
                'article_uuid' => $uuId,
144
            ];
145
146
        $this->articleMapper->insert($article);
147
        $this->articleVideosMapper->insert($videos);
148
    }
149
150 View Code Duplication
    public function updateArticle($data, $id)
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...
151
    {
152
        $oldArticle = $this->articleVideosMapper->get($id);
153
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
154
        $videosFilter = $this->videosFilter->getInputFilter()->setData($data);
155
156
        if (!$articleFilter->isValid() || !$videosFilter->isValid()) {
157
            throw new FilterException($articleFilter->getMessages() + $videosFilter->getMessages());
158
        }
159
160
        $article = $articleFilter->getValues() + ['article_uuid' => $oldArticle->article_uuid];
161
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
162
        $article['admin_user_uuid'] = $this->adminUsersMapper->getUuid($article['admin_user_id']);
163
        unset($article['category_id']);
164
        unset($article['admin_user_id']);
165
166
        $videos = $videosFilter->getValues() + [
167
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
168
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
169
            ];
170
171
        // We don't want to force user to re-upload image on edit
172
        if (!$videos['featured_img']) {
173
            unset($videos['featured_img']);
174
        } else {
175
            $this->upload->deleteFile($oldArticle->featured_img);
176
        }
177
178
        if (!$videos['main_img']) {
179
            unset($videos['main_img']);
180
        } else {
181
            $this->upload->deleteFile($oldArticle->main_img);
182
        }
183
184
        $this->articleMapper->update($article, ['article_uuid' => $article['article_uuid']]);
185
        $this->articleVideosMapper->update($videos, ['article_uuid' => $article['article_uuid']]);
186
    }
187
188 View Code Duplication
    public function deleteArticle($id)
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...
189
    {
190
        $video = $this->articleVideosMapper->get($id);
191
192
        if (!$video) {
193
            throw new \Exception('Article not found!');
194
        }
195
196
        $this->upload->deleteFile($video->main_img);
197
        $this->upload->deleteFile($video->featured_img);
198
        $this->articleVideosMapper->delete(['article_uuid' => $video->article_uuid]);
199
        $this->delete($video->article_uuid);
200
    }
201
}
202