1 | <?php |
|||||||||||
2 | ||||||||||||
3 | namespace Evilnet\Inbox; |
|||||||||||
4 | use Evilnet\Inbox\Controller; |
|||||||||||
5 | use Evilnet\Inbox\Services\InboxService; |
|||||||||||
6 | use Illuminate\Http\Request; |
|||||||||||
7 | ||||||||||||
8 | class InboxController extends Controller |
|||||||||||
9 | { |
|||||||||||
10 | ||||||||||||
11 | protected $inboxService; |
|||||||||||
12 | ||||||||||||
13 | public function __construct(InboxService $inboxService) |
|||||||||||
14 | { |
|||||||||||
15 | $this->inboxService = $inboxService; |
|||||||||||
16 | } |
|||||||||||
17 | ||||||||||||
18 | /** |
|||||||||||
19 | * Display a listing of the resource. |
|||||||||||
20 | * |
|||||||||||
21 | * @return \Illuminate\Http\Response |
|||||||||||
22 | */ |
|||||||||||
23 | public function index() |
|||||||||||
24 | { |
|||||||||||
25 | $conversations = $this->inboxService->fetchAllConversation(); |
|||||||||||
26 | ||||||||||||
27 | $users = $this->inboxService->getInboxUsers($conversations); |
|||||||||||
28 | ||||||||||||
29 | ||||||||||||
30 | return view('inbox::inbox.index', compact('conversations', 'users')); |
|||||||||||
31 | } |
|||||||||||
32 | /** |
|||||||||||
33 | * Show the form for creating a new resource. |
|||||||||||
34 | * |
|||||||||||
35 | * @return \Illuminate\Http\Response |
|||||||||||
36 | */ |
|||||||||||
37 | public function create() |
|||||||||||
38 | { |
|||||||||||
39 | return view('inbox::inbox.create'); |
|||||||||||
40 | } |
|||||||||||
41 | ||||||||||||
42 | /** |
|||||||||||
43 | * Store a newly created resource in storage. |
|||||||||||
44 | * |
|||||||||||
45 | * @param \Illuminate\Http\Request $request |
|||||||||||
46 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response |
|||||||||||
47 | */ |
|||||||||||
48 | public function store(Request $request) |
|||||||||||
49 | { |
|||||||||||
50 | $this->validate($request, |
|||||||||||
51 | [ |
|||||||||||
52 | 'user' => 'required', |
|||||||||||
53 | 'message' => 'required|max:10000', |
|||||||||||
54 | 'subject' => 'required' |
|||||||||||
55 | ] |
|||||||||||
56 | ); |
|||||||||||
57 | $this->inboxService->addConversation($request->get('user'), $request->get('message'), $request->get('subject')); |
|||||||||||
58 | } |
|||||||||||
59 | ||||||||||||
60 | /** |
|||||||||||
61 | * Display the specified resource. |
|||||||||||
62 | * |
|||||||||||
63 | * @param conversation|int $id |
|||||||||||
64 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|||||||||||
65 | */ |
|||||||||||
66 | public function show(Conversation $id) |
|||||||||||
67 | { |
|||||||||||
68 | if(auth()->id() == $id->id_to OR auth()->id() == $id->id_from) { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
69 | return view('inbox::inbox.show')->with('conversation', $id); |
|||||||||||
70 | } |
|||||||||||
71 | return redirect()->back(); |
|||||||||||
72 | ||||||||||||
73 | } |
|||||||||||
74 | ||||||||||||
75 | /** |
|||||||||||
76 | * Update the specified resource in storage. |
|||||||||||
77 | * |
|||||||||||
78 | * @param \Illuminate\Http\Request $request |
|||||||||||
79 | * @param conversation|int $id |
|||||||||||
80 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response |
|||||||||||
81 | */ |
|||||||||||
82 | public function addMessage(Request $request, Conversation $id) |
|||||||||||
83 | { |
|||||||||||
84 | $this->validate($request, |
|||||||||||
85 | [ |
|||||||||||
86 | 'message' => 'required|max:10000' |
|||||||||||
87 | ] |
|||||||||||
88 | ); |
|||||||||||
89 | if(auth()->id() == $id->id_to OR auth()->id() == $id->id_from) { |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
90 | ||||||||||||
91 | $this->inboxService->addMessage($request, $id); |
|||||||||||
92 | } |
|||||||||||
93 | return redirect()->back(); |
|||||||||||
94 | ||||||||||||
95 | } |
|||||||||||
96 | /** |
|||||||||||
97 | * Remove the specified resource from storage. |
|||||||||||
98 | * |
|||||||||||
99 | * @param int $id |
|||||||||||
100 | * @return \Illuminate\Http\Response |
|||||||||||
101 | */ |
|||||||||||
102 | public function destroy(Conversation $id) |
|||||||||||
103 | { |
|||||||||||
104 | if(auth()->id() == $id->id_to OR auth()->id() == $id->id_from) { |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
105 | ||||||||||||
106 | $this->inboxService->deleteConversation($id); |
|||||||||||
107 | } |
|||||||||||
108 | return redirect()->back(); |
|||||||||||
109 | ||||||||||||
110 | } |
|||||||||||
111 | } |
|||||||||||
112 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.