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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.