Completed
Pull Request — master (#474)
by
unknown
13:41
created

NotificationsProvider::__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
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Kernel\CriteriaElement;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaElement.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Xoops\Core\Service\AbstractContract;
14
use Xoops\Core\Service\Response;
15
use Xoops\Core\Service\Contract\NotificationsInterface;
16
17
/**
18
 * Notifications provider for service manager
19
 *
20
 * @category  Module
21
 * @package   Notifications
22
 * @author    Alain91 <[email protected]>
23
 * @copyright XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 * @since     2.6.0
27
 */
28
class NotificationsProvider extends AbstractContract implements NotificationsInterface
29
{
30
	protected $xoops_url;
31
	protected $xoops_upload_url;
32
33
    public function __construct()
34
    {
35
		$this->xoops_url = \XoopsBaseConfig::get('url');
36
		$this->xoops_upload_url = \XoopsBaseConfig::get('uploads-url');
37
    }
38
39
    /**
40
     * getName - get a short name for this service provider. This should be unique within the
41
     * scope of the named service, so using module dirname is suggested.
42
     *
43
     * @return string - a unique name for the service provider
44
     */
45
    public function getName()
46
    {
47
        return 'notifications';
48
    }
49
50
    /**
51
     * getDescription - get human readable description of the service provider
52
     *
53
     * @return string
54
     */
55
    public function getDescription()
56
    {
57
        return 'Traditional XOOPS notifications.';
58
    }
59
60
    public function notifyUser(Response $response, $template_dir, $template, $subject, $tags)
61
    {
62
        $notification = new NotificationsNotification();
63
        $ret = $notification->notifyUser($template_dir, $template, $subject, $tags);
64
        if (!$ret)
65
            $response->setSuccess(false)->addErrorMessage('Unable to notify user');           
66
    }
67
68
    public function getObjectsArray(Response $response, CriteriaElement $criteria = null, $id_as_key = false)
69
    {
70
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71
        $ret = $handler->getObjectsArray($criteria, $id_as_key);
72
        if ($ret)
73
            $response->setValue($ret);
74
        else
75
            $response->setSuccess(false)->addErrorMessage('Unable to getObjectsArray');
76
    }
77
    
78
    public function getNotification(Response $response, $module_id, $category, $item_id, $event, $user_id)
79
    {
80
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
81
        $ret = $handler->getNotification($module_id, $category, $item_id, $event, $user_id);
82
        if ($ret)
83
            $response->setValue($ret);
84
        else
85
            $response->setSuccess(false)->addErrorMessage('Unable to getNotification');
86
    }
87
    
88
    public function isSubscribed(Response $response, $category, $item_id, $event, $module_id, $user_id)
89
    {
90
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
        $ret = $handler->isSubscribed($module_id, $category, $item_id, $event, $user_id);
92
        $response->setValue((int)$ret);
93
    }
94
    
95 View Code Duplication
    public function subscribe(Response $response, $category, $item_id, $events, $mode = null, $module_id = null, $user_id = null)
96
    {
97
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
        $ret = $handler->suscribe($category, $item_id, $events, $mode, $module_id, $user_id);
99
        if (!$ret)
100
            $response->setSuccess(false)->addErrorMessage('Unable to suscribe');
101
    }
102
103
    public function getByUser(Response $response, $user_id)
104
    {
105
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
        $ret = $handler->getByUser($user_id);
107
        $response->setValue($ret);
108
    }
109
    
110
    public function getSubscribedEvents(Response $response, $category, $item_id, $module_id, $user_id)
111
    {
112
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
113
        $ret = $handler->getSubscribedEvents($category, $item_id, $module_id, $user_id);
114
        $response->setValue($ret);
115
    }
116
117
    public function getByItemId(Response $response, $module_id, $item_id, $order = null, $status = null)
118
    {
119
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
120
        $ret = $handler->getByItemId($module_id, $item_id, $order, $status);
121
        $response->setValue($ret);
122
    }
123
    
124 View Code Duplication
    public function triggerEvents(
125
        Response $response,
126
        $category,
127
        $item_id,
128
        $events,
129
        $extra_tags = array(),
130
        $user_list = array(),
131
        $module_id = null,
132
        $omit_user_id = null
133
    ) {
134
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
135
        $ret = $handler->triggerEvents($category, $item_id, $events, $extra_tags, $user_list, $module_id, $omit_user_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
        if (!ret)
137
            $response->setSuccess(false)->addErrorMessage('Unable to triggerEvents');
138
    }
139
    
140
    public function triggerEvent(
141
        Response $response,
142
        $category,
143
        $item_id,
144
        $event,
145
        $extra_tags = array(),
146
        $user_list = array(),
147
        $module_id = null,
148
        $omit_user_id = null
149
    ) {
150
        $helper = Notifications::getInstance();
151
        $handler = $helper->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
152
        if (empty($category) AND empty($module_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
153
            $response->setSuccess(false)->addErrorMessage('Unable to triggerEvent');
154
            return;
155
        }
156
        if (empty($category)) {
157
            $xoops = \Xoops::getInstance();
158
            $module = $xoops->getModuleById($module_id);
159
            $catinfo = $helper->getCommentsCategory($module->getVar('dirname'));
160
            $category = $catinfo['name'];
161
        }
162
        $ret = $handler->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
        if (!ret)
164
            $response->setSuccess(false)->addErrorMessage('Unable to triggerEvent');
165
    }
166
167 View Code Duplication
    public function unsubscribeByUser(Response $response, $user_id)
168
    {
169
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
170
        $ret = $handler->unsubscribeByUser($user_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
171
        if (!ret)
172
            $response->setSuccess(false)->addErrorMessage('Unable to unsubscribeByUser');
173
    }
174
    
175
    public function unsubscribe(Response $response, $category, $item_id, $events, $module_id = null, $user_id = null)
176
    {
177
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
178
        $ret = $handler->unsubscribe($category, $item_id, $events, $module_id, $user_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
        if (!ret)
180
            $response->setSuccess(false)->addErrorMessage('Unable to unsubscribe');
181
    }
182
183 View Code Duplication
    public function unsubscribeByModule(Response $response, $module_id)
184
    {
185
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
186
        $ret = $handler->unsubscribeByModule($module_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
187
        if (!ret)
188
            $response->setSuccess(false)->addErrorMessage('Unable to unsubscribeByModule');
189
    }
190
191 View Code Duplication
    public function unsubscribeByItem(Response $response, $module_id, $category, $item_id)
192
    {
193
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
194
        $ret = $handler->unsubscribeByItem($module_id, $category, $item_id);
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
        if (!ret)
196
            $response->setSuccess(false)->addErrorMessage('Unable to unsubscribeByItem');
197
    }
198
199
    public function doLoginMaintenance(Response $response, $user_id)
200
    {
201
        $handler = Notifications::getInstance()->getHandlerNotification();
0 ignored issues
show
Bug introduced by
The method getHandlerNotification() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
202
        $handler->doLoginMaintenance($user_id);
203
    }
204
    
205
}
206