From 17d2791d40001f1d427e3dd17f85ca4509a3ddef Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 4 Nov 2021 10:59:48 +0200 Subject: [PATCH] type fixes --- application/Espo/Controllers/Attachment.php | 27 +++++++++++++------ application/Espo/Controllers/EmailAccount.php | 26 ++++++++++++------ application/Espo/Controllers/EmailAddress.php | 9 ++++++- application/Espo/Controllers/EmailFolder.php | 13 ++++++--- .../Espo/Controllers/ExternalAccount.php | 19 ++++++++----- application/Espo/Controllers/InboundEmail.php | 25 ++++++++++++----- application/Espo/Controllers/Notification.php | 15 +++++++---- .../Espo/Core/Controllers/RecordBase.php | 7 +++++ application/Espo/Services/ExternalAccount.php | 3 +++ 9 files changed, 106 insertions(+), 38 deletions(-) diff --git a/application/Espo/Controllers/Attachment.php b/application/Espo/Controllers/Attachment.php index 90ebe62170..0d9a95876b 100644 --- a/application/Espo/Controllers/Attachment.php +++ b/application/Espo/Controllers/Attachment.php @@ -29,6 +29,8 @@ namespace Espo\Controllers; +use Espo\Services\Attachment as Service; + use Espo\Core\{ Exceptions\Forbidden, Exceptions\BadRequest, @@ -37,7 +39,7 @@ use Espo\Core\{ Controllers\RecordBase, }; -use StdClass; +use stdClass; class Attachment extends RecordBase { @@ -48,7 +50,7 @@ class Attachment extends RecordBase } } - public function postActionGetAttachmentFromImageUrl(Request $request): StdClass + public function postActionGetAttachmentFromImageUrl(Request $request): stdClass { $data = $request->getParsedBody(); @@ -57,13 +59,15 @@ class Attachment extends RecordBase } if (empty($data->field)) { - throw new BadRequest('postActionGetAttachmentFromImageUrl: No field specified'); + throw new BadRequest('postActionGetAttachmentFromImageUrl: No field specified.'); } - return $this->getRecordService()->getAttachmentFromImageUrl($data)->getValueMap(); + return $this->getAttachmentService() + ->getAttachmentFromImageUrl($data) + ->getValueMap(); } - public function postActionGetCopiedAttachment(Request $request): StdClass + public function postActionGetCopiedAttachment(Request $request): stdClass { $data = $request->getParsedBody(); @@ -72,10 +76,12 @@ class Attachment extends RecordBase } if (empty($data->field)) { - throw new BadRequest('postActionGetCopiedAttachment copy: No field specified'); + throw new BadRequest('postActionGetCopiedAttachment copy: No field specified.'); } - return $this->getRecordService()->getCopiedAttachment($data)->getValueMap(); + return $this->getAttachmentService() + ->getCopiedAttachment($data) + ->getValueMap(); } public function getActionFile(Request $request, Response $response): void @@ -86,7 +92,7 @@ class Attachment extends RecordBase throw new BadRequest(); } - $fileData = $this->getRecordService()->getFileData($id); + $fileData = $this->getAttachmentService()->getFileData($id); $response ->setHeader('Content-Type', $fileData->type) @@ -94,4 +100,9 @@ class Attachment extends RecordBase ->setHeader('Content-Length', (string) $fileData->size) ->setBody($fileData->stream); } + + private function getAttachmentService(): Service + { + return $this->getRecordService(); + } } diff --git a/application/Espo/Controllers/EmailAccount.php b/application/Espo/Controllers/EmailAccount.php index 0daa92608d..a43f4dc526 100644 --- a/application/Espo/Controllers/EmailAccount.php +++ b/application/Espo/Controllers/EmailAccount.php @@ -32,13 +32,20 @@ namespace Espo\Controllers; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\Error; +use Espo\Services\EmailAccount as Service; + +use Espo\Core\Di\CryptAware; +use Espo\Core\Di\CryptSetter; + use Espo\Core\{ Controllers\Record, Api\Request, }; -class EmailAccount extends Record +class EmailAccount extends Record implements CryptAware { + use CryptSetter; + protected function checkAccess(): bool { return $this->acl->check('EmailAccountScope'); @@ -59,7 +66,7 @@ class EmailAccount extends Record 'userId' => $data->userId ?? null, ]; - return $this->getRecordService()->getFolders($params); + return $this->getEmailAccountService()->getFolders($params); } public function postActionTestConnection(Request $request): bool @@ -69,24 +76,27 @@ class EmailAccount extends Record if (is_null($data->password)) { $emailAccount = $this->entityManager->getEntity('EmailAccount', $data->id); - if (!$emailAccount || !$emailAccount->id) { + if (!$emailAccount || !$emailAccount->getId()) { throw new Error(); } if ( - $emailAccount->get('assignedUserId') != $this->user->id && + $emailAccount->get('assignedUserId') !== $this->user->getId() && !$this->user->isAdmin() ) { throw new Forbidden(); } - $data->password = $this->getContainer() - ->get('crypt') - ->decrypt($emailAccount->get('password')); + $data->password = $this->crypt->decrypt($emailAccount->get('password')); } - $this->getRecordService()->testConnection(get_object_vars($data)); + $this->getEmailAccountService()->testConnection(get_object_vars($data)); return true; } + + private function getEmailAccountService(): Service + { + return $this->getRecordService(); + } } diff --git a/application/Espo/Controllers/EmailAddress.php b/application/Espo/Controllers/EmailAddress.php index 938726245d..9667e526ce 100644 --- a/application/Espo/Controllers/EmailAddress.php +++ b/application/Espo/Controllers/EmailAddress.php @@ -31,6 +31,8 @@ namespace Espo\Controllers; use Espo\Core\Exceptions\Forbidden; +use Espo\Services\EmailAddress as Service; + use Espo\Core\{ Controllers\RecordBase, Api\Request, @@ -58,6 +60,11 @@ class EmailAddress extends RecordBase $onlyActual = $request->getQueryParam('onlyActual') === 'true'; - return $this->getRecordService()->searchInAddressBook($q, $maxSize, $onlyActual); + return $this->getEmailAddressService()->searchInAddressBook($q, $maxSize, $onlyActual); + } + + private function getEmailAddressService(): Service + { + return $this->getRecordService(); } } diff --git a/application/Espo/Controllers/EmailFolder.php b/application/Espo/Controllers/EmailFolder.php index 2e941b664c..a399a870ee 100644 --- a/application/Espo/Controllers/EmailFolder.php +++ b/application/Espo/Controllers/EmailFolder.php @@ -31,6 +31,8 @@ namespace Espo\Controllers; use Espo\Core\Exceptions\BadRequest; +use Espo\Services\EmailFolder as Service; + use Espo\Core\{ Controllers\RecordBase, Api\Request, @@ -46,7 +48,7 @@ class EmailFolder extends RecordBase throw new BadRequest(); } - $this->getRecordService()->moveUp($data->id); + $this->getEmailFolderService()->moveUp($data->id); return true; } @@ -59,13 +61,18 @@ class EmailFolder extends RecordBase throw new BadRequest(); } - $this->getRecordService()->moveDown($data->id); + $this->getEmailFolderService()->moveDown($data->id); return true; } public function getActionListAll(): array { - return $this->getRecordService()->listAll(); + return $this->getEmailFolderService()->listAll(); + } + + private function getEmailFolderService(): Service + { + return $this->getRecordService(); } } diff --git a/application/Espo/Controllers/ExternalAccount.php b/application/Espo/Controllers/ExternalAccount.php index 98f3cd1e63..033b7d8d34 100644 --- a/application/Espo/Controllers/ExternalAccount.php +++ b/application/Espo/Controllers/ExternalAccount.php @@ -31,6 +31,8 @@ namespace Espo\Controllers; use Espo\Core\Exceptions\Forbidden; +use Espo\Services\ExternalAccount as Service; + use Espo\Core\{ Controllers\RecordBase, Api\Request, @@ -51,18 +53,20 @@ class ExternalAccount extends RecordBase public function getActionList(Request $request, Response $response): stdClass { - $integrations = $this->entityManager->getRepository('Integration')->find(); + $integrations = $this->entityManager + ->getRDBRepository('Integration') + ->find(); $list = []; foreach ($integrations as $entity) { if ( $entity->get('enabled') && - $this->metadata->get('integrations.' . $entity->id .'.allowUserAccounts') + $this->metadata->get('integrations.' . $entity->getId() .'.allowUserAccounts') ) { $userAccountAclScope = $this->metadata - ->get(['integrations', $entity->id, 'userAccountAclScope']); + ->get(['integrations', $entity->getId(), 'userAccountAclScope']); if ($userAccountAclScope) { if (!$this->acl->checkScope($userAccountAclScope)) { @@ -97,7 +101,7 @@ class ExternalAccount extends RecordBase return (object) [ 'clientId' => $entity->get('clientId'), 'redirectUri' => $this->config->get('siteUrl') . '?entryPoint=oauthCallback', - 'isConnected' => $this->getRecordService()->ping($integration, $userId) + 'isConnected' => $this->getExternalAccount()->ping($integration, $userId) ]; } @@ -151,8 +155,11 @@ class ExternalAccount extends RecordBase throw new Forbidden(); } - $service = $this->getRecordService(); + return $this->getExternalAccount()->authorizationCode($integration, $userId, $code); + } - return $service->authorizationCode($integration, $userId, $code); + private function getExternalAccount(): Service + { + return $this->getRecordService(); } } diff --git a/application/Espo/Controllers/InboundEmail.php b/application/Espo/Controllers/InboundEmail.php index 492d3de92f..739a1d210a 100644 --- a/application/Espo/Controllers/InboundEmail.php +++ b/application/Espo/Controllers/InboundEmail.php @@ -29,14 +29,22 @@ namespace Espo\Controllers; +use Espo\Services\InboundEmail as Service; + use Espo\Core\Exceptions\Error; + +use Espo\Core\Di\CryptAware; +use Espo\Core\Di\CryptSetter; + use Espo\Core\{ Controllers\Record, Api\Request, }; -class InboundEmail extends Record +class InboundEmail extends Record implements CryptAware { + use CryptSetter; + protected function checkAccess(): bool { return $this->getUser()->isAdmin(); @@ -55,7 +63,7 @@ class InboundEmail extends Record 'id' => $data->id ?? null, ]; - return $this->getRecordService()->getFolders($params); + return $this->getInboundEmailService()->getFolders($params); } public function postActionTestConnection(Request $request): bool @@ -65,17 +73,20 @@ class InboundEmail extends Record if (is_null($data->password)) { $inboundEmail = $this->entityManager->getEntity('InboundEmail', $data->id); - if (!$inboundEmail || !$inboundEmail->id) { + if (!$inboundEmail || !$inboundEmail->getId()) { throw new Error(); } - $data->password = $this->getContainer() - ->get('crypt') - ->decrypt($inboundEmail->get('password')); + $data->password = $this->crypt->decrypt($inboundEmail->get('password')); } - $this->getRecordService()->testConnection(get_object_vars($data)); + $this->getInboundEmailService()->testConnection(get_object_vars($data)); return true; } + + private function getInboundEmailService(): Service + { + return $this->getRecordService(); + } } diff --git a/application/Espo/Controllers/Notification.php b/application/Espo/Controllers/Notification.php index 3372d26e79..0ef5fac589 100644 --- a/application/Espo/Controllers/Notification.php +++ b/application/Espo/Controllers/Notification.php @@ -29,6 +29,8 @@ namespace Espo\Controllers; +use Espo\Services\Notification as Service; + use Espo\Core\{ Controllers\RecordBase, Api\Request, @@ -58,9 +60,7 @@ class Notification extends RecordBase 'after' => $after, ]; - $recordCollection = $this->recordServiceContainer - ->get('Notification') - ->getList($userId, $params); + $recordCollection = $this->getNotificationService()->getList($userId, $params); return (object) [ 'total' => $recordCollection->getTotal(), @@ -72,15 +72,20 @@ class Notification extends RecordBase { $userId = $this->user->getId(); - return $this->recordServiceContainer->get('Notification')->getNotReadCount($userId); + return $this->getNotificationService()->getNotReadCount($userId); } public function postActionMarkAllRead(Request $request): bool { $userId = $this->user->getId(); - $this->recordServiceContainer->get('Notification')->markAllRead($userId); + $this->getNotificationService()->markAllRead($userId); return true; } + + private function getNotificationService(): Service + { + return $this->recordServiceContainer->get('Notification'); + } } diff --git a/application/Espo/Core/Controllers/RecordBase.php b/application/Espo/Core/Controllers/RecordBase.php index 8b1a928554..a5e15d2993 100644 --- a/application/Espo/Core/Controllers/RecordBase.php +++ b/application/Espo/Core/Controllers/RecordBase.php @@ -55,6 +55,8 @@ use Espo\Core\Di; use Espo\Entities\User; use Espo\Entities\Preferences; +use Espo\ORM\EntityManager; + use stdClass; class RecordBase extends Base implements Di\EntityManagerAware @@ -95,12 +97,17 @@ class RecordBase extends Base implements Di\EntityManagerAware protected $config; + /** + * @var User + */ protected $user; protected $acl; /** * @deprecated + * + * @var EntityManager */ protected $entityManager; diff --git a/application/Espo/Services/ExternalAccount.php b/application/Espo/Services/ExternalAccount.php index 66df1afc03..1d6ff184fa 100644 --- a/application/Espo/Services/ExternalAccount.php +++ b/application/Espo/Services/ExternalAccount.php @@ -82,6 +82,9 @@ class ExternalAccount extends Record implements Di\HookManagerAware return $this->entityManager->getEntity('ExternalAccount', $integration . '__' . $userId); } + /** + * @return bool + */ public function ping(string $integration, string $userId) { $entity = $this->getExternalAccountEntity($integration, $userId);