From 8c7565165079ea87c06293a92ba26dda4fb1b977 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 27 Jun 2020 14:34:25 +0300 Subject: [PATCH] services refactoring --- application/Espo/Entities/Preferences.php | 8 +-- .../Crm/Business/Event/Invitations.php | 47 ++++++++++------ .../Espo/Modules/Crm/Services/Activities.php | 49 +++++++++++------ .../Espo/Modules/Crm/Services/Campaign.php | 31 +++++++---- .../Crm/Services/KnowledgeBaseArticle.php | 22 +++++--- .../Espo/Modules/Crm/Services/Lead.php | 37 ++++++------- .../Espo/Modules/Crm/Services/MassEmail.php | 21 ++++---- .../Espo/Modules/Crm/Services/Meeting.php | 54 +++---------------- application/Espo/Services/Email.php | 6 +-- application/Espo/Services/EmailAccount.php | 4 +- application/Espo/Services/InboundEmail.php | 4 +- 11 files changed, 145 insertions(+), 138 deletions(-) diff --git a/application/Espo/Entities/Preferences.php b/application/Espo/Entities/Preferences.php index efd9f0bfff..88644b416f 100644 --- a/application/Espo/Entities/Preferences.php +++ b/application/Espo/Entities/Preferences.php @@ -31,9 +31,9 @@ namespace Espo\Entities; class Preferences extends \Espo\Core\ORM\Entity { - public function getSmtpParams() + public function getSmtpParams() : ?array { - $smtpParams = array(); + $smtpParams = []; $smtpParams['server'] = $this->get('smtpServer'); if ($smtpParams['server']) { $smtpParams['port'] = $this->get('smtpPort'); @@ -45,8 +45,8 @@ class Preferences extends \Espo\Core\ORM\Entity $smtpParams['password'] = $this->get('smtpPassword'); } return $smtpParams; - } else { - return false; } + + return null; } } diff --git a/application/Espo/Modules/Crm/Business/Event/Invitations.php b/application/Espo/Modules/Crm/Business/Event/Invitations.php index 4c6cf3f375..1a1756d5db 100644 --- a/application/Espo/Modules/Crm/Business/Event/Invitations.php +++ b/application/Espo/Modules/Crm/Business/Event/Invitations.php @@ -29,30 +29,47 @@ namespace Espo\Modules\Crm\Business\Event; -use \Espo\ORM\Entity; +use Espo\ORM\Entity; use Espo\Core\Utils\Util; +use Espo\Core\{ + ORM\EntityManager, + Mail\Sender, + Utils\Config, + Utils\File\Manager as FileManager, + Utils\DateTime, + Utils\NumberUtil, + Utils\Language, + Utils\TemplateFileManager, +}; + class Invitations { - protected $entityManager; - protected $smtpParams; - protected $mailSender; - - protected $config; - - protected $dateTime; - - protected $language; - protected $ics; + protected $entityManager; + protected $mailSender; + protected $config; + protected $dateTime; + protected $language; + protected $number; protected $templateFileManager; + protected $fileManager; - public function __construct($entityManager, $smtpParams, $mailSender, $config, $fileManager, $dateTime, $number, $language, $templateFileManager) - { + public function __construct( + EntityManager $entityManager, + ?array $smtpParams, + Sender $mailSender, + Config $config, + FileManager $fileManager, + DateTime $dateTime, + NumberUtil $number, + Language $language, + TemplateFileManager $templateFileManager + ) { $this->entityManager = $entityManager; $this->smtpParams = $smtpParams; $this->mailSender = $mailSender; @@ -74,7 +91,7 @@ class Invitations return $this->config; } - public function sendInvitation(Entity $entity, Entity $invitee, $link) + public function sendInvitation(Entity $entity, Entity $invitee, string $link) { $uid = $this->getEntityManager()->getEntity('UniqueId'); $uid->set('data', [ @@ -82,7 +99,7 @@ class Invitations 'eventId' => $entity->id, 'inviteeId' => $invitee->id, 'inviteeType' => $invitee->getEntityType(), - 'link' => $link + 'link' => $link, ]); if ($entity->get('dateEnd')) { diff --git a/application/Espo/Modules/Crm/Services/Activities.php b/application/Espo/Modules/Crm/Services/Activities.php index 73c65f8856..5c872489ad 100644 --- a/application/Espo/Modules/Crm/Services/Activities.php +++ b/application/Espo/Modules/Crm/Services/Activities.php @@ -37,17 +37,26 @@ use Espo\ORM\Entity; use PDO; -class Activities extends \Espo\Core\Services\Base +use Espo\Core\Di; + +class Activities implements + + Di\ConfigAware, + Di\MetadataAware, + Di\AclAware, + Di\SelectManagerFactoryAware, + Di\ServiceFactoryAware, + Di\EntityManagerAware, + Di\UserAware { - protected function init() - { - $this->addDependencyList([ - 'metadata', - 'acl', - 'selectManagerFactory', - 'serviceFactory', - ]); - } + + use Di\ConfigSetter; + use Di\MetadataSetter; + use Di\AclSetter; + use Di\SelectManagerFactorySetter; + use Di\ServiceFactorySetter; + use Di\EntityManagerSetter; + use Di\UserSetter; const UPCOMING_ACTIVITIES_FUTURE_DAYS = 1; @@ -64,37 +73,43 @@ class Activities extends \Espo\Core\Services\Base protected function getEntityManager() { - return $this->getInjection('entityManager'); + return $this->entityManager; } protected function getUser() { - return $this->getInjection('user'); + return $this->user; } protected function getAcl() { - return $this->getInjection('acl'); + return $this->acl; + } + + protected function getConfig() + { + return $this->config; } protected function getMetadata() { - return $this->getInjection('metadata'); + return $this->metadata; } protected function getSelectManagerFactory() { - return $this->getInjection('selectManagerFactory'); + return $this->selectManagerFactory; } protected function getServiceFactory() { - return $this->getInjection('serviceFactory'); + return $this->serviceFactory; } protected function isPerson($scope) { - return in_array($scope, ['Contact', 'Lead', 'User']) || $this->getMetadata()->get(['scopes', $scope, 'type']) === 'Person'; + return in_array($scope, ['Contact', 'Lead', 'User']) || + $this->getMetadata()->get(['scopes', $scope, 'type']) === 'Person'; } protected function isCompany($scope) diff --git a/application/Espo/Modules/Crm/Services/Campaign.php b/application/Espo/Modules/Crm/Services/Campaign.php index cabcd27a9b..03aca85389 100644 --- a/application/Espo/Modules/Crm/Services/Campaign.php +++ b/application/Espo/Modules/Crm/Services/Campaign.php @@ -35,13 +35,13 @@ use Espo\Core\Exceptions\Error, Espo\Core\Exceptions\Forbidden, Espo\Core\Exceptions\BadRequest; -class Campaign extends \Espo\Services\Record +use Espo\Core\Di; + +class Campaign extends \Espo\Services\Record implements + + Di\DefaultLanguageAware { - protected function init() - { - parent::init(); - $this->addDependency('container'); - } + use Di\DefaultLanguageSetter; protected $entityTypeAddressFieldListMap = [ 'Account' => ['billingAddress', 'shippingAddress'], @@ -195,8 +195,15 @@ class Campaign extends \Espo\Services\Record $this->getEntityManager()->saveEntity($logRecord); } - public function logSent($campaignId, $queueItemId = null, Entity $target, Entity $emailOrEmailTemplate = null, $emailAddress, $actionDate = null, $isTest = false) - { + public function logSent( + string $campaignId, + ?string $queueItemId = null, + Entity $target, + Entity $emailOrEmailTemplate = null, + $emailAddress, + $actionDate = null, + $isTest = false + ) { if (empty($actionDate)) { $actionDate = date('Y-m-d H:i:s'); } @@ -466,13 +473,15 @@ class Campaign extends \Espo\Services\Record throw new Error("No targets available for mail merge."); } - $filename = $campaign->get('name') . ' - ' . $this->getDefaultLanguage()->translate($targetEntityType, 'scopeNamesPlural'); + $filename = $campaign->get('name') . ' - ' . + $this->getDefaultLanguage()->translate($targetEntityType, 'scopeNamesPlural'); - return $this->getServiceFactory()->create('Pdf')->generateMailMerge($targetEntityType, $targetEntityList, $template, $filename, $campaign->id); + return $this->getServiceFactory()->create('Pdf')->generateMailMerge( + $targetEntityType, $targetEntityList, $template, $filename, $campaign->id); } protected function getDefaultLanguage() { - return $this->getInjection('container')->get('defaultLanguage'); + return $this->defaultLanguage; } } diff --git a/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php b/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php index 5191736934..9947f15a59 100644 --- a/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php +++ b/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php @@ -35,8 +35,14 @@ use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\NotFound; use Espo\Core\Exceptions\Error; -class KnowledgeBaseArticle extends \Espo\Services\Record +use Espo\Core\Di; + +class KnowledgeBaseArticle extends \Espo\Services\Record implements + + Di\FileStorageManagerAware { + use Di\FileStorageManagerSetter; + protected $readOnlyAttributeList = ['order']; protected function init() @@ -49,12 +55,12 @@ class KnowledgeBaseArticle extends \Espo\Services\Record protected function getFileStorageManager() { - return $this->getInjection('fileStorageManager'); + return $this->fileStorageManager; } - public function getCopiedAttachments($id, $parentType = null, $parentId = null) + public function getCopiedAttachments(string $id, ?string $parentType = null, ?string $parentId = null) { - $ids = array(); + $ids = []; $names = new \stdClass(); if (empty($id)) { @@ -103,7 +109,7 @@ class KnowledgeBaseArticle extends \Espo\Services\Record ); } - public function moveUp($id, $where = null) + public function moveUp(string $id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); @@ -141,7 +147,7 @@ class KnowledgeBaseArticle extends \Espo\Services\Record $this->getEntityManager()->saveEntity($previousEntity); } - public function moveDown($id, $where = null) + public function moveDown(string $id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); @@ -179,7 +185,7 @@ class KnowledgeBaseArticle extends \Espo\Services\Record $this->getEntityManager()->saveEntity($nextEntity); } - public function moveToTop($id, $where = null) + public function moveToTop(string $id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); @@ -215,7 +221,7 @@ class KnowledgeBaseArticle extends \Espo\Services\Record $this->getEntityManager()->saveEntity($entity); } - public function moveToBottom($id, $where = null) + public function moveToBottom(string $id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); diff --git a/application/Espo/Modules/Crm/Services/Lead.php b/application/Espo/Modules/Crm/Services/Lead.php index ce9b133fd0..00d41578f7 100644 --- a/application/Espo/Modules/Crm/Services/Lead.php +++ b/application/Espo/Modules/Crm/Services/Lead.php @@ -34,26 +34,27 @@ use Espo\Core\Exceptions\Forbidden; use Espo\ORM\Entity; -class Lead extends \Espo\Core\Templates\Services\Person +use Espo\Modules\Crm\Entities\Lead as LeadEntity; + +use Espo\Core\Di; + +class Lead extends \Espo\Core\Templates\Services\Person implements + + Di\FieldManagerUtilAware { + use Di\FieldManagerUtilSetter; - protected function init() - { - parent::init(); - $this->addDependency('container'); - } - - protected $linkSelectParams = array( - 'targetLists' => array( - 'additionalColumns' => array( + protected $linkSelectParams = [ + 'targetLists' => [ + 'additionalColumns' => [ 'optedOut' => 'isOptedOut' - ) - ) - ); + ] + ] + ]; - protected function getFieldManager() + protected function getFieldManagerUtil() { - return $this->getInjection('container')->get('fieldManager'); + return $this->fieldManagerUtil; } protected function afterCreateEntity(Entity $entity, $data) @@ -163,8 +164,8 @@ class Lead extends \Espo\Core\Templates\Services\Person continue; } - $leadAttributeList = $this->getFieldManager()->getAttributeList('Lead', $leadField); - $attributeList = $this->getFieldManager()->getAttributeList($entityType, $field); + $leadAttributeList = $this->getFieldManagerUtil()->getAttributeList('Lead', $leadField); + $attributeList = $this->getFieldManagerUtil()->getAttributeList($entityType, $field); foreach ($attributeList as $i => $attribute) { if (in_array($attribute, $ignoreAttributeList)) continue; @@ -183,7 +184,7 @@ class Lead extends \Espo\Core\Templates\Services\Person return $data; } - public function convert(string $id, object $recordsData, ?object $additionalData = null) : \Espo\Modules\Crm\Entities\Lead + public function convert(string $id, object $recordsData, ?object $additionalData = null) : LeadEntity { $lead = $this->getEntity($id); diff --git a/application/Espo/Modules/Crm/Services/MassEmail.php b/application/Espo/Modules/Crm/Services/MassEmail.php index fd4b1db036..23880810ad 100644 --- a/application/Espo/Modules/Crm/Services/MassEmail.php +++ b/application/Espo/Modules/Crm/Services/MassEmail.php @@ -42,8 +42,16 @@ use Laminas\Mail\Message; use Espo\Core\Record\Collection as RecordCollection; -class MassEmail extends \Espo\Services\Record +use Espo\Core\Di; + +class MassEmail extends \Espo\Services\Record implements + + Di\DefaultLanguageAware, + Di\MailSenderAware { + use Di\DefaultLanguageSetter; + use Di\MailSenderSetter; + const MAX_ATTEMPT_COUNT = 3; const MAX_PER_HOUR_COUNT = 10000; @@ -56,21 +64,14 @@ class MassEmail extends \Espo\Services\Record protected $targetsLinkList = ['accounts', 'contacts', 'leads', 'users']; - protected function init() - { - parent::init(); - $this->addDependency('container'); - $this->addDependency('defaultLanguage'); - } - protected function getMailSender() { - return $this->getInjection('container')->get('mailSender'); + return $this->mailSender; } protected function getLanguage() { - return $this->getInjection('defaultLanguage'); + return $this->defaultLanguage; } protected function beforeCreateEntity(Entity $entity, $data) diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index 43ac3af9e6..5c3dddf8c9 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -36,48 +36,16 @@ use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; -class Meeting extends \Espo\Services\Record -{ +class Meeting extends \Espo\Services\Record { + protected $validateRequiredSkipFieldList = [ 'dateEnd' ]; - protected function init() - { - $this->addDependencyList([ - 'preferences', - 'language', - 'dateTime', - 'container', - 'fileManager', - 'number' - ]); - } - protected $exportSkipFieldList = ['duration']; protected $duplicateIgnoreAttributeList = ['usersColumns', 'contactsColumns', 'leadsColumns']; - protected function getMailSender() - { - return $this->getInjection('container')->get('mailSender'); - } - - protected function getPreferences() - { - return $this->getInjection('preferences'); - } - - protected function getLanguage() - { - return $this->getInjection('language'); - } - - protected function getDateTime() - { - return $this->getInjection('dateTime'); - } - public function checkAssignment(Entity $entity) : bool { $result = parent::checkAssignment($entity); @@ -119,26 +87,16 @@ class Meeting extends \Espo\Services\Record $smtpParams = $this->getServiceFactory()->create('Email')->getUserSmtpParams($this->getUser()->id); } - $templateFileManager = $this->getInjection('container')->get('templateFileManager'); - - return new Invitations( - $this->getEntityManager(), - $smtpParams, - $this->getMailSender(), - $this->getConfig(), - $this->getInjection('fileManager'), - $this->getDateTime(), - $this->getInjection('number'), - $this->getLanguage(), - $templateFileManager - ); + return $this->injectableFactory->createWith(Invitations::class, [ + 'smtpParams' => $smtpParams, + ]); } public function sendInvitations(Entity $entity, bool $useUserSmtp = true) { $invitationManager = $this->getInvitationManager($useUserSmtp); - $emailHash = array(); + $emailHash = []; $sentCount = 0; diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index 03e5336b22..9d8ddcfdf7 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -103,17 +103,17 @@ class Email extends Record implements return $this->crypt; } - public function getUserSmtpParams(string $userId) + public function getUserSmtpParams(string $userId) : ?array { $user = $this->getEntityManager()->getEntity('User', $userId); - if (!$user) return; + if (!$user) return null; $fromAddress = $user->get('emailAddress'); if ($fromAddress) $fromAddress = strtolower($fromAddress); $preferences = $this->getEntityManager()->getEntity('Preferences', $user->id); - if (!$preferences) return; + if (!$preferences) return null; $smtpParams = $preferences->getSmtpParams(); if ($smtpParams) { diff --git a/application/Espo/Services/EmailAccount.php b/application/Espo/Services/EmailAccount.php index d9249a3dea..415e2c0f0f 100644 --- a/application/Espo/Services/EmailAccount.php +++ b/application/Espo/Services/EmailAccount.php @@ -526,7 +526,7 @@ class EmailAccount extends Record implements return $emailAccount; } - public function getSmtpParamsFromAccount(EmailAccountEntity $emailAccount) + public function getSmtpParamsFromAccount(EmailAccountEntity $emailAccount) : ?array { $smtpParams = []; $smtpParams['server'] = $emailAccount->get('smtpHost'); @@ -547,7 +547,7 @@ class EmailAccount extends Record implements return $smtpParams; } - return; + return null; } public function applySmtpHandler(EmailAccountEntity $emailAccount, array &$params) diff --git a/application/Espo/Services/InboundEmail.php b/application/Espo/Services/InboundEmail.php index b266bf3574..7ae9240a4c 100644 --- a/application/Espo/Services/InboundEmail.php +++ b/application/Espo/Services/InboundEmail.php @@ -977,7 +977,7 @@ class InboundEmail extends \Espo\Services\Record implements $storage->appendMessage($message->toString(), $folder); } - public function getSmtpParamsFromAccount(\Espo\Entities\InboundEmail $emailAccount) + public function getSmtpParamsFromAccount(\Espo\Entities\InboundEmail $emailAccount) : ?array { $smtpParams = []; $smtpParams['server'] = $emailAccount->get('smtpHost'); @@ -1006,7 +1006,7 @@ class InboundEmail extends \Espo\Services\Record implements return $smtpParams; } - return; + return null; } public function applySmtpHandler(\Espo\Entities\InboundEmail $emailAccount, array &$params)