HasNotificationOptions::setNotificationOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
4
namespace BRKFun\NotificationOptions\Traits;
5
6
use BRKFun\NotificationOptions\Models\NotificationOption;
7
8
trait HasNotificationOptions
9
{
10
    public $token;
11
12
    public function setToken()
13
    {
14
        $this->token = tokenSetter();
15
    }
16
17
    public function initializeHasNotificationOptions()
18
    {
19
        $this->setToken();
20
        $this->with[] = 'notificationOptions';
0 ignored issues
show
Documentation introduced by
The property with does not exist on object<BRKFun\Notificati...HasNotificationOptions>. 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...
21
    }
22
23
    public function saveNotificationOptions($key, $value)
24
    {
25
        $this
26
            ->notificationOptions()
27
            ->updateOrCreate(
28
                [
29
                    'key' => $key,
30
                ],
31
                [
32
                    'value' => $value,
33
                    'token' => $this->token,
34
                ]
35
            );
36
    }
37
38
    public function setNotificationOption($key)
39
    {
40
        $this
41
            ->notificationOptions()
42
            ->create(
43
                [
44
                    'key'   => $key,
45
                    'value' => config('notification-option.defaultValue', 1),
46
                    'token' => $this->token,
47
                ]
48
            );
49
        $this->load('notificationOptions');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
    }
51
52
    public function notificationOptions()
53
    {
54
        return $this->morphMany(config('notification-option.model', NotificationOption::class), 'notifiable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
    }
56
57
    public function __get($key)
58
    {
59
        if (substr($key, 0, 5) === 'wants') {
60
            $wantWhat = lcfirst(substr($key, 5));
61
            $data     = $this->notificationOptions->where('key', $wantWhat)->first();
0 ignored issues
show
Documentation introduced by
The property notificationOptions does not exist on object<BRKFun\Notificati...HasNotificationOptions>. 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...
62
            if ($data) {
63
                return $data->value;
64
            }
65
            $this->setNotificationOption($wantWhat);
66
            return $this->notificationOptions->where('key', $wantWhat)->first()->value;
0 ignored issues
show
Documentation introduced by
The property notificationOptions does not exist on object<BRKFun\Notificati...HasNotificationOptions>. 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
        }
68
69
        return parent::__get($key);
70
    }
71
72
    public function __set($name, $value)
73
    {
74
        if (substr($name, 0, 5) === 'wants') {
75
            $setName = lcfirst(substr($name, 5));
76
            $this->saveNotificationOptions($setName, $value);
77
            unset($this->$name);
78
            return $this;
79
        }
80
81
        return parent::__set($name, $value);
82
    }
83
}
84