1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Repositories; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Xetaravel\Events\Discuss\CategoryWasChangedEvent; |
7
|
|
|
use Xetaravel\Events\Discuss\ConversationWasLockedEvent; |
8
|
|
|
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent; |
9
|
|
|
use Xetaravel\Events\Discuss\TitleWasChangedEvent; |
10
|
|
|
use Xetaravel\Models\DiscussConversation; |
11
|
|
|
use Xetaravel\Models\DiscussPost; |
12
|
|
|
use Xetaravel\Models\DiscussUser; |
13
|
|
|
|
14
|
|
|
class DiscussConversationRepository |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create the new conversation and save it. |
19
|
|
|
* |
20
|
|
|
* @param array $data The data used to create the conversation. |
21
|
|
|
* |
22
|
|
|
* @return \Xetaravel\Models\DiscussConversation |
23
|
|
|
*/ |
24
|
|
|
public static function create(array $data): DiscussConversation |
25
|
|
|
{ |
26
|
|
|
$conversation = [ |
27
|
|
|
'title' => $data['title'], |
28
|
|
|
'category_id' => $data['category_id'] |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
$user = Auth::user(); |
32
|
|
|
|
33
|
|
|
if ($user->hasPermission('manage.discuss.conversations')) { |
34
|
|
|
$conversation += [ |
35
|
|
|
'is_locked' => isset($data['is_locked']) ? true : false, |
36
|
|
|
'is_pinned' => isset($data['is_pinned']) ? true : false, |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$conversation = DiscussConversation::create($conversation); |
41
|
|
|
|
42
|
|
|
$comment = DiscussPost::create([ |
43
|
|
|
'conversation_id' => $conversation->id, |
44
|
|
|
'content' => $data['content'] |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$participant = DiscussUser::create([ |
|
|
|
|
48
|
|
|
'conversation_id' => $conversation->id, |
49
|
|
|
'is_read' => 1 |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$conversation->first_post_id = $comment->id; |
53
|
|
|
$conversation->last_post_id = $comment->id; |
54
|
|
|
$conversation->save(); |
55
|
|
|
|
56
|
|
|
return $conversation; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Update the conversation data and save it. |
61
|
|
|
* |
62
|
|
|
* @param array $data The data used to update the conversation. |
63
|
|
|
* @param \Xetaravel\Models\DiscussConversation $conversation The conversation to update. |
64
|
|
|
* |
65
|
|
|
* @return \Xetaravel\Models\DiscussConversation |
66
|
|
|
*/ |
67
|
|
|
public static function update(array $data, DiscussConversation $conversation): DiscussConversation |
68
|
|
|
{ |
69
|
|
|
if (Auth::user()->hasPermission('manage.discuss.conversations')) { |
|
|
|
|
70
|
|
|
$data['is_pinned'] = isset($data['is_pinned']) ? true : false; |
71
|
|
|
$data['is_locked'] = isset($data['is_locked']) ? true : false; |
72
|
|
|
|
73
|
|
View Code Duplication |
if ($conversation->is_pinned != $data['is_pinned'] && $data['is_pinned'] == true) { |
|
|
|
|
74
|
|
|
event(new ConversationWasPinnedEvent($conversation, Auth::user())); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
if ($conversation->is_locked != $data['is_locked'] && $data['is_locked'] == true) { |
|
|
|
|
78
|
|
|
event(new ConversationWasLockedEvent($conversation, Auth::user())); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$conversation->is_locked = $data['is_locked']; |
82
|
|
|
$conversation->is_pinned = $data['is_pinned']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($conversation->title != $data['title']) { |
86
|
|
|
event(new TitleWasChangedEvent($conversation, $data['title'], $conversation->title)); |
87
|
|
|
|
88
|
|
|
$conversation->title = $data['title']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($conversation->category_id != $data['category_id']) { |
92
|
|
|
event(new CategoryWasChangedEvent($conversation, $data['category_id'], $conversation->category_id)); |
93
|
|
|
|
94
|
|
|
$conversation->category_id = $data['category_id']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$conversation->save(); |
98
|
|
|
|
99
|
|
|
return $conversation; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.