StaffService   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
c 0
b 0
f 0
dl 0
loc 213
rs 10
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A batch_response() 0 14 3
A delete_staff() 0 5 1
A new_response() 0 13 3
A attachEvents() 0 10 1
A new_response_rating() 0 7 1
A __construct() 0 5 1
A reopen_ticket() 0 9 2
A view_ticket() 0 18 2
A uniqueArray() 0 13 3
A batch_status() 0 8 2
A close_ticket() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
use Xmf\Request;
23
24
/**
25
 * xhelp_staffService class
26
 *
27
 * Part of the Messaging Subsystem.  Updates staff member information.
28
 *
29
 *
30
 * @author  Brian Wahoff <[email protected]>
31
 */
32
class StaffService extends Service
33
{
34
    /**
35
     * Instance of the xoopsStaffHandler
36
     *
37
     * @var object
38
     */
39
    public $staffHandler;
40
41
    /**
42
     * Class Constructor
43
     */
44
    public function __construct()
45
    {
46
        $this->helper       = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
47
        $this->staffHandler = $this->helper->getHandler('Staff');
48
        $this->init();
49
    }
50
51
    /**
52
     * Update staff response time if first staff response
53
     * @param Ticket   $ticket   Ticket for response
54
     * @param Response $response Response
55
     */
56
    public function new_response(Ticket $ticket, Response $response): void
57
    {
58
        global $xoopsUser;
59
60
        //if first response for ticket, update staff responsetime
61
        /** @var \XoopsModules\Xhelp\ResponseHandler $responseHandler */
62
        $responseHandler = $this->helper->getHandler('Response');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
63
        /** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */
64
        $membershipHandler = $this->helper->getHandler('Membership');
65
        if (1 == $responseHandler->getStaffResponseCount($ticket->getVar('id'))) {
66
            if ($membershipHandler->isStaffMember($response->getVar('uid'), $ticket->getVar('department'))) {
67
                $responseTime = \abs($response->getVar('updateTime') - $ticket->getVar('posted'));
68
                $this->staffHandler->updateResponseTime($response->getVar('uid'), $responseTime);
69
            }
70
        }
71
    }
72
73
    /**
74
     * Update staff response time if first staff response
75
     * @param array    $tickets
76
     * @param Response $response Response
77
     * @internal param Ticket $ticket Ticket for response
78
     * @internal param int $timespent Number of minutes spent on ticket
79
     * @internal param bool $private Is the response private?
80
     */
81
    public function batch_response(array $tickets, Response $response): void
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

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

81
    public function batch_response(array $tickets, /** @scrutinizer ignore-unused */ Response $response): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        global $xoopsUser;
84
85
        $update          = \time();
86
        $uid             = $xoopsUser->getVar('uid');
87
        $responseHandler = $this->helper->getHandler('Response');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
88
        foreach ($tickets as $ticket) {
89
            //if first response for ticket, update staff responsetime
90
91
            $membershipHandler = $this->helper->getHandler('Membership');
0 ignored issues
show
Unused Code introduced by
The assignment to $membershipHandler is dead and can be removed.
Loading history...
92
            if (1 == $responseHandler->getStaffResponseCount($ticket->getVar('id'))) {
93
                $responseTime = \abs($update - $ticket->getVar('posted'));
94
                $this->staffHandler->updateResponseTime($uid, $responseTime);
95
            }
96
        }
97
    }
98
99
    /**
100
     * Handler for the 'batch_status' event
101
     * @param array  $tickets   Array of Ticket objects
102
     * @param Status $newstatus New Status of all tickets
103
     */
104
    public function batch_status(array $tickets, Status $newstatus): void
105
    {
106
        global $xoopsUser;
107
108
        $uid = $xoopsUser->getVar('uid');
109
110
        if (\XHELP_STATE_RESOLVED == $newstatus->getVar('state')) {
111
            $this->staffHandler->increaseCallsClosed($uid, \count($tickets));
112
        }
113
    }
114
115
    /**
116
     * Callback function for the 'close_ticket' event
117
     * @param Ticket $ticket Closed ticket
118
     * @return bool        True on success, false on error
119
     */
120
    public function close_ticket(Ticket $ticket): bool
121
    {
122
        global $xoopsUser;
123
124
        /** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */
125
        $membershipHandler = $this->helper->getHandler('Membership');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
126
        if ($membershipHandler->isStaffMember($ticket->getVar('closedBy'), $ticket->getVar('department'))) {
127
            $this->staffHandler->increaseCallsClosed($ticket->getVar('closedBy'), 1);
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Callback function for the 'reopen_ticket' event
135
     * @param Ticket $ticket
136
     * @return bool True on success, false on error
137
     * @internal param array $args Array of arguments passed to EventService
138
     */
139
    public function reopen_ticket(Ticket $ticket): bool
140
    {
141
        /** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */
142
        $membershipHandler = $this->helper->getHandler('Membership');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
143
        if ($membershipHandler->isStaffMember($ticket->getVar('closedBy'), $ticket->getVar('department'))) {
144
            $this->staffHandler->increaseCallsClosed($ticket->getVar('closedBy'), -1);
145
        }
146
147
        return true;
148
    }
149
150
    /**
151
     * Callback function for the 'new_response_rating' event
152
     * @param \XoopsModules\Xhelp\StaffReview $rating   Rating
153
     * @param Ticket                          $ticket   Ticket that was rated
154
     * @param Response                        $response Response that was rated
155
     * @return bool          True on success, false on error
156
     */
157
    public function new_response_rating(StaffReview $rating, Ticket $ticket, Response $response): bool
0 ignored issues
show
Unused Code introduced by
The parameter $ticket is not used and could be removed. ( Ignorable by Annotation )

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

157
    public function new_response_rating(StaffReview $rating, /** @scrutinizer ignore-unused */ Ticket $ticket, Response $response): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

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

157
    public function new_response_rating(StaffReview $rating, Ticket $ticket, /** @scrutinizer ignore-unused */ Response $response): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
    {
159
        global $xoopsUser;
160
161
        $staffHandler = $this->helper->getHandler('Staff');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
162
163
        return $staffHandler->updateRating($rating->getVar('staffid'), $rating->getVar('rating'));
164
    }
165
166
    /**
167
     * Event Handler for 'view_ticket'
168
     * @param Ticket $ticket Ticket being viewd
169
     */
170
    public function view_ticket(Ticket $ticket): void
171
    {
172
        $value = [];
173
174
        //Store a list of recent tickets in the xhelp_recent_tickets cookie
175
        if (Request::hasVar('xhelp_recent_tickets', 'COOKIE')) {
176
            $oldvalue = \explode(',', $_COOKIE['xhelp_recent_tickets']);
177
        } else {
178
            $oldvalue = [];
179
        }
180
181
        $value[] = $ticket->getVar('id');
182
183
        $value = \array_merge($value, $oldvalue);
184
        $value = $this->uniqueArray($value);
185
        $value = \array_slice($value, 0, 5);
186
        //Keep this value for 15 days
187
        setcookie('xhelp_recent_tickets', \implode(',', $value), \time() + 15 * 24 * 60 * 60, '/');
188
    }
189
190
    /**
191
     * Event Handler for 'delete_staff' event
192
     * @param Staff $staff Staff member being deleted
193
     * @return bool       True on success, false on error
194
     */
195
    public function delete_staff(Staff $staff): bool
196
    {
197
        $ticketHandler = $this->helper->getHandler('Ticket');
0 ignored issues
show
Bug introduced by
The property helper is declared private in XoopsModules\Xhelp\Service and cannot be accessed from this context.
Loading history...
198
199
        return $ticketHandler->updateAll('ownership', 0, new \Criteria('ownership', $staff->getVar('uid')));
0 ignored issues
show
Bug introduced by
It seems like $staff->getVar('uid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

199
        return $ticketHandler->updateAll('ownership', 0, new \Criteria('ownership', /** @scrutinizer ignore-type */ $staff->getVar('uid')));
Loading history...
200
    }
201
202
    /**
203
     * Only have 1 instance of class used
204
     * @return StaffService {@link StaffService}
205
     */
206
    public static function getInstance(): StaffService
207
    {
208
        static $instance;
209
        if (null === $instance) {
210
            $instance = new static();
211
        }
212
213
        return $instance;
214
    }
215
216
    /**
217
     * @param array $array
218
     * @return array
219
     */
220
    private function uniqueArray(array $array): array
221
    {
222
        $out = [];
223
224
        //    loop through the inbound
225
        foreach ($array as $key => $value) {
226
            //    if the item isn't in the array
227
            if (!\in_array($value, $out)) { //    add it to the array
228
                $out[$key] = $value;
229
            }
230
        }
231
232
        return $out;
233
    }
234
235
    public function attachEvents(): void
236
    {
237
        $this->attachEvent('batch_response', $this);
238
        $this->attachEvent('batch_status', $this);
239
        $this->attachEvent('close_ticket', $this);
240
        $this->attachEvent('delete_staff', $this);
241
        $this->attachEvent('new_response', $this);
242
        $this->attachEvent('new_response_rating', $this);
243
        $this->attachEvent('reopen_ticket', $this);
244
        $this->attachEvent('view_ticket', $this);
245
    }
246
}
247