services refactoring

This commit is contained in:
Yuri Kuznetsov
2020-06-27 14:34:25 +03:00
parent 44ea2aee40
commit 8c75651650
11 changed files with 145 additions and 138 deletions
+4 -4
View File
@@ -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;
}
}
@@ -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')) {
@@ -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)
@@ -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;
}
}
@@ -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();
+19 -18
View File
@@ -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);
@@ -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)
@@ -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;
+3 -3
View File
@@ -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) {
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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)