Passed
Push — devel-3.0 ( 7ceb3a...2fc71e )
by Rubén
03:58
created

ItemPresetService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

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

10 Methods

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