UpgradeAuthToken::actionMapper()   D
last analyzed

Complexity

Conditions 23
Paths 23

Size

Total Lines 50
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 46
c 1
b 0
f 0
nc 23
nop 1
dl 0
loc 50
rs 4.1666

How to fix   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
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Services\Upgrade;
26
27
use Exception;
28
use SP\Core\Acl\ActionsInterface;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventMessage;
31
use SP\Services\AuthToken\AuthTokenService;
32
use SP\Services\Service;
33
34
35
/**
36
 * Class UpgradeAuthToken
37
 *
38
 * @package SP\Services\Upgrade
39
 */
40
final class UpgradeAuthToken extends Service
41
{
42
    /**
43
     * @var AuthTokenService
44
     */
45
    private $authtokenService;
46
47
    /**
48
     * upgrade_300_18072901
49
     *
50
     * @throws Exception
51
     */
52
    public function upgrade_300_18072901()
53
    {
54
        $this->eventDispatcher->notifyEvent('upgrade.authToken.start',
55
            new Event($this, EventMessage::factory()
56
                ->addDescription(__u('API authorizations update'))
57
                ->addDescription(__FUNCTION__))
58
        );
59
60
        try {
61
            $this->transactionAware(function () {
62
                foreach ($this->authtokenService->getAllBasic() as $item) {
63
64
                    $itemData = clone $item;
65
                    $itemData->setActionId($this->actionMapper($item->getActionId()));
66
67
                    $this->authtokenService->updateRaw($itemData);
68
69
                    $this->eventDispatcher->notifyEvent('upgrade.authToken.process',
70
                        new Event($this, EventMessage::factory()
71
                            ->addDescription(__u('Authorization updated'))
72
                            ->addDetail(__u('Authorization'), $item->getId()))
73
                    );
74
                }
75
            });
76
        } catch (Exception $e) {
77
            processException($e);
78
79
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
80
81
            throw $e;
82
        }
83
84
        $this->eventDispatcher->notifyEvent('upgrade.authToken.end',
85
            new Event($this, EventMessage::factory()
86
                ->addDescription(__u('API authorizations update'))
87
                ->addDescription(__FUNCTION__))
88
        );
89
    }
90
91
    /**
92
     * @param int $moduleId
93
     *
94
     * @return int
95
     */
96
    private function actionMapper(int $moduleId)
97
    {
98
        switch ($moduleId) {
99
            case 1:
100
                return ActionsInterface::ACCOUNT_SEARCH;
101
            case 100:
102
                return ActionsInterface::ACCOUNT_VIEW;
103
            case 104:
104
                return ActionsInterface::ACCOUNT_VIEW_PASS;
105
            case 103:
106
                return ActionsInterface::ACCOUNT_DELETE;
107
            case 101:
108
                return ActionsInterface::ACCOUNT_CREATE;
109
            case 615:
110
                return ActionsInterface::CATEGORY_SEARCH;
111
            case 610:
112
                return ActionsInterface::CATEGORY_VIEW;
113
            case 611:
114
                return ActionsInterface::CATEGORY_CREATE;
115
            case 612:
116
                return ActionsInterface::CATEGORY_EDIT;
117
            case 613:
118
                return ActionsInterface::CATEGORY_DELETE;
119
            case 625:
120
                return ActionsInterface::CLIENT_SEARCH;
121
            case 620:
122
                return ActionsInterface::CLIENT_VIEW;
123
            case 621:
124
                return ActionsInterface::CLIENT_CREATE;
125
            case 622:
126
                return ActionsInterface::CLIENT_EDIT;
127
            case 623:
128
                return ActionsInterface::CLIENT_DELETE;
129
            case 685:
130
                return ActionsInterface::TAG_SEARCH;
131
            case 681:
132
                return ActionsInterface::TAG_VIEW;
133
            case 680:
134
                return ActionsInterface::TAG_CREATE;
135
            case 682:
136
                return ActionsInterface::TAG_EDIT;
137
            case 683:
138
                return ActionsInterface::TAG_DELETE;
139
            case 1041:
140
                return ActionsInterface::CONFIG_BACKUP_RUN;
141
            case 1061:
142
                return ActionsInterface::CONFIG_EXPORT_RUN;
143
        }
144
145
        return $moduleId;
146
    }
147
148
    protected function initialize()
149
    {
150
        $this->authtokenService = $this->dic->get(AuthTokenService::class);
151
    }
152
}