Passed
Push — master ( d15464...d64dd2 )
by Alexey
05:25
created

Notifications::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 12
rs 9.4285
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 1
    public function subscribe($chanelAlias) {
21 1
        $chanel = $this->getChanel($chanelAlias);
22 1
        $subscriber = $this->getCurSubscriber(true);
23 1
        $subscribe = Notifications\Subscribe::get([['subscriber_id', $subscriber->id], ['chanel_id', $chanel->id]]);
24 1
        if ($subscribe) {
25 1
            $response = new Server\Result();
26 1
            $response->successMsg = 'Вы уже подписаны';
27 1
            return $response->send();
28
        }
29 1
        $subscribe = new Notifications\Subscribe();
30 1
        $subscribe->subscriber_id = $subscriber->id;
31 1
        $subscribe->chanel_id = $chanel->id;
32 1
        $subscribe->save();
33 1
        $response = new Server\Result();
34 1
        $response->successMsg = 'Вы были подписаны на уведомления';
35 1
        return $response->send();
36
    }
37
38 1
    public function getChanel($alias) {
39 1
        $chanel = \Notifications\Chanel::get($alias, 'alias');
40 1
        if (!$chanel) {
41 1
            $chanel = new \Notifications\Chanel();
42 1
            $chanel->alias = $alias;
43 1
            $chanel->name = $alias;
44 1
            $chanel->save();
45 1
        }
46 1
        return $chanel;
47
    }
48
49 2
    public function getCurSubscriber($create = false) {
50 2
        $device = $this->getCurDevice($create);
51 2
        if (!$device) {
52 1
            return false;
53
        }
54 2
        if (!$device->subscriber) {
55 2
            $subscriber = null;
56 2
            if (class_exists('Users\User') && Users\User::$cur->id) {
57
                $subscriber = \Notifications\Subscriber::get(Users\User::$cur->id, 'user_id');
58
            }
59 2
            if (!$subscriber) {
60 2
                $subscriber = new \Notifications\Subscriber();
61 2
                if (class_exists('Users\User') && Users\User::$cur->id) {
62
                    $subscriber->user_id = Users\User::$cur->id;
63
                }
64 2
                $subscriber->save(['empty' => true]);
65 2
            }
66 2
            $device->subscriber_id = $subscriber->id;
67 2
            $device->save();
68 2
            return $subscriber;
69
        }
70
71 2
        return $device->subscriber;
72
    }
73
74 3
    public function getCurDevice($create = false) {
75 3
        $deviceKey = $this->getDevicekey();
76 3
        if (!$deviceKey) {
77 3
            if ($create) {
78 3
                $deviceKey = Tools::randomString(70);
79 3
                $this->setDeviceKey($deviceKey);
80 3
            } else {
81 2
                return false;
82
            }
83 3
        } else {
84 3
            $this->setDeviceKey($deviceKey);
85
        }
86 3
        $device = \Notifications\Subscriber\Device::get($deviceKey, 'key');
87 3
        if (!$device && $create) {
88 3
            $device = new \Notifications\Subscriber\Device();
89 3
            $device->key = $deviceKey;
90 3
            $device->save();
91 3
            $device->date_last_check = $device->date_create;
92 3
            $device->save();
93 3
        } elseif (!$device) {
94 1
            return false;
95
        }
96 3
        return $device;
97
    }
98
99 3
    public function getDevicekey() {
2 ignored issues
show
Coding Style introduced by
getDevicekey uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getDevicekey uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100 3
        if (!empty($_SESSION['notification-device'])) {
101 3
            return $_SESSION['notification-device'];
102
        }
103 3
        if (!empty($_COOKIE['notification-device'])) {
104
            return $_COOKIE['notification-device'];
105
        }
106 3
    }
107
108 3
    public function setDeviceKey($deviceKey) {
1 ignored issue
show
Coding Style introduced by
setDeviceKey uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
109 3
        if (headers_sent()) {
110 3
            $_SESSION['notification-device'] = $deviceKey;
111 3
        } else {
112
            setcookie("notification-device", $deviceKey, time() + 360000, "/");
113
        }
114
    }
115
}