. */ namespace SP\Infrastructure\User\Repositories; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\UserProfileData; use SP\Domain\User\In\UserProfileRepositoryInterface; use SP\Infrastructure\Common\Repositories\DuplicatedItemException; use SP\Infrastructure\Common\Repositories\Repository; use SP\Infrastructure\Common\Repositories\RepositoryItemTrait; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; /** * Class UserProfileRepository * * @package SP\Infrastructure\User\Repositories */ final class UserProfileRepository extends Repository implements UserProfileRepositoryInterface { use RepositoryItemTrait; /** * Obtener el nombre de los usuarios que usan un perfil. * * @param $id int El id del perfil * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function getUsersForProfile(int $id): QueryResult { $queryData = new QueryData(); $queryData->setQuery('SELECT id, login FROM User WHERE userProfileId = ?'); $queryData->addParam($id); return $this->db->doSelect($queryData); } /** * Deletes an item * * @param int $id * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function delete(int $id): int { $queryData = new QueryData(); $queryData->setQuery('DELETE FROM UserProfile WHERE id = ? LIMIT 1'); $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error while removing the profile')); return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Checks whether the item is in use or not * * @param $id int * * @return bool * @throws ConstraintException * @throws QueryException */ public function checkInUse(int $id): bool { $queryData = new QueryData(); $queryData->setQuery('SELECT userProfileId FROM User WHERE userProfileId = ?'); $queryData->addParam($id); return $this->db->doSelect($queryData)->getNumRows() > 0; } /** * Returns the item for given id * * @param int $id * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function getById(int $id): QueryResult { $queryData = new QueryData(); $queryData->setMapClassName(UserProfileData::class); $queryData->setQuery('SELECT id, `name`, `profile` FROM UserProfile WHERE id = ? LIMIT 1'); $queryData->addParam($id); return $this->db->doSelect($queryData); } /** * Returns all the items * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function getAll(): QueryResult { $queryData = new QueryData(); $queryData->setMapClassName(UserProfileData::class); $queryData->setQuery('SELECT id, `name` FROM UserProfile ORDER BY `name`'); return $this->db->doSelect($queryData); } /** * Returns all the items for given ids * * @param array $ids * * @return \SP\Infrastructure\Database\QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids): QueryResult { if (count($ids) === 0) { return new QueryResult(); } $query = /** @lang SQL */ 'SELECT id, `name` FROM UserProfile WHERE id IN ('.$this->getParamsFromArray($ids).')'; $queryData = new QueryData(); $queryData->setMapClassName(UserProfileData::class); $queryData->setQuery($query); $queryData->setParams($ids); return $this->db->doSelect($queryData); } /** * Deletes all the items for given ids * * @param int[] $ids * * @return int * @throws ConstraintException * @throws QueryException */ public function deleteByIdBatch(array $ids): int { if (count($ids) === 0) { return 0; } $queryData = new QueryData(); $queryData->setQuery('DELETE FROM UserProfile WHERE id IN ('.$this->getParamsFromArray($ids).')'); $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error while removing the profiles')); return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Searches for items by a given filter * * @param ItemSearchData $itemSearchData * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function search(ItemSearchData $itemSearchData): QueryResult { $queryData = new QueryData(); $queryData->setSelect('id, name'); $queryData->setFrom('UserProfile'); $queryData->setOrder('name'); if (!empty($itemSearchData->getSeachString())) { $queryData->setWhere('name LIKE ?'); $search = '%'.$itemSearchData->getSeachString().'%'; $queryData->addParam($search); } $queryData->setLimit( '?,?', [$itemSearchData->getLimitStart(), $itemSearchData->getLimitCount()] ); return $this->db->doSelect($queryData, true); } /** * Creates an item * * @param UserProfileData $itemData * * @return int * @throws ConstraintException * @throws QueryException * @throws DuplicatedItemException */ public function create($itemData): int { if ($this->checkDuplicatedOnAdd($itemData)) { throw new DuplicatedItemException(__u('Duplicated profile name')); } $queryData = new QueryData(); $queryData->setQuery('INSERT INTO UserProfile SET `name` = ?, `profile` = ?'); $queryData->setParams([ $itemData->getName(), serialize($itemData->getProfile()), ]); $queryData->setOnErrorMessage(__u('Error while creating the profile')); return $this->db->doQuery($queryData)->getLastId(); } /** * Checks whether the item is duplicated on adding * * @param UserProfileData $itemData * * @return bool * @throws ConstraintException * @throws QueryException */ public function checkDuplicatedOnAdd($itemData): bool { $queryData = new QueryData(); $queryData->setQuery('SELECT `name` FROM UserProfile WHERE UPPER(`name`) = ?'); $queryData->addParam($itemData->getName()); return $this->db->doQuery($queryData)->getNumRows() > 0; } /** * Updates an item * * @param UserProfileData $itemData * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Infrastructure\Common\Repositories\DuplicatedItemException */ public function update($itemData): int { if ($this->checkDuplicatedOnUpdate($itemData)) { throw new DuplicatedItemException(__u('Duplicated profile name')); } $query = /** @lang SQL */ 'UPDATE UserProfile SET `name` = ?, `profile` = ? WHERE id = ? LIMIT 1'; $queryData = new QueryData(); $queryData->setQuery($query); $queryData->setParams([ $itemData->getName(), serialize($itemData->getProfile()), $itemData->getId(), ]); $queryData->setOnErrorMessage(__u('Error while updating the profile')); return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Checks whether the item is duplicated on updating * * @param UserProfileData $itemData * * @return bool * @throws ConstraintException * @throws QueryException */ public function checkDuplicatedOnUpdate($itemData): bool { $query = /** @lang SQL */ 'SELECT `name` FROM UserProfile WHERE UPPER(`name`) = ? AND id <> ?'; $queryData = new QueryData(); $queryData->setParams([ $itemData->getName(), $itemData->getId(), ]); $queryData->setQuery($query); return $this->db->doSelect($queryData)->getNumRows() > 0; } }