Passed
Push — devel-3.0 ( 543c48...5b8639 )
by Rubén
03:28
created

AccountToUserGroupRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserGroupsByAccountId() 0 15 1
A getUserGroupsByUserGroupId() 0 15 1
A deleteByUserGroupId() 0 8 1
A deleteTypeByAccountId() 0 8 1
A deleteByAccountId() 0 8 1
A updateByType() 0 5 1
A addByType() 0 25 3
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, 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\Repositories\Account;
26
27
use SP\DataModel\ItemData;
28
use SP\Repositories\Repository;
29
use SP\Repositories\RepositoryItemTrait;
30
use SP\Services\Account\AccountRequest;
31
use SP\Storage\Database\QueryData;
32
33
/**
34
 * Class AccountToUserGroupRepository
35
 *
36
 * @package SP\Repositories\Account
37
 */
38
final class AccountToUserGroupRepository extends Repository
39
{
40
    use RepositoryItemTrait;
41
42
    /**
43
     * Obtiene el listado con el nombre de los grupos de una cuenta.
44
     *
45
     * @param int $id con el Id de la cuenta
46
     *
47
     * @return \SP\Storage\Database\QueryResult
48
     * @throws \SP\Core\Exceptions\ConstraintException
49
     * @throws \SP\Core\Exceptions\QueryException
50
     */
51
    public function getUserGroupsByAccountId($id)
52
    {
53
        $query = /** @lang SQL */
54
            'SELECT UserGroup.id, UserGroup.name, AccountToUserGroup.isEdit
55
            FROM AccountToUserGroup
56
            INNER JOIN UserGroup ON AccountToUserGroup.userGroupId = UserGroup.id
57
            WHERE AccountToUserGroup.accountId = ?
58
            ORDER BY UserGroup.name';
59
60
        $queryData = new QueryData();
61
        $queryData->setQuery($query);
62
        $queryData->addParam($id);
63
        $queryData->setMapClassName(ItemData::class);
64
65
        return $this->db->doSelect($queryData);
66
    }
67
68
    /**
69
     * Obtiene el listado con el nombre de los grupos de una cuenta.
70
     *
71
     * @param $id
72
     *
73
     * @return \SP\Storage\Database\QueryResult
74
     * @throws \SP\Core\Exceptions\ConstraintException
75
     * @throws \SP\Core\Exceptions\QueryException
76
     */
77
    public function getUserGroupsByUserGroupId($id)
78
    {
79
        $query = /** @lang SQL */
80
            'SELECT UserGroup.id, UserGroup.name, AccountToUserGroup.isEdit
81
            FROM AccountToUserGroup
82
            INNER JOIN UserGroup ON AccountToUserGroup.userGroupId = UserGroup.id
83
            WHERE AccountToUserGroup.userGroupId = ?
84
            ORDER BY UserGroup.name';
85
86
        $queryData = new QueryData();
87
        $queryData->setQuery($query);
88
        $queryData->addParam($id);
89
        $queryData->setMapClassName(ItemData::class);
90
91
        return $this->db->doSelect($queryData);
92
    }
93
94
    /**
95
     * @param $id int
96
     *
97
     * @return int
98
     * @throws \SP\Core\Exceptions\ConstraintException
99
     * @throws \SP\Core\Exceptions\QueryException
100
     */
101
    public function deleteByUserGroupId($id)
102
    {
103
        $queryData = new QueryData();
104
        $queryData->setQuery('DELETE FROM AccountToUserGroup WHERE userGroupId = ?');
105
        $queryData->addParam($id);
106
        $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta'));
107
108
        return $this->db->doQuery($queryData)->getAffectedNumRows();
109
    }
110
111
    /**
112
     * @param AccountRequest $accountRequest
113
     * @param bool           $isEdit
114
     *
115
     * @return bool
116
     * @throws \SP\Core\Exceptions\ConstraintException
117
     * @throws \SP\Core\Exceptions\QueryException
118
     */
119
    public function updateByType(AccountRequest $accountRequest, bool $isEdit)
120
    {
121
        $this->deleteTypeByAccountId($accountRequest->id, $isEdit);
122
123
        return $this->addByType($accountRequest, $isEdit);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addByType($accountRequest, $isEdit) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
124
    }
125
126
    /**
127
     * @param int  $id
128
     * @param bool $isEdit
129
     *
130
     * @return int
131
     * @throws \SP\Core\Exceptions\ConstraintException
132
     * @throws \SP\Core\Exceptions\QueryException
133
     */
134
    public function deleteTypeByAccountId($id, bool $isEdit)
135
    {
136
        $queryData = new QueryData();
137
        $queryData->setQuery('DELETE FROM AccountToUserGroup WHERE accountId = ? AND isEdit = ?');
138
        $queryData->setParams([$id, (int)$isEdit]);
139
        $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta'));
140
141
        return $this->db->doQuery($queryData)->getAffectedNumRows();
142
    }
143
144
    /**
145
     * @param AccountRequest $accountRequest
146
     * @param bool           $isEdit
147
     *
148
     * @return int Last ID inserted
149
     * @throws \SP\Core\Exceptions\ConstraintException
150
     * @throws \SP\Core\Exceptions\QueryException
151
     */
152
    public function addByType(AccountRequest $accountRequest, bool $isEdit)
153
    {
154
        $items = $isEdit ? $accountRequest->userGroupsEdit : $accountRequest->userGroupsView;
155
        $values = $this->getParamsFromArray($items, '(?,?,?)');
156
157
        $query = /** @lang SQL */
158
            'INSERT INTO AccountToUserGroup (accountId, userGroupId, isEdit) 
159
              VALUES ' . $values . '
160
              ON DUPLICATE KEY UPDATE isEdit = ' . (int)$isEdit;
161
162
        $queryData = new QueryData();
163
        $queryData->setQuery($query);
164
        $queryData->setOnErrorMessage(__u('Error al actualizar los grupos secundarios'));
165
166
        $params = [];
167
168
        foreach ($items as $userGroup) {
169
            $params[] = $accountRequest->id;
170
            $params[] = $userGroup;
171
            $params[] = (int)$isEdit;
172
        }
173
174
        $queryData->setParams($params);
175
176
        return $this->db->doQuery($queryData)->getAffectedNumRows();
177
    }
178
179
    /**
180
     * @param $id int
181
     *
182
     * @return int
183
     * @throws \SP\Core\Exceptions\ConstraintException
184
     * @throws \SP\Core\Exceptions\QueryException
185
     */
186
    public function deleteByAccountId($id)
187
    {
188
        $queryData = new QueryData();
189
        $queryData->setQuery('DELETE FROM AccountToUserGroup WHERE accountId = ?');
190
        $queryData->addParam($id);
191
        $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta'));
192
193
        return $this->db->doQuery($queryData)->getAffectedNumRows();
194
    }
195
}