EventService::fetchFutureEvents()   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 0
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\EventFilter;
11
use Article\Mapper\ArticleEventsMapper;
12
use Article\Mapper\ArticleMapper;
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 EventService extends ArticleService
21
{
22
    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...
23
    private $articleEventsMapper;
24
    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...
25
    private $eventFilter;
26
    private $categoryMapper;
27
    private $upload;
28
    private $adminUsersMapper;
29
30 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...
31
        ArticleMapper $articleMapper,
32
        ArticleEventsMapper $articleEventsMapper,
33
        ArticleFilter $articleFilter,
34
        EventFilter $eventFilter,
35
        CategoryMapper $categoryMapper,
36
        Upload $upload,
37
        AdminUsersMapper $adminUsersMapper
38
    ) {
39
        parent::__construct($articleMapper, $articleFilter);
40
        $this->articleMapper = $articleMapper;
41
        $this->articleEventsMapper = $articleEventsMapper;
42
        $this->articleFilter = $articleFilter;
43
        $this->eventFilter = $eventFilter;
44
        $this->categoryMapper = $categoryMapper;
45
        $this->upload = $upload;
46
        $this->adminUsersMapper = $adminUsersMapper;
47
    }
48
49
    public function fetchAllArticles($page, $limit)
50
    {
51
        $select = $this->articleEventsMapper->getPaginationSelect();
52
53
        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...
54
    }
55
56
    public function fetchFutureEvents()
57
    {
58
        return $this->articleEventsMapper->getFuture();
59
    }
60
61
    public function fetchPastEventsPagination($page = 1, $limit = 10)
62
    {
63
        $select = $this->articleEventsMapper->getPastSelect();
64
65
        return $this->getPagination($select, $page, $limit);
66
    }
67
68
    public function fetchEvents($page = 1, $limit = 10)
69
    {
70
        $status = 1;
71
        $select = $this->articleEventsMapper->getPaginationSelect($status);
72
73
        return $this->getPagination($select, $page, $limit);
74
    }
75
76
    public function fetchLatest($limit)
77
    {
78
        return $this->articleEventsMapper->getLatest($limit);
79
    }
80
81
    public function fetchSingleArticle($articleId)
82
    {
83
        return $this->articleEventsMapper->get($articleId);
84
    }
85
86
    public function fetchEventBySlug($slug)
87
    {
88
        return $this->articleEventsMapper->getBySlug($slug);
89
    }
90
91 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...
92
    {
93
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
94
        $eventFilter = $this->eventFilter->getInputFilter()->setData($data);
95
96
        if (!$articleFilter->isValid() || !$eventFilter->isValid()) {
97
            throw new FilterException($articleFilter->getMessages() + $eventFilter->getMessages());
98
        }
99
100
        $id = Uuid::uuid1()->toString();
101
        $uuId = (new MysqlUuid($id))->toFormat(new Binary());
102
103
        $article = $articleFilter->getValues();
104
        $article += [
105
            'admin_user_uuid' => $this->adminUsersMapper->getUuid($article['admin_user_id']),
106
            'type'            => ArticleType::EVENT,
107
            'article_id'      => $id,
108
            'article_uuid'    => $uuId,
109
        ];
110
        unset($article['admin_user_id']);
111
112
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
113
        unset($article['category_id']);
114
115
        $event = $eventFilter->getValues() + [
116
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
117
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
118
                'article_uuid' => $uuId,
119
            ];
120
121
        $this->articleMapper->insert($article);
122
        $this->articleEventsMapper->insert($event);
123
    }
124
125 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...
126
    {
127
        $oldArticle = $this->articleEventsMapper->get($id);
128
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
129
        $eventFilter = $this->eventFilter->getInputFilter()->setData($data);
130
131
        if (!$articleFilter->isValid() || !$eventFilter->isValid()) {
132
            throw new FilterException($articleFilter->getMessages() + $eventFilter->getMessages());
133
        }
134
135
        $article = $articleFilter->getValues() + ['article_uuid' => $oldArticle->article_uuid];
136
        $event = $eventFilter->getValues() + [
137
                'featured_img' => $this->upload->uploadImage($data, 'featured_img'),
138
                'main_img'     => $this->upload->uploadImage($data, 'main_img'),
139
            ];
140
141
        $article['admin_user_uuid'] = $this->adminUsersMapper->getUuid($article['admin_user_id']);
142
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
143
        unset($article['category_id'], $article['admin_user_id']);
144
145
        // We don't want to force user to re-upload image on edit
146
        if (!$event['featured_img']) {
147
            unset($event['featured_img']);
148
        } else {
149
            $this->upload->deleteFile($oldArticle->featured_img);
150
        }
151
152
        if (!$event['main_img']) {
153
            unset($event['main_img']);
154
        } else {
155
            $this->upload->deleteFile($oldArticle->main_img);
156
        }
157
158
        $this->articleMapper->update($article, ['article_uuid' => $article['article_uuid']]);
159
        $this->articleEventsMapper->update($event, ['article_uuid' => $article['article_uuid']]);
160
    }
161
162 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...
163
    {
164
        $event = $this->articleEventsMapper->get($id);
165
166
        if (!$event) {
167
            throw new \Exception('Article not found!');
168
        }
169
170
        $this->upload->deleteFile($event->main_img);
171
        $this->upload->deleteFile($event->featured_img);
172
        $this->articleEventsMapper->delete(['article_uuid' => $event->article_uuid]);
173
        $this->delete($event->article_uuid);
174
    }
175
}
176