PostService::deleteArticle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
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\PostFilter;
11
use Article\Mapper\ArticleMapper;
12
use Article\Mapper\ArticlePostsMapper;
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
use Zend\Paginator\Paginator;
20
21
class PostService extends ArticleService
22
{
23
    /**
24
     * @var ArticleMapper
25
     */
26
    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...
27
28
    /**
29
     * @var ArticlePostsMapper
30
     */
31
    private $articlePostsMapper;
32
33
    /**
34
     * @var ArticleFilter
35
     */
36
    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...
37
38
    /**
39
     * @var PostFilter
40
     */
41
    private $postFilter;
42
43
    /**
44
     * @var CategoryMapper
45
     */
46
    private $categoryMapper;
47
48
    /**
49
     * @var Upload
50
     */
51
    private $upload;
52
53
    /**
54
     * @var AdminUsersMapper
55
     */
56
    private $adminUsersMapper;
57
58
    /**
59
     * PostService constructor.
60
     *
61
     * @param ArticleMapper      $articleMapper
62
     * @param ArticlePostsMapper $articlePostsMapper
63
     * @param ArticleFilter      $articleFilter
64
     * @param PostFilter         $postFilter
65
     * @param CategoryMapper     $categoryMapper
66
     * @param Upload             $upload
67
     * @param AdminUsersMapper   $adminUsersMapper
68
     */
69 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...
70
        ArticleMapper $articleMapper,
71
        ArticlePostsMapper $articlePostsMapper,
72
        ArticleFilter $articleFilter,
73
        PostFilter $postFilter,
74
        CategoryMapper $categoryMapper,
75
        Upload $upload,
76
        AdminUsersMapper $adminUsersMapper
77
    ) {
78
        parent::__construct($articleMapper, $articleFilter);
79
80
        $this->articleMapper = $articleMapper;
81
        $this->articlePostsMapper = $articlePostsMapper;
82
        $this->articleFilter = $articleFilter;
83
        $this->postFilter = $postFilter;
84
        $this->categoryMapper = $categoryMapper;
85
        $this->upload = $upload;
86
        $this->adminUsersMapper = $adminUsersMapper;
87
    }
88
89
    public function fetchAllArticles($page, $limit): Paginator
90
    {
91
        $select = $this->articlePostsMapper->getPaginationSelect();
92
93
        return $this->getPagination($select, $page, $limit);
94
    }
95
96
    public function fetchSingleArticleBySlug($slug)
97
    {
98
        return $this->articlePostsMapper->getBySlug($slug);
99
    }
100
101
    public function fetchSingleArticle($articleId)
102
    {
103
        return $this->articlePostsMapper->get($articleId);
104
    }
105
106
    public function fetchNearestArticle($articlePublishedAt)
107
    {
108
        return [
109
            $this->articlePostsMapper->getNear($articlePublishedAt, 1),
110
            $this->articlePostsMapper->getNear($articlePublishedAt, -1),
111
        ];
112
    }
113
114 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...
115
    {
116
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
117
        $postFilter = $this->postFilter->getInputFilter()->setData($data);
118
119
        if (!$articleFilter->isValid() || !$postFilter->isValid()) {
120
            throw new FilterException($articleFilter->getMessages() + $postFilter->getMessages());
121
        }
122
123
        $id = Uuid::uuid1()->toString();
124
        $uuId = (new MysqlUuid($id))->toFormat(new Binary());
125
126
        $article = $articleFilter->getValues();
127
        $article += [
128
            'admin_user_uuid' => $this->adminUsersMapper->getUuid($article['admin_user_id']),
129
            'type'            => ArticleType::POST,
130
            'article_id'      => $id,
131
            'article_uuid'    => $uuId,
132
        ];
133
134
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
135
        unset($article['category_id'], $article['admin_user_id']);
136
137
        $post = $postFilter->getValues() + [
138
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
139
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
140
                'article_uuid' => $article['article_uuid'],
141
            ];
142
143
        $this->articleMapper->insert($article);
144
        $this->articlePostsMapper->insert($post);
145
    }
146
147 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...
148
    {
149
        $oldArticle = $this->articlePostsMapper->get($id);
150
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
151
        $postFilter = $this->postFilter->getInputFilter()->setData($data);
152
153
        if (!$articleFilter->isValid() || !$postFilter->isValid()) {
154
            throw new FilterException($articleFilter->getMessages() + $postFilter->getMessages());
155
        }
156
157
        $article = $articleFilter->getValues() + ['article_uuid' => $oldArticle->article_uuid];
158
        $post = $postFilter->getValues() + [
159
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
160
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
161
            ];
162
163
        $article['admin_user_uuid'] = $this->adminUsersMapper->getUuid($article['admin_user_id']);
164
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
165
        unset($article['category_id'], $article['admin_user_id']);
166
167
        // We don't want to force user to re-upload image on edit
168
        if (!$post['featured_img']) {
169
            unset($post['featured_img']);
170
        } else {
171
            $this->upload->deleteFile($oldArticle->featured_img);
172
        }
173
174
        if (!$post['main_img']) {
175
            unset($post['main_img']);
176
        } else {
177
            $this->upload->deleteFile($oldArticle->main_img);
178
        }
179
180
        $this->articleMapper->update($article, ['article_uuid' => $article['article_uuid']]);
181
        $this->articlePostsMapper->update($post, ['article_uuid' => $article['article_uuid']]);
182
    }
183
184 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...
185
    {
186
        $post = $this->articlePostsMapper->get($id);
187
188
        if (!$post) {
189
            throw new \Exception('Article not found!');
190
        }
191
192
        $this->upload->deleteFile($post->main_img);
193
        $this->upload->deleteFile($post->featured_img);
194
        $this->articlePostsMapper->delete(['article_uuid' => $post->article_uuid]);
195
        $this->delete($post->article_uuid);
196
    }
197
198
    public function getForSelect()
199
    {
200
        return $this->articlePostsMapper->getAll();
201
    }
202
203
    public function getCategories($articleId)
204
    {
205
        return $this->articleMapper->getCategories($articleId);
206
    }
207
208
    public function getLatestWeb($limit)
209
    {
210
        return $this->articlePostsMapper->getLatest($limit);
211
    }
212
}
213