CategoryService::paginateCategoryPosts()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Category\Service;
6
7
use Category\Filter\CategoryFilter;
8
use Category\Mapper\CategoryMapper;
9
use MysqlUuid\Formats\Binary;
10
use MysqlUuid\Uuid as MysqlUuid;
11
use Ramsey\Uuid\Uuid;
12
use Std\FilterException;
13
use UploadHelper\Upload;
14
use Zend\Paginator\Adapter\DbSelect;
15
use Zend\Paginator\Paginator;
16
17
/**
18
 * Class CategoryService.
19
 */
20
class CategoryService
21
{
22
    private $categoryMapper;
23
    private $categoryFilter;
24
    private $upload;
25
26
    /**
27
     * CategoryService constructor.
28
     *
29
     * @param CategoryMapper $categoryMapper
30
     * @param CategoryFilter $categoryFilter
31
     * @param Upload         $upload
32
     */
33
    public function __construct(CategoryMapper $categoryMapper, CategoryFilter $categoryFilter, Upload $upload)
34
    {
35
        $this->categoryMapper = $categoryMapper;
36
        $this->categoryFilter = $categoryFilter;
37
        $this->upload = $upload;
38
    }
39
40
    /**
41
     * Return pagination object to paginate results on view.
42
     *
43
     * @param int $page  Current page set to pagination to display
44
     * @param int $limit Limit set to pagination
45
     *
46
     * @return Paginator
47
     */
48 View Code Duplication
    public function getPagination($page, $limit)
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...
49
    {
50
        $select = $this->categoryMapper->getPaginationSelect();
51
        $paginatorAdapter = new DbSelect($select, $this->categoryMapper->getAdapter());
0 ignored issues
show
Documentation introduced by
$this->categoryMapper->getAdapter() is of type object<Zend\Db\Adapter\AdapterInterface>, but the function expects a object<Zend\Db\Adapter\A...object<Zend\Db\Sql\Sql>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
        $paginator = new Paginator($paginatorAdapter);
53
54
        $paginator->setCurrentPageNumber($page);
55
        $paginator->setItemCountPerPage($limit);
56
57
        return $paginator;
58
    }
59
60
    /**
61
     * Return one category for given UUID.
62
     *
63
     * @param string $categoryId UUID from DB
64
     *
65
     * @return array|\ArrayObject|null
66
     */
67
    public function getCategory($categoryId)
68
    {
69
        return $this->categoryMapper->get($categoryId);
70
    }
71
72
    /**
73
     * Return one category for given URL Slug.
74
     *
75
     * @param string $urlSlug
76
     *
77
     * @return array|\ArrayObject|null
78
     */
79
    public function getCategoryBySlug($urlSlug)
80
    {
81
        return $this->categoryMapper->select(['slug' => $urlSlug])->current();
82
    }
83
84
    /**
85
     * Create new category.
86
     *
87
     * @param  $data
88
     *
89
     * @throws FilterException
90
     */
91
    public function createCategory($data)
92
    {
93
        $filter = $this->categoryFilter->getInputFilter()->setData($data);
94
95
        if (!$filter->isValid()) {
96
            throw new FilterException($filter->getMessages());
97
        }
98
99
        $values = $filter->getValues();
100
        $values['category_id'] = Uuid::uuid1()->toString();
101
        $values['category_uuid'] = (new MysqlUuid($values['category_id']))->toFormat(new Binary());
102
        $values['main_img'] = $this->upload->uploadImage($data, 'main_img');
103
104
        $this->categoryMapper->insert($values);
105
    }
106
107
    /**
108
     * Update existing category.
109
     *
110
     * @param  $data
111
     * @param  $categoryId
112
     *
113
     * @throws FilterException
114
     * @throws \Exception
115
     */
116
    public function updateCategory($data, $categoryId)
117
    {
118
        if (!($oldCategory = $this->getCategory($categoryId))) {
119
            throw new \Exception('CategoryId dos not exist.');
120
        }
121
122
        $filter = $this->categoryFilter->getInputFilter()->setData($data);
123
124
        if (!$filter->isValid()) {
125
            throw new FilterException($filter->getMessages());
126
        }
127
128
        $values = $filter->getValues() + [
129
                'main_img' => $this->upload->uploadImage($data, 'main_img'),
130
            ];
131
132
        // We don't want to force user to re-upload image on edit
133
        if (!$values['main_img']) {
134
            unset($values['main_img']);
135
        } else {
136
            $this->upload->deleteFile($oldCategory->main_img);
137
        }
138
139
        $this->categoryMapper->update($values, ['category_id' => $categoryId]);
140
    }
141
142
    /**
143
     * Delete category by given UUID.
144
     *
145
     * @param string $categoryId UUID from DB
146
     *
147
     * @return bool
148
     */
149
    public function delete($categoryId)
150
    {
151
        $category = $this->getCategory($categoryId);
152
153
        $this->upload->deleteFile($category->main_img);
154
155
        return (bool) $this->categoryMapper->delete(['category_id' => $categoryId]);
156
    }
157
158
    /**
159
     * Returnall categoriess typically to populate select box.
160
     *
161
     * @return \Zend\Db\ResultSet\ResultSet
162
     */
163
    public function getAll($type = null)
164
    {
165
        if ($type) {
166
            return $this->categoryMapper->select(['type' => $type]);
167
        }
168
169
        return $this->categoryMapper->select();
170
    }
171
172
    /**
173
     * Return categories with posts(articles).
174
     *
175
     * @param null $inHomepage
176
     * @param null $inCategoryList
177
     *
178
     * @return mixed
179
     */
180
    public function getCategoriesWithPosts($inHomepage = null, $inCategoryList = null)
181
    {
182
        $categories = $this->categoryMapper->getWeb(7, null, $inHomepage, $inCategoryList)->toArray();
183
184
        foreach ($categories as $ctn => $category) {
185
            $select = $this->categoryMapper->getCategoryPostsSelect($category['category_id'], 4);
186
            $posts = $this->categoryMapper->selectWith($select)->toArray();
187
            $categories[$ctn]['posts'] = $posts;
188
        }
189
190
        return $categories;
191
    }
192
193
    /**
194
     * Return categories posts/articles.
195
     *
196
     * @param null $inCategoryList
197
     *
198
     * @return null|\Zend\Db\ResultSet\ResultSetInterface
199
     */
200
    public function getCategories($inCategoryList = null)
201
    {
202
        return $this->categoryMapper->getWeb(null, ['name' => 'asc'], null, $inCategoryList);
203
    }
204
205
    /**
206
     * Get posts - articles with type == Posts.
207
     *
208
     * @param  $category
209
     * @param int $page
210
     *
211
     * @return Paginator
212
     */
213
    public function paginateCategoryPosts($category, $page = 1): Paginator
214
    {
215
        $categoryid = isset($category->category_id) ? $category->category_id : null;
216
        $select = $this->categoryMapper->getCategoryPostsSelect($categoryid, 12);
217
        $paginatorAdapter = new DbSelect($select, $this->categoryMapper->getAdapter());
0 ignored issues
show
Documentation introduced by
$this->categoryMapper->getAdapter() is of type object<Zend\Db\Adapter\AdapterInterface>, but the function expects a object<Zend\Db\Adapter\A...object<Zend\Db\Sql\Sql>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
218
        $paginator = new Paginator($paginatorAdapter);
219
220
        $paginator->setCurrentPageNumber($page);
221
        $paginator->setItemCountPerPage(12);
222
223
        return $paginator;
224
    }
225
}
226