DiscussionService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 22.77 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 13
dl 23
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
A fetchAllArticles() 0 6 1
A fetchSingleArticle() 0 4 1
A createArticle() 3 28 3
A updateArticle() 3 20 3
A deleteArticle() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\DiscussionFilter;
11
use Article\Mapper\ArticleDiscussionsMapper;
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
19
class DiscussionService extends ArticleService
20
{
21
    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...
22
    private $articleDiscussionsMapper;
23
    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...
24
    private $discussionFilter;
25
    private $categoryMapper;
26
    private $adminUsersMapper;
27
28 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...
29
        ArticleMapper $articleMapper,
30
        ArticleDiscussionsMapper $articleDiscussionsMapper,
31
        ArticleFilter $articleFilter,
32
        DiscussionFilter $discussionFilter,
33
        CategoryMapper $categoryMapper,
34
        AdminUsersMapper $adminUsersMapper
35
    ) {
36
        parent::__construct($articleMapper, $articleFilter);
37
38
        $this->articleMapper = $articleMapper;
39
        $this->articleDiscussionsMapper = $articleDiscussionsMapper;
40
        $this->articleFilter = $articleFilter;
41
        $this->discussionFilter = $discussionFilter;
42
        $this->categoryMapper = $categoryMapper;
43
        $this->adminUsersMapper = $adminUsersMapper;
44
    }
45
46
    public function fetchAllArticles($page, $limit)
47
    {
48
        $select = $this->articleDiscussionsMapper->getPaginationSelect();
49
50
        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...
51
    }
52
53
    public function fetchSingleArticle($articleId)
54
    {
55
        return $this->articleDiscussionsMapper->get($articleId);
56
    }
57
58
    public function createArticle($user, $data)
59
    {
60
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
61
        $discussionFilter = $this->discussionFilter->getInputFilter()->setData($data);
62
63 View Code Duplication
        if (!$articleFilter->isValid() || !$discussionFilter->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
            throw new FilterException($articleFilter->getMessages() + $discussionFilter->getMessages());
65
        }
66
67
        $id = Uuid::uuid1()->toString();
68
        $uuId = (new MysqlUuid($id))->toFormat(new Binary());
69
70
        $article = $articleFilter->getValues();
71
        $article += [
72
            'admin_user_uuid' => $this->adminUsersMapper->getUuid($article['admin_user_id']),
73
            'type'            => ArticleType::DISCUSSION,
74
            'article_id'      => $id,
75
            'article_uuid'    => $uuId,
76
        ];
77
78
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
79
        unset($article['category_id'], $article['admin_user_id']);
80
81
        $discussion = $discussionFilter->getValues() + ['article_uuid' => $uuId];
82
83
        $this->articleMapper->insert($article);
84
        $this->articleDiscussionsMapper->insert($discussion);
85
    }
86
87
    public function updateArticle($data, $id)
88
    {
89
        $article = $this->articleDiscussionsMapper->get($id);
90
        $articleFilter = $this->articleFilter->getInputFilter()->setData($data);
91
        $discussionFilter = $this->discussionFilter->getInputFilter()->setData($data);
92
93 View Code Duplication
        if (!$articleFilter->isValid() || !$discussionFilter->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            throw new FilterException($articleFilter->getMessages() + $discussionFilter->getMessages());
95
        }
96
97
        $article = $articleFilter->getValues() + ['article_uuid' => $article->article_uuid];
98
        $discussion = $discussionFilter->getValues();
99
100
        $article['admin_user_uuid'] = $this->adminUsersMapper->getUuid($article['admin_user_id']);
101
        $article['category_uuid'] = $this->categoryMapper->get($article['category_id'])->category_uuid;
102
        unset($article['category_id'], $article['admin_user_id']);
103
104
        $this->articleMapper->update($article, ['article_uuid' => $article['article_uuid']]);
105
        $this->articleDiscussionsMapper->update($discussion, ['article_uuid' => $article['article_uuid']]);
106
    }
107
108
    public function deleteArticle($id)
109
    {
110
        $discussion = $this->articleDiscussionsMapper->get($id);
111
112
        if (!$discussion) {
113
            throw new \Exception('Article not found!');
114
        }
115
116
        $this->articleDiscussionsMapper->delete(['article_uuid' => $discussion->article_uuid]);
117
        $this->delete($discussion->article_uuid);
118
    }
119
}
120