Completed
Push — master ( 434bf1...28f331 )
by Phecho
03:35
created

AddCommentCommandHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 3
dl 53
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
B handle() 24 24 3

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
/*
4
 * This file is part of Gitamin.
5
 * 
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Handlers\Commands\Comment;
13
14
use Gitamin\Commands\Comment\AddCommentCommand;
15
use Gitamin\Dates\DateFactory;
16
use Gitamin\Events\Comment\CommentWasAddedEvent;
17
use Gitamin\Models\Comment;
18
use Gitamin\Models\Project;
19
20 View Code Duplication
class AddCommentCommandHandler
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * The date factory instance.
24
     *
25
     * @var \Gitamin\Dates\DateFactory
26
     */
27
    protected $dates;
28
29
    /**
30
     * Create a new report issue command handler instance.
31
     *
32
     * @param \Gitamin\Dates\DateFactory $dates
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct(DateFactory $dates)
37
    {
38
        $this->dates = $dates;
39
    }
40
41
    /**
42
     * Handle the report comment command.
43
     *
44
     * @param \Gitamin\Commands\Comment\AddCommentCommand $command
45
     *
46
     * @return \Gitamin\Models\Comment
47
     */
48
    public function handle(AddCommentCommand $command)
49
    {
50
        $data = [
51
            'message'     => $command->message,
52
            'target_type' => $command->target_type,
53
            'target_id'   => $command->target_id,
54
        ];
55
56
        // Link with the user.
57
        if ($command->author_id) {
58
            $data['author_id'] = $command->author_id;
59
        }
60
        // Link with the project.
61
        if ($command->project_id) {
62
            $data['project_id'] = $command->project_id;
63
        }
64
65
        // Create the comment
66
        $comment = Comment::create($data);
67
68
        event(new CommentWasAddedEvent($comment));
69
70
        return $comment;
71
    }
72
}
73