Passed
Push — master ( 2c7b25...f52af3 )
by Mihail
05:14
created

EntityAddNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
6
use Apps\ActiveRecord\UserNotification;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Serialize;
9
10
/**
11
 * Class EntityAddNotification. Add user notification in database table
12
 * @package Apps\Model\Front\Profile
13
 */
14
class EntityAddNotification extends Model
15
{
16
    const MSG_DEFAULT = 'New notification event: &laquo;%snippet%&raquo;';
17
    const MSG_ADD_WALLPOST = 'New post on the wall: &laquo;%snippet%&raquo;';
18
    const MSG_ADD_WALLANSWER = 'New answer &laquo;%snippet%&raquo; for wall post &laquo;%post%&raquo;';
19
    const MSG_ADD_COMMENTANSWER ='New answer &laquo;%snippet%&raquo; to your comment &laquo;%post%&raquo;';
20
    
21
    private $_targetId;
22
23
    /**
24
     * EntityAddNotification constructor. Pass target user_id inside the model
25
     * @param bool $targetId
26
     */
27
    public function __construct($targetId)
28
    {
29
        $this->_targetId = $targetId;
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Add notification for user
35
     * @param string $uri
36
     * @param string $msg
37
     * @param array|null $vars
38
     */
39
    public function add($uri, $msg = self::MSG_DEFAULT, array $vars = null)
40
    {
41
        // save data into database
42
        $record = new UserNotification();
43
        $record->user_id = $this->_targetId;
44
        $record->uri = $uri;
45
        $record->msg = $msg;
46
        if ($vars !== null) {
47
            $record->vars = Serialize::encode($vars);
48
        }
49
50
        $record->save();
51
    }
52
}