UserUnreadNotificationModel::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Model;
13
14
use Jitamin\Foundation\Database\Model;
15
16
/**
17
 * User Unread Notification.
18
 */
19
class UserUnreadNotificationModel extends Model
20
{
21
    /**
22
     * SQL table name.
23
     *
24
     * @var string
25
     */
26
    const TABLE = 'user_has_unread_notifications';
27
28
    /**
29
     * Add unread notification to someone.
30
     *
31
     * @param int    $user_id
32
     * @param string $event_name
33
     * @param array  $event_data
34
     */
35
    public function create($user_id, $event_name, array $event_data)
36
    {
37
        $this->db->table(self::TABLE)->insert([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
38
            'user_id'       => $user_id,
39
            'date_creation' => time(),
40
            'event_name'    => $event_name,
41
            'event_data'    => json_encode($event_data),
42
        ]);
43
    }
44
45
    /**
46
     * Get one notification.
47
     *
48
     * @param int $notification_id
49
     *
50
     * @return array|null
51
     */
52
    public function getById($notification_id)
53
    {
54
        $notification = $this->db->table(self::TABLE)->eq('id', $notification_id)->findOne();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
55
56
        if (!empty($notification)) {
57
            $this->unserialize($notification);
58
        }
59
60
        return $notification;
61
    }
62
63
    /**
64
     * Get all notifications for a user.
65
     *
66
     * @param int $user_id
67
     *
68
     * @return array
69
     */
70 View Code Duplication
    public function getAll($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $events = $this->db->table(self::TABLE)->eq('user_id', $user_id)->desc('date_creation')->findAll();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
73
74
        foreach ($events as &$event) {
75
            $this->unserialize($event);
76
        }
77
78
        return $events;
79
    }
80
81
    /**
82
     * Mark a notification as read.
83
     *
84
     * @param int $user_id
85
     * @param int $notification_id
86
     *
87
     * @return bool
88
     */
89
    public function markAsRead($user_id, $notification_id)
90
    {
91
        return $this->db->table(self::TABLE)->eq('id', $notification_id)->eq('user_id', $user_id)->remove();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
92
    }
93
94
    /**
95
     * Mark all notifications as read for a user.
96
     *
97
     * @param int $user_id
98
     *
99
     * @return bool
100
     */
101
    public function markAllAsRead($user_id)
102
    {
103
        return $this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
104
    }
105
106
    /**
107
     * Return true if the user as unread notifications.
108
     *
109
     * @param int $user_id
110
     *
111
     * @return bool
112
     */
113
    public function hasNotifications($user_id)
114
    {
115
        return $this->db->table(self::TABLE)->eq('user_id', $user_id)->exists();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
116
    }
117
118
    /**
119
     * Unserialize the event.
120
     *
121
     * @param GenericEvent $event
122
     */
123
    private function unserialize(&$event)
124
    {
125
        $event['event_data'] = json_decode($event['event_data'], true);
126
        $event['title'] = $this->notificationModel->getTitleWithoutAuthor($event['event_name'], $event['event_data']);
0 ignored issues
show
Documentation introduced by
The property notificationModel does not exist on object<Jitamin\Model\UserUnreadNotificationModel>. 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...
127
    }
128
}
129