1<?php 2 3declare(strict_types=1); 4 5 6/** 7 * Circles - Bring cloud-users closer together. 8 * 9 * This file is licensed under the Affero General Public License version 3 or 10 * later. See the COPYING file. 11 * 12 * @author Maxence Lange <maxence@artificial-owl.com> 13 * @copyright 2021 14 * @license GNU AGPL version 3 or any later version 15 * 16 * This program is free software: you can redistribute it and/or modify 17 * it under the terms of the GNU Affero General Public License as 18 * published by the Free Software Foundation, either version 3 of the 19 * License, or (at your option) any later version. 20 * 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU Affero General Public License for more details. 25 * 26 * You should have received a copy of the GNU Affero General Public License 27 * along with this program. If not, see <http://www.gnu.org/licenses/>. 28 * 29 */ 30 31 32namespace OCA\Circles\Db; 33 34use ArtificialOwl\MySmallPhpTools\Exceptions\RowNotFoundException; 35use OCA\Circles\Exceptions\MemberNotFoundException; 36use OCA\Circles\Exceptions\RequestBuilderException; 37use OCA\Circles\IFederatedUser; 38use OCA\Circles\Model\FederatedUser; 39use OCA\Circles\Model\Member; 40 41/** 42 * Class MemberRequestBuilder 43 * 44 * @package OCA\Circles\Db 45 */ 46class MemberRequestBuilder extends CoreRequestBuilder { 47 48 49 /** 50 * @return CoreQueryBuilder 51 */ 52 protected function getMemberInsertSql(): CoreQueryBuilder { 53 $qb = $this->getQueryBuilder(); 54 $qb->insert(self::TABLE_MEMBER) 55 ->setValue('joined', $qb->createNamedParameter($this->timezoneService->getUTCDate())); 56 57 return $qb; 58 } 59 60 61 /** 62 * @return CoreQueryBuilder 63 */ 64 protected function getMemberUpdateSql(): CoreQueryBuilder { 65 $qb = $this->getQueryBuilder(); 66 $qb->update(self::TABLE_MEMBER); 67 68 return $qb; 69 } 70 71 72 /** 73 * @param IFederatedUser|null $initiator 74 * @param bool $getBasedOn 75 * 76 * @return CoreQueryBuilder 77 * @throws RequestBuilderException 78 */ 79 protected function getMemberSelectSql( 80 ?IFederatedUser $initiator = null, 81 bool $getBasedOn = true 82 ): CoreQueryBuilder { 83 $qb = $this->getQueryBuilder(); 84 $qb->generateSelect( 85 self::TABLE_MEMBER, 86 self::$tables[self::TABLE_MEMBER], 87 CoreQueryBuilder::MEMBER 88 ) 89 ->generateGroupBy(self::$tables[self::TABLE_MEMBER], CoreQueryBuilder::MEMBER) 90 ->orderBy(CoreQueryBuilder::MEMBER . '.joined'); 91 92 if ($getBasedOn) { 93 $qb->leftJoinBasedOn(CoreQueryBuilder::MEMBER, $initiator); 94 } 95 96 return $qb; 97 } 98 99 100 /** 101 * Base of the Sql Delete request 102 * 103 * @return CoreQueryBuilder 104 */ 105 protected function getMemberDeleteSql(): CoreQueryBuilder { 106 $qb = $this->getQueryBuilder(); 107 $qb->delete(self::TABLE_MEMBER); 108 109 return $qb; 110 } 111 112 113 /** 114 * @param CoreQueryBuilder $qb 115 * 116 * @return Member 117 * @throws MemberNotFoundException 118 */ 119 public function getItemFromRequest(CoreQueryBuilder $qb): Member { 120 /** @var Member $member */ 121 try { 122 $member = $qb->asItem(Member::class); 123 } catch (RowNotFoundException $e) { 124 throw new MemberNotFoundException(); 125 } 126 127 return $member; 128 } 129 130 /** 131 * @param CoreQueryBuilder $qb 132 * @param bool $asFederatedUser 133 * 134 * @return Member|FederatedUser[] 135 */ 136 public function getItemsFromRequest(CoreQueryBuilder $qb, bool $asFederatedUser = false): array { 137 $object = Member::class; 138 if ($asFederatedUser) { 139 $object = FederatedUser::class; 140 } 141 142 /** @var Member|FederatedUser[] $result */ 143 return $qb->asItems($object); 144 } 145} 146