|
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; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ArticleVideosMapper |
|
29
|
|
|
*/ |
|
30
|
|
|
private $articleVideosMapper; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ArticleFilter |
|
34
|
|
|
*/ |
|
35
|
|
|
private $articleFilter; |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|