Completed
Push — version-4 ( a26cca...5903bf )
by
unknown
02:38
created

OnceSender::send()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 25
c 1
b 0
f 1
nc 11
nop 1
dl 0
loc 33
rs 5.3846
1
<?php
2
namespace Fenos\Notifynder\Senders;
3
4
use BadMethodCallException;
5
use Fenos\Notifynder\Contracts\SenderContract;
6
use Fenos\Notifynder\Contracts\SenderManagerContract;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
9
class OnceSender implements SenderContract
10
{
11
    protected $notifications;
12
13
    public function __construct(array $notifications)
14
    {
15
        $this->notifications = $notifications;
16
    }
17
18
    public function send(SenderManagerContract $sender)
19
    {
20
        $model = notifynder_config()->getNotificationModel();
21
22
        $success = true;
23
        foreach($this->notifications as $notification) {
24
            $query = $model::query();
25
            if (!($query instanceof EloquentBuilder)) {
26
                throw new BadMethodCallException("The query method hasn't return an instance of the eloquent query builder.");
27
            }
28
            $query
29
                ->where('from_id', $notification->from_id)
30
                ->where('from_type', $notification->from_type)
31
                ->where('to_id', $notification->to_id)
32
                ->where('to_type', $notification->to_type)
33
                ->where('category_id', $notification->category_id);
34
            if (isset($notification->extra) && !empty($notification->extra)) {
35
                $extra = $notification->extra;
36
                if(is_array($extra)) {
37
                    $extra = json_encode($extra);
38
                }
39
                $query->where('extra', $extra);
40
            }
41
            if (!$query->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean shouldRunExistsQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
                $success = $sender->send([$notification]) ? $success : false;
43
            } else {
44
                $notification = $query->first();
45
                $notification->touch();
0 ignored issues
show
Bug introduced by
The method touch does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
                $notification->unread();
47
            }
48
        }
49
        return $success;
50
    }
51
}