Passed
Push — devel-3.0 ( 5f7f30...cd1038 )
by Rubén
03:44
created

AccountDefaultPermissionService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A getForUser() 0 13 2
A delete() 0 7 2
A getById() 0 12 2
A update() 0 3 1
A initialize() 0 3 1
A getAll() 0 3 1
A search() 0 3 1
A deleteByIdBatch() 0 7 2
A getForCurrentUser() 0 5 1
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\Services\Account;
26
27
use SP\DataModel\AccountDefaultPermissionData;
28
use SP\DataModel\ItemSearchData;
29
use SP\Repositories\Account\AccountDefaultPermissionRepository;
30
use SP\Repositories\NoSuchItemException;
31
use SP\Services\Service;
32
use SP\Services\ServiceException;
33
use SP\Storage\Database\QueryResult;
34
35
/**
36
 * Class AccountDefaultPermissionService
37
 *
38
 * @package SP\Services\Account
39
 */
40
class AccountDefaultPermissionService extends Service
41
{
42
    /**
43
     * @var AccountDefaultPermissionRepository
44
     */
45
    private $accountDefaultPermissionRepository;
46
47
    /**
48
     * @param AccountDefaultPermissionData $accountDefaultPermissionData
49
     *
50
     * @return int
51
     * @throws \SP\Core\Exceptions\ConstraintException
52
     * @throws \SP\Core\Exceptions\QueryException
53
     */
54
    public function create(AccountDefaultPermissionData $accountDefaultPermissionData)
55
    {
56
        return $this->accountDefaultPermissionRepository->create($accountDefaultPermissionData);
57
    }
58
59
    /**
60
     * @param AccountDefaultPermissionData $accountDefaultPermissionData
61
     *
62
     * @return int
63
     * @throws \SP\Core\Exceptions\ConstraintException
64
     * @throws \SP\Core\Exceptions\QueryException
65
     */
66
    public function update(AccountDefaultPermissionData $accountDefaultPermissionData)
67
    {
68
        return $this->accountDefaultPermissionRepository->update($accountDefaultPermissionData);
69
    }
70
71
    /**
72
     * Deletes an item
73
     *
74
     * @param $id
75
     *
76
     * @return AccountDefaultPermissionService
77
     * @throws NoSuchItemException
78
     * @throws \SP\Core\Exceptions\ConstraintException
79
     * @throws \SP\Core\Exceptions\QueryException
80
     */
81
    public function delete($id)
82
    {
83
        if ($this->accountDefaultPermissionRepository->delete($id) === 0) {
84
            throw new NoSuchItemException(__u('Permiso no encontrada'));
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Returns the item for given id
92
     *
93
     * @param int $id
94
     *
95
     * @return AccountDefaultPermissionData
96
     * @throws NoSuchItemException
97
     * @throws \SP\Core\Exceptions\ConstraintException
98
     * @throws \SP\Core\Exceptions\QueryException
99
     */
100
    public function getById($id)
101
    {
102
        $result = $this->accountDefaultPermissionRepository->getById($id);
103
104
        if ($result->getNumRows() === 0) {
105
            throw new NoSuchItemException(__u('Permiso no encontrada'));
106
        }
107
108
        /** @var AccountDefaultPermissionData $data */
109
        $data = $result->getData();
110
111
        return $data->hydrate();
112
    }
113
114
    /**
115
     * Returns all the items
116
     *
117
     * @return AccountDefaultPermissionData[]
118
     * @throws \SP\Core\Exceptions\ConstraintException
119
     * @throws \SP\Core\Exceptions\QueryException
120
     */
121
    public function getAll()
122
    {
123
        return $this->accountDefaultPermissionRepository->getAll()->getDataAsArray();
124
    }
125
126
    /**
127
     * Searches for items by a given filter
128
     *
129
     * @param ItemSearchData $itemSearchData
130
     *
131
     * @return QueryResult
132
     * @throws \SP\Core\Exceptions\ConstraintException
133
     * @throws \SP\Core\Exceptions\QueryException
134
     */
135
    public function search(ItemSearchData $itemSearchData)
136
    {
137
        return $this->accountDefaultPermissionRepository->search($itemSearchData);
138
    }
139
140
    /**
141
     * @return AccountDefaultPermissionData
142
     * @throws \SP\Core\Exceptions\ConstraintException
143
     * @throws \SP\Core\Exceptions\QueryException
144
     */
145
    public function getForCurrentUser()
146
    {
147
        $userData = $this->context->getUserData();
148
149
        return $this->getForUser($userData->getId(), $userData->getUserGroupId(), $userData->getUserProfileId());
150
    }
151
152
    /**
153
     * @param int $userId
154
     * @param int $userGroupId
155
     * @param int $userProfileId
156
     *
157
     * @return AccountDefaultPermissionData
158
     * @throws \SP\Core\Exceptions\ConstraintException
159
     * @throws \SP\Core\Exceptions\QueryException
160
     */
161
    public function getForUser(int $userId, int $userGroupId, int $userProfileId)
162
    {
163
        $result = $this->accountDefaultPermissionRepository->getByFilter(
164
            $userId,
165
            $userGroupId,
166
            $userProfileId
167
        );
168
169
        if ($result->getNumRows() === 1) {
170
            return $result->getData()->hydrate();
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * @param array $ids
178
     *
179
     * @return int
180
     * @throws ServiceException
181
     * @throws \SP\Core\Exceptions\ConstraintException
182
     * @throws \SP\Core\Exceptions\QueryException
183
     */
184
    public function deleteByIdBatch(array $ids)
185
    {
186
        if (($count = $this->accountDefaultPermissionRepository->deleteByIdBatch($ids)) !== count($ids)) {
187
            throw new ServiceException(__u('Error al eliminar los permisos'), ServiceException::WARNING);
188
        }
189
190
        return $count;
191
    }
192
193
    protected function initialize()
194
    {
195
        $this->accountDefaultPermissionRepository = $this->dic->get(AccountDefaultPermissionRepository::class);
196
    }
197
}