Completed
Push — master ( 0bba58...460685 )
by
unknown
10:36 queued 06:09
created

OnceSender::send()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Fenos\Notifynder\Senders;
4
5
use BadMethodCallException;
6
use Fenos\Notifynder\Builder\Notification;
7
use Fenos\Notifynder\Contracts\SenderContract;
8
use Fenos\Notifynder\Contracts\SenderManagerContract;
9
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
10
11
/**
12
 * Class OnceSender.
13
 */
14
class OnceSender implements SenderContract
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $notifications;
20
21
    /**
22
     * OnceSender constructor.
23
     *
24
     * @param array $notifications
25
     */
26
    public function __construct(array $notifications)
27
    {
28
        $this->notifications = $notifications;
29
    }
30
31
    /**
32
     * Send the notification once.
33
     *
34
     * @param SenderManagerContract $sender
35
     * @return bool
36
     */
37
    public function send(SenderManagerContract $sender)
38
    {
39
        $success = true;
40
        foreach ($this->notifications as $notification) {
41
            $query = $this->getQuery($notification);
42
            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...
43
                $success = $sender->send([$notification]) ? $success : false;
44
                continue;
45
            }
46
            $success = $query->firstOrFail()->resend() ? $success : false;
47
        }
48
49
        return $success;
50
    }
51
52
    /**
53
     * Get the base query.
54
     *
55
     * @param Notification $notification
56
     * @return \Illuminate\Database\Eloquent\Builder
57
     */
58
    protected function getQuery(Notification $notification)
59
    {
60
        $query = $this->getQueryInstance();
61
        $query
62
            ->where('from_id', $notification->from_id)
0 ignored issues
show
Documentation introduced by
The property from_id does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
            ->where('from_type', $notification->from_type)
0 ignored issues
show
Documentation introduced by
The property from_type does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
            ->where('to_id', $notification->to_id)
0 ignored issues
show
Documentation introduced by
The property to_id does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
            ->where('to_type', $notification->to_type)
0 ignored issues
show
Documentation introduced by
The property to_type does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
            ->where('category_id', $notification->category_id);
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
        $extra = $notification->extra;
0 ignored issues
show
Documentation introduced by
The property extra does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
        if (! is_null($extra) && ! empty($extra)) {
69
            if (is_array($extra)) {
70
                $extra = json_encode($extra);
71
            }
72
            $query->where('extra', $extra);
73
        }
74
75
        return $query;
76
    }
77
78
    /**
79
     * @return \Illuminate\Database\Eloquent\Builder
80
     */
81
    protected function getQueryInstance()
82
    {
83
        $model = notifynder_config()->getNotificationModel();
84
        $query = $model::query();
85
        if (! ($query instanceof EloquentBuilder)) {
86
            throw new BadMethodCallException("The query method hasn't return an instance of [".EloquentBuilder::class.'].');
87
        }
88
89
        return $query;
90
    }
91
}
92