InboxRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B conversationExists() 0 11 6
A fetchAll() 0 4 1
A userExists() 0 6 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: me
5
 * Date: 08.07.2017
6
 * Time: 23:26
7
 */
8
9
namespace Evilnet\Inbox\Repository;
10
11
use Evilnet\Inbox\Conversation;
12
use Evilnet\Inbox\User;
13
use Carbon\Carbon;
14
15
class InboxRepository
16
{
17
	public function fetchAll()
18
    {
19
        return Conversation::where('id_from', auth()->id())->orderBy('id', 'desc')->orWhere('id_to', auth()->id())->orderBy('id', 'desc')->get()->groupBy(function ($data){
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        return Conversation::where('id_from', /** @scrutinizer ignore-call */ auth()->id())->orderBy('id', 'desc')->orWhere('id_to', auth()->id())->orderBy('id', 'desc')->get()->groupBy(function ($data){
Loading history...
20
            return Carbon::parse($data->created_at)->format('Y-m-d');
21
        });
22
    }
23
24
    public function userExists($user)
25
    {
26
        if (count(User::where('name', $user)->first())) {
27
            return true;
28
        } else {
29
            return false;
30
        }
31
    }
32
33
    public function conversationExists($to, $from)
34
    {
35
        //prevent to pm yourself
36
        if(auth()->user()->name == $to->name){
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        if(/** @scrutinizer ignore-call */ auth()->user()->name == $to->name){
Loading history...
37
            return true;
38
        }
39
        //check if conversation already exists
40
        if (count(Conversation::where('id_to', $to->id)->first()) == 1 AND count(Conversation::where('id_from', $from)->first()) == 1 OR count(Conversation::where('id_to', $from)->first()) == 1 AND count(Conversation::where('id_from', $to->id)->first()) == 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

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

Loading history...
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

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

Loading history...
41
            return true;
42
        }
43
        return false;
44
    }
45
46
}