diff options
author | Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com> | 2023-08-14 08:11:37 +0200 |
---|---|---|
committer | Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com> | 2023-08-30 18:36:29 +0200 |
commit | 303dadfaa9c274a529c4cc55791cb71fbb9e67f9 (patch) | |
tree | e0b2d8d88312da64eca5322f227ee4f5846f51d5 /apps/sharebymail/lib | |
parent | 23197a840cce0c518fd4dc298bece04a20c03209 (diff) | |
download | nextcloud-server-303dadfaa9c274a529c4cc55791cb71fbb9e67f9.tar.gz nextcloud-server-303dadfaa9c274a529c4cc55791cb71fbb9e67f9.zip |
sharebymail: remove ILogger and upgrade to PHP 8 syntax
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
Diffstat (limited to 'apps/sharebymail/lib')
-rw-r--r-- | apps/sharebymail/lib/ShareByMailProvider.php | 234 |
1 files changed, 59 insertions, 175 deletions
diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index af3d14b49bc..60b7f20b88d 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -55,7 +55,6 @@ use OCP\HintException; use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; -use OCP\ILogger; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; @@ -68,6 +67,7 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; use OCP\Share\IShareProvider; +use Psr\Log\LoggerInterface; /** * Class ShareByMail @@ -75,100 +75,41 @@ use OCP\Share\IShareProvider; * @package OCA\ShareByMail */ class ShareByMailProvider implements IShareProvider { - private IConfig $config; - - /** @var IDBConnection */ - private $dbConnection; - - /** @var ILogger */ - private $logger; - - /** @var ISecureRandom */ - private $secureRandom; - - /** @var IUserManager */ - private $userManager; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IL10N */ - private $l; - - /** @var IMailer */ - private $mailer; - - /** @var IURLGenerator */ - private $urlGenerator; - - /** @var IManager */ - private $activityManager; - - /** @var SettingsManager */ - private $settingsManager; - - /** @var Defaults */ - private $defaults; - - /** @var IHasher */ - private $hasher; - - /** @var IEventDispatcher */ - private $eventDispatcher; - - /** @var IShareManager */ - private $shareManager; - /** * Return the identifier of this provider. * * @return string Containing only [a-zA-Z0-9] */ - public function identifier() { + public function identifier(): string { return 'ocMailShare'; } - public function __construct(IConfig $config, - IDBConnection $connection, - ISecureRandom $secureRandom, - IUserManager $userManager, - IRootFolder $rootFolder, - IL10N $l, - ILogger $logger, - IMailer $mailer, - IURLGenerator $urlGenerator, - IManager $activityManager, - SettingsManager $settingsManager, - Defaults $defaults, - IHasher $hasher, - IEventDispatcher $eventDispatcher, - IShareManager $shareManager) { - $this->config = $config; - $this->dbConnection = $connection; - $this->secureRandom = $secureRandom; - $this->userManager = $userManager; - $this->rootFolder = $rootFolder; - $this->l = $l; - $this->logger = $logger; - $this->mailer = $mailer; - $this->urlGenerator = $urlGenerator; - $this->activityManager = $activityManager; - $this->settingsManager = $settingsManager; - $this->defaults = $defaults; - $this->hasher = $hasher; - $this->eventDispatcher = $eventDispatcher; - $this->shareManager = $shareManager; + public function __construct( + private IConfig $config, + private IDBConnection $dbConnection, + private ISecureRandom $secureRandom, + private IUserManager $userManager, + private IRootFolder $rootFolder, + private IL10N $l, + private LoggerInterface $logger, + private IMailer $mailer, + private IURLGenerator $urlGenerator, + private IManager $activityManager, + private SettingsManager $settingsManager, + private Defaults $defaults, + private IHasher $hasher, + private IEventDispatcher $eventDispatcher, + private IShareManager $shareManager, + ) { } /** * Share a path * - * @param IShare $share - * @return IShare The share object * @throws ShareNotFound * @throws \Exception */ - public function create(IShare $share) { + public function create(IShare $share): IShare { $shareWith = $share->getSharedWith(); /* * Check if file is not already shared with the remote user @@ -213,11 +154,9 @@ class ShareByMailProvider implements IShareProvider { /** * auto generate password in case of password enforcement on mail shares * - * @param IShare $share - * @return string * @throws \Exception */ - protected function autoGeneratePassword($share) { + protected function autoGeneratePassword(IShare $share): string { $initiatorUser = $this->userManager->get($share->getSharedBy()); $initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null; $allowPasswordByMail = $this->settingsManager->sendPasswordByMail(); @@ -241,11 +180,8 @@ class ShareByMailProvider implements IShareProvider { /** * create activity if a file/folder was shared by mail - * - * @param IShare $share - * @param string $type */ - protected function createShareActivity(IShare $share, string $type = 'share') { + protected function createShareActivity(IShare $share, string $type = 'share'): void { $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); $this->publishActivity( @@ -273,12 +209,8 @@ class ShareByMailProvider implements IShareProvider { /** * create activity if a file/folder was shared by mail - * - * @param IShare $share - * @param string $sharedWith - * @param bool $sendToSelf */ - protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) { + protected function createPasswordSendActivity(IShare $share, string $sharedWith, bool $sendToSelf): void { $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); if ($sendToSelf) { @@ -303,14 +235,8 @@ class ShareByMailProvider implements IShareProvider { /** * publish activity if a file/folder was shared by mail - * - * @param string $subject - * @param array $parameters - * @param string $affectedUser - * @param int $fileId - * @param string $filePath */ - protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath) { + protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath): void { $event = $this->activityManager->generateEvent(); $event->setApp('sharebymail') ->setType('shared') @@ -321,11 +247,9 @@ class ShareByMailProvider implements IShareProvider { } /** - * @param IShare $share - * @return int * @throws \Exception */ - protected function createMailShare(IShare $share) { + protected function createMailShare(IShare $share): int { $share->setToken($this->generateToken()); $shareId = $this->addShareToDB( $share->getNodeId(), @@ -366,18 +290,16 @@ class ShareByMailProvider implements IShareProvider { $share->getNote() ); } catch (HintException $hintException) { - $this->logger->logException($hintException, [ - 'message' => 'Failed to send share by mail.', - 'level' => ILogger::ERROR, + $this->logger->error('Failed to send share by mail.', [ 'app' => 'sharebymail', + 'exception' => $hintException, ]); $this->removeShareFromTable($shareId); throw $hintException; } catch (\Exception $e) { - $this->logger->logException($e, [ - 'message' => 'Failed to send share by mail.', - 'level' => ILogger::ERROR, + $this->logger->error('Failed to send share by mail.', [ 'app' => 'sharebymail', + 'exception' => $e, ]); $this->removeShareFromTable($shareId); throw new HintException('Failed to send share by mail', @@ -388,20 +310,16 @@ class ShareByMailProvider implements IShareProvider { } /** - * @param string $filename - * @param string $link - * @param string $initiator - * @param string $shareWith - * @param \DateTime|null $expiration * @throws \Exception If mail couldn't be sent */ - protected function sendMailNotification($filename, - $link, - $initiator, - $shareWith, - \DateTime $expiration = null, - $note = '' - ) { + protected function sendMailNotification( + string $filename, + string $link, + string $initiator, + string $shareWith, + ?\DateTime $expiration = null, + string $note = '', + ): void { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; $message = $this->mailer->createMessage(); @@ -464,12 +382,8 @@ class ShareByMailProvider implements IShareProvider { /** * send password to recipient of a mail share - * - * @param IShare $share - * @param string $password - * @return bool */ - protected function sendPassword(IShare $share, $password) { + protected function sendPassword(IShare $share, string $password): bool { $filename = $share->getNode()->getName(); $initiator = $share->getSharedBy(); $shareWith = $share->getSharedWith(); @@ -538,7 +452,7 @@ class ShareByMailProvider implements IShareProvider { return true; } - protected function sendNote(IShare $share) { + protected function sendNote(IShare $share): void { $recipient = $share->getSharedWith(); @@ -598,12 +512,9 @@ class ShareByMailProvider implements IShareProvider { * send auto generated password to the owner. This happens if the admin enforces * a password for mail shares and forbid to send the password by mail to the recipient * - * @param IShare $share - * @param string $password - * @return bool * @throws \Exception */ - protected function sendPasswordToOwner(IShare $share, $password) { + protected function sendPasswordToOwner(IShare $share, string $password): bool { $filename = $share->getNode()->getName(); $initiator = $this->userManager->get($share->getSharedBy()); $initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null; @@ -665,10 +576,8 @@ class ShareByMailProvider implements IShareProvider { /** * generate share token - * - * @return string */ - protected function generateToken($size = 15) { + protected function generateToken(int $size = 15): string { $token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE); return $token; } @@ -676,10 +585,9 @@ class ShareByMailProvider implements IShareProvider { /** * Get all children of this share * - * @param IShare $parent * @return IShare[] */ - public function getChildren(IShare $parent) { + public function getChildren(IShare $parent): array { $children = []; $qb = $this->dbConnection->getQueryBuilder(); @@ -752,12 +660,8 @@ class ShareByMailProvider implements IShareProvider { /** * Update a share - * - * @param IShare $share - * @param string|null $plainTextPassword - * @return IShare The share object */ - public function update(IShare $share, $plainTextPassword = null) { + public function update(IShare $share, ?string $plainTextPassword = null): IShare { $originalShare = $this->getShareById($share->getId()); // a real password was given @@ -796,7 +700,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function move(IShare $share, $recipient) { + public function move(IShare $share, $recipient): IShare { /** * nothing to do here, mail shares are only outgoing shares */ @@ -808,7 +712,7 @@ class ShareByMailProvider implements IShareProvider { * * @param IShare $share */ - public function delete(IShare $share) { + public function delete(IShare $share): void { try { $this->createShareActivity($share, 'unshare'); } catch (\Exception $e) { @@ -820,7 +724,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function deleteFromSelf(IShare $share, $recipient) { + public function deleteFromSelf(IShare $share, $recipient): void { // nothing to do here, mail shares are only outgoing shares } @@ -831,7 +735,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset): array { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') ->from('share'); @@ -887,7 +791,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function getShareById($id, $recipientId = null) { + public function getShareById($id, $recipientId = null): IShare { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') @@ -915,10 +819,9 @@ class ShareByMailProvider implements IShareProvider { /** * Get shares for a given path * - * @param \OCP\Files\Node $path * @return IShare[] */ - public function getSharesByPath(Node $path) { + public function getSharesByPath(Node $path): array { $qb = $this->dbConnection->getQueryBuilder(); $cursor = $qb->select('*') @@ -939,7 +842,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { + public function getSharedWith($userId, $shareType, $node, $limit, $offset): array { /** @var IShare[] $shares */ $shares = []; @@ -979,11 +882,9 @@ class ShareByMailProvider implements IShareProvider { /** * Get a share by token * - * @param string $token - * @return IShare * @throws ShareNotFound */ - public function getShareByToken($token) { + public function getShareByToken($token): IShare { $qb = $this->dbConnection->getQueryBuilder(); $cursor = $qb->select('*') @@ -1009,8 +910,6 @@ class ShareByMailProvider implements IShareProvider { /** * remove share from table - * - * @param int $shareId */ protected function removeShareFromTable(int $shareId): void { $qb = $this->dbConnection->getQueryBuilder(); @@ -1022,12 +921,10 @@ class ShareByMailProvider implements IShareProvider { /** * Create a share object from an database row * - * @param array $data - * @return IShare * @throws InvalidShare * @throws ShareNotFound */ - protected function createShareObject($data) { + protected function createShareObject(array $data): IShare { $share = new Share($this->rootFolder, $this->userManager); $share->setId((int)$data['id']) ->setShareType((int)$data['share_type']) @@ -1078,12 +975,9 @@ class ShareByMailProvider implements IShareProvider { /** * Get the node with file $id for $user * - * @param string $userId - * @param int $id - * @return \OCP\Files\File|\OCP\Files\Folder * @throws InvalidShare */ - private function getNode($userId, $id) { + private function getNode(string $userId, int $id): Node { try { $userFolder = $this->rootFolder->getUserFolder($userId); } catch (NoUserException $e) { @@ -1102,11 +996,8 @@ class ShareByMailProvider implements IShareProvider { /** * A user is deleted from the system * So clean up the relevant shares. - * - * @param string $uid - * @param int $shareType */ - public function userDeleted($uid, $shareType) { + public function userDeleted($uid, $shareType): void { $qb = $this->dbConnection->getQueryBuilder(); $qb->delete('share') @@ -1117,29 +1008,22 @@ class ShareByMailProvider implements IShareProvider { /** * This provider does not support group shares - * - * @param string $gid */ - public function groupDeleted($gid) { + public function groupDeleted($gid): void { } /** * This provider does not support group shares - * - * @param string $uid - * @param string $gid */ - public function userDeletedFromGroup($uid, $gid) { + public function userDeletedFromGroup($uid, $gid): void { } /** * get database row of a give share * - * @param $id - * @return array * @throws ShareNotFound */ - protected function getRawShare($id) { + protected function getRawShare(int $id): array { // Now fetch the inserted share and create a complete share object $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') @@ -1157,7 +1041,7 @@ class ShareByMailProvider implements IShareProvider { return $data; } - public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) { + public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true): array { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') ->from('share', 's') @@ -1206,7 +1090,7 @@ class ShareByMailProvider implements IShareProvider { /** * @inheritdoc */ - public function getAccessList($nodes, $currentAccess) { + public function getAccessList($nodes, $currentAccess): array { $ids = []; foreach ($nodes as $node) { $ids[] = $node->getId(); |