PageBreakReservation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 34
c 0
b 0
f 0
dl 0
loc 77
ccs 0
cts 48
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 22 4
A doBreakReserve() 0 23 2
A doUnreserve() 0 12 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages\RequestAction;
11
12
use Exception;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\AccessDeniedException;
16
use Waca\Exceptions\ApplicationLogicException;
17
use Waca\Helpers\Logger;
18
use Waca\PdoDatabase;
19
use Waca\WebRequest;
20
21
class PageBreakReservation extends RequestActionBase
22
{
23
    protected function main()
24
    {
25
        $this->checkPosted();
26
        $database = $this->getDatabase();
27
        $request = $this->getRequest($database);
28
29
        if ($request->getReserved() === null) {
30
            throw new ApplicationLogicException('Request is not reserved!');
31
        }
32
33
        $currentUser = User::getCurrent($database);
34
35
        if ($currentUser->getId() === $request->getReserved()) {
36
            $this->doUnreserve($request, $database);
37
        }
38
        else {
39
            // not the same user!
40
            if ($this->barrierTest('force', $currentUser)) {
41
                $this->doBreakReserve($request, $database);
42
            }
43
            else {
44
                throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
45
            }
46
        }
47
    }
48
49
    /**
50
     * @param Request     $request
51
     * @param PdoDatabase $database
52
     *
53
     * @throws Exception
54
     */
55
    protected function doUnreserve(Request $request, PdoDatabase $database)
56
    {
57
        // same user! we allow people to unreserve their own stuff
58
        $request->setReserved(null);
59
        $request->setUpdateVersion(WebRequest::postInt('updateversion'));
60
        $request->save();
61
62
        Logger::unreserve($database, $request);
63
        $this->getNotificationHelper()->requestUnreserved($request);
64
65
        // Redirect home!
66
        $this->redirect();
67
    }
68
69
    /**
70
     * @param Request     $request
71
     * @param PdoDatabase $database
72
     *
73
     * @throws Exception
74
     */
75
    protected function doBreakReserve(Request $request, PdoDatabase $database)
76
    {
77
        if (!WebRequest::postBoolean("confirm")) {
78
            $this->assignCSRFToken();
79
80
            $this->assign("request", $request->getId());
81
            $this->assign("reservedUser", User::getById($request->getReserved(), $database));
82
            $this->assign("updateversion", WebRequest::postInt('updateversion'));
83
84
            $this->skipAlerts();
85
86
            $this->setTemplate("confirmations/breakreserve.tpl");
87
        }
88
        else {
89
            $request->setReserved(null);
90
            $request->setUpdateVersion(WebRequest::postInt('updateversion'));
91
            $request->save();
92
93
            Logger::breakReserve($database, $request);
94
            $this->getNotificationHelper()->requestReserveBroken($request);
95
96
            // Redirect home!
97
            $this->redirect();
98
        }
99
    }
100
}
101