Passed
Push — master ( 4be74a...8532bf )
by Goffy
03:34
created

TaskHandler::processTasks()   C

Complexity

Conditions 16
Paths 12

Size

Total Lines 74
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 16
eloc 53
c 1
b 0
f 1
nc 12
nop 1
dl 0
loc 74
rs 5.5666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace XoopsModules\Wgevents;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
*/
17
18
/**
19
 * wgEvents module for xoops
20
 *
21
 * @copyright    2021 XOOPS Project (https://xoops.org)
22
 * @license      GPL 2.0 or later
23
 * @package      wgevents
24
 * @since        1.0.0
25
 * @min_xoops    2.5.11 Beta1
26
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
27
 */
28
29
use XoopsModules\Wgevents;
30
use XoopsModules\Wgevents\{
31
    Constants,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Wgevents\Constants. Consider defining an alias.

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...
32
    MailHandler
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Wgevents\MailHandler. Consider defining an alias.

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...
33
};
34
35
/**
36
 * Class Object Handler Task
37
 */
38
class TaskHandler extends \XoopsPersistableObjectHandler
39
{
40
    /**
41
     * Constructor
42
     *
43
     * @param \XoopsDatabase $db
44
     */
45
    public function __construct(\XoopsDatabase $db)
46
    {
47
        parent::__construct($db, 'wgevents_task', Task::class, 'id', 'id');
48
    }
49
50
    /**
51
     * @param bool $isNew
52
     *
53
     * @return object
54
     */
55
    public function create($isNew = true)
56
    {
57
        return parent::create($isNew);
58
    }
59
60
    /**
61
     * retrieve a field
62
     *
63
     * @param int $id field id
64
     * @param null fields
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wgevents\fields 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...
65
     * @return \XoopsObject|null reference to the {@link Get} object
66
     */
67
    public function get($id = null, $fields = null)
68
    {
69
        return parent::get($id, $fields);
70
    }
71
72
    /**
73
     * get inserted id
74
     *
75
     * @param null
76
     * @return int reference to the {@link Get} object
77
     */
78
    public function getInsertId()
79
    {
80
        return $this->db->getInsertId();
81
    }
82
83
    /**
84
     * Get Count Task in the database
85
     * @param int    $start
86
     * @param int    $limit
87
     * @param string $sort
88
     * @param string $order
89
     * @return int
90
     */
91
    public function getCountTasks($start = 0, $limit = 0, $sort = 'id', $order = 'ASC')
92
    {
93
        $crCountTasks = new \CriteriaCompo();
94
        $crCountTasks = $this->getTasksCriteria($crCountTasks, $start, $limit, $sort, $order);
95
        return $this->getCount($crCountTasks);
0 ignored issues
show
Bug introduced by
$crCountTasks of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getCount(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountTasks);
Loading history...
96
    }
97
98
    /**
99
     * Get All Task in the database
100
     * @param int    $start
101
     * @param int    $limit
102
     * @param string $sort
103
     * @param string $order
104
     * @return array
105
     */
106
    public function getAllTasks($start = 0, $limit = 0, $sort = 'id', $order = 'ASC')
107
    {
108
        $crAllTasks = new \CriteriaCompo();
109
        $crAllTasks = $this->getTasksCriteria($crAllTasks, $start, $limit, $sort, $order);
110
        return $this->getAll($crAllTasks);
0 ignored issues
show
Bug introduced by
$crAllTasks of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getAll(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        return $this->getAll(/** @scrutinizer ignore-type */ $crAllTasks);
Loading history...
111
    }
112
113
    /**
114
     * Get Criteria Task
115
     * @param        $crTasks
116
     * @param int    $start
117
     * @param int    $limit
118
     * @param string $sort
119
     * @param string $order
120
     * @return int
121
     */
122
    private function getTasksCriteria($crTasks, $start, $limit, $sort, $order)
123
    {
124
        $crTasks->setStart($start);
125
        $crTasks->setLimit($limit);
126
        $crTasks->setSort($sort);
127
        $crTasks->setOrder($order);
128
        return $crTasks;
129
    }
130
131
    /**
132
     * Create a task
133
     * @param $type
134
     * @param $recipient
135
     * @param $params
136
     * @return bool
137
     */
138
    public function createTask($type, $recipient, $params)
139
    {
140
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? (int)$GLOBALS['xoopsUser']->uid() : 0;
141
142
        $taskObj = $this->create();
143
        // Set Vars
144
        $taskObj->setVar('type', $type);
145
        $taskObj->setVar('params', $params);
146
        $taskObj->setVar('recipient', $recipient);
147
        $taskObj->setVar('datecreated', time());
148
        $taskObj->setVar('submitter', $uidCurrent);
149
        $taskObj->setVar('status', Constants::STATUS_PENDING);
150
151
        // Insert Data
152
        return (bool)$this->insert($taskObj);
153
    }
154
155
    /**
156
     * process all task if limit is not exceeded
157
     * @param $log_level
158
     * @return bool
159
     */
160
    public function processTasks($log_level = 0)
161
    {
162
        $helper = \XoopsModules\Wgevents\Helper::getInstance();
163
164
        // get limit_hour from primary account
165
        $accountHandler = $helper->getHandler('Account');
166
        $limitHour = $accountHandler->getLimitHour();
167
168
        $crTaskPending = new \CriteriaCompo();
169
        $crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING));
170
        $crTaskDone = new \CriteriaCompo();
171
        $crTaskDone->add(new \Criteria('status', Constants::STATUS_DONE));
172
        $crTaskDone->add(new \Criteria('datedone', time() - 3600, '>'));
173
        $tasksCountPending = $this->getCount($crTaskPending);
174
        $tasksCountDone = $this->getCount($crTaskDone);
175
        $counterDone = 0;
176
        if ($log_level > 0) {
177
            echo '<br>Sart processTasks';
178
            echo '<br>time - 3600: ' . \formatTimestamp(time() - 3600, 'm');
179
            echo '<br>tasksCountPending: ' . $tasksCountPending;
180
            echo '<br>tasksCountDone: ' . $tasksCountDone;
181
        }
182
        if (($tasksCountPending > 0) && ($tasksCountDone < $limitHour || 0 == $limitHour)) {
183
            if ($limitHour > 0) {
184
                $crTaskPending->setLimit($limitHour);
185
            }
186
            $tasksAll = $this->getAll($crTaskPending);
187
            foreach (\array_keys($tasksAll) as $i) {
188
                // check whether task is still pending
189
                // ignore it if meanwhile another one started to process the task
190
                if ($log_level > 0) {
191
                    echo '<br>tasksAll key: ' . $i;
192
                }
193
                if ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status'))
194
                    && ($tasksCountDone < $limitHour || 0 == $limitHour)) {
195
                    $taskProcessObj = $this->get($i);
196
                    $taskProcessObj->setVar('status', Constants::STATUS_PROCESSING);
197
                    if ($this->insert($taskProcessObj)) {
198
                        $mailsHandler = new MailHandler();
199
                        $mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true);
200
                        $mailParams['recipients'] = $tasksAll[$i]->getVar('recipient');
201
                        $mailParams['taskId'] = $i;
202
                        $mailsHandler->setParams($mailParams);
203
                        $mailsHandler->setType($tasksAll[$i]->getVar('type'));
204
                        // send mails
205
                        $result = $mailsHandler->execute();
206
                        unset($mailsHandler);
207
                        //update task list corresponding the result
208
                        if ($result) {
209
                            $taskProcessObj->setVar('status', Constants::STATUS_DONE);
210
                            $taskProcessObj->setVar('datedone', time());
211
                            $counterDone++;
212
                            if ($log_level > 0) {
213
                                echo ' - done';
214
                            }
215
                        } else {
216
                            $taskProcessObj->setVar('status', Constants::STATUS_PENDING);
217
                            if ($log_level > 0) {
218
                                echo ' - failed';
219
                            }
220
                        }
221
                        $this->insert($taskProcessObj);
222
                    } else {
223
                        echo ' - skipped';
224
                    }
225
                }
226
                // check once more number of done
227
                $tasksCountDone = $this->getCount($crTaskDone);
228
            }
229
        }
230
        if ($log_level > 0) {
231
            echo '<br>End processTasks';
232
        }
233
        return ['pending' => $tasksCountPending, 'done' => $counterDone];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('pending' =...'done' => $counterDone) returns the type array<string,integer> which is incompatible with the documented return type boolean.
Loading history...
234
    }
235
}
236