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