Notifications::init()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Notifications module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Notifications extends Module {
12
13
    public function init() {
14
        $subscriber = $this->getCurSubscriber();
15
        if ($subscriber && $subscriber->subscribes) {
16
            App::$cur->view->customAsset('js', '/moduleAsset/Notifications/js/Notifications.js');
17
        }
18
    }
19
20
    public function subscribe($chanelAlias) {
21
        $chanel = $this->getChanel($chanelAlias);
22
        $subscriber = $this->getCurSubscriber(true);
23
        $subscribe = Notifications\Subscribe::get([['subscriber_id', $subscriber->id], ['chanel_id', $chanel->id]]);
0 ignored issues
show
Bug introduced by
The property id does not exist on false.
Loading history...
Bug Best Practice introduced by
The property id does not exist on Notifications\Chanel. Since you implemented __get, consider adding a @property annotation.
Loading history...
24
        if ($subscribe) {
25
            $response = new Server\Result();
0 ignored issues
show
Bug introduced by
The type Server\Result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
            $response->successMsg = 'Вы уже подписаны';
27
            return $response->send();
28
        }
29
        $subscribe = new Notifications\Subscribe();
30
        $subscribe->subscriber_id = $subscriber->id;
0 ignored issues
show
Bug Best Practice introduced by
The property subscriber_id does not exist on Notifications\Subscribe. Since you implemented __set, consider adding a @property annotation.
Loading history...
31
        $subscribe->chanel_id = $chanel->id;
0 ignored issues
show
Bug Best Practice introduced by
The property chanel_id does not exist on Notifications\Subscribe. Since you implemented __set, consider adding a @property annotation.
Loading history...
32
        $subscribe->save();
33
        $response = new Server\Result();
34
        $response->successMsg = 'Вы были подписаны на уведомления';
35
        return $response->send();
36
    }
37
38
    public function getChanel($alias) {
39
        $chanel = \Notifications\Chanel::get($alias, 'alias');
40
        if (!$chanel) {
41
            $chanel = new \Notifications\Chanel();
42
            $chanel->alias = $alias;
0 ignored issues
show
Bug Best Practice introduced by
The property alias does not exist on Notifications\Chanel. Since you implemented __set, consider adding a @property annotation.
Loading history...
43
            $chanel->name = $alias;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Notifications\Chanel. Since you implemented __set, consider adding a @property annotation.
Loading history...
44
            $chanel->save();
45
        }
46
        return $chanel;
47
    }
48
49
    public function getCurSubscriber($create = false) {
50
        $device = $this->getCurDevice($create);
51
        if (!$device) {
52
            return false;
53
        }
54
        if (!$device->subscriber) {
55
            $subscriber = null;
56
            if (class_exists('Users\User') && Users\User::$cur->id) {
57
                $subscriber = \Notifications\Subscriber::get(Users\User::$cur->id, 'user_id');
58
            }
59
            if (!$subscriber) {
60
                $subscriber = new \Notifications\Subscriber();
61
                if (class_exists('Users\User') && Users\User::$cur->id) {
62
                    $subscriber->user_id = Users\User::$cur->id;
63
                }
64
                $subscriber->save(['empty' => true]);
65
            }
66
            $device->subscriber_id = $subscriber->id;
67
            $device->save();
68
            return $subscriber;
69
        }
70
71
        return $device->subscriber;
72
    }
73
74
    public function getCurDevice($create = false) {
75
        $deviceKey = $this->getDevicekey();
76
        if (!$deviceKey) {
77
            if ($create) {
78
                $deviceKey = Tools::randomString(70);
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
                $this->setDeviceKey($deviceKey);
80
            } else {
81
                return false;
82
            }
83
        } else {
84
            $this->setDeviceKey($deviceKey);
85
        }
86
        $device = \Notifications\Subscriber\Device::get($deviceKey, 'key');
87
        if (!$device && $create) {
88
            $device = new \Notifications\Subscriber\Device();
89
            $device->key = $deviceKey;
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Notifications\Subscriber\Device. Since you implemented __set, consider adding a @property annotation.
Loading history...
90
            $device->save();
91
            $device->date_last_check = $device->date_create;
0 ignored issues
show
Bug Best Practice introduced by
The property date_last_check does not exist on Notifications\Subscriber\Device. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property date_create does not exist on Notifications\Subscriber\Device. Since you implemented __get, consider adding a @property annotation.
Loading history...
92
            $device->save();
93
        } elseif (!$device) {
94
            return false;
95
        }
96
        return $device;
97
    }
98
99
    public function getDevicekey() {
100
        if (!empty($_SESSION['notification-device'])) {
101
            return $_SESSION['notification-device'];
102
        }
103
        if (!empty($_COOKIE['notification-device'])) {
104
            return $_COOKIE['notification-device'];
105
        }
106
    }
107
108
    public function setDeviceKey($deviceKey) {
109
        if (headers_sent()) {
110
            $_SESSION['notification-device'] = $deviceKey;
111
        } else {
112
            setcookie("notification-device", $deviceKey, time() + 360000, "/");
113
        }
114
    }
115
}