From aee81c15335be71bf185d790c204302eabdf0fa2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 26 Jun 2020 16:06:28 +0300 Subject: [PATCH] services refactoring --- application/Espo/Core/Controllers/Record.php | 25 +++- .../Espo/Core/Controllers/RecordTree.php | 22 ++- application/Espo/Core/Record/Collection.php | 74 ++++++++++ .../Espo/Core/RecordServiceContainer.php | 4 +- .../Core/Services/{Record.php => Crud.php} | 4 +- .../Espo/Modules/Crm/Services/MassEmail.php | 9 +- .../Espo/Modules/Crm/Services/TargetList.php | 17 ++- application/Espo/ORM/EntityCollection.php | 4 +- application/Espo/ORM/EntityManager.php | 2 +- application/Espo/ORM/ICollection.php | 2 +- .../Espo/Services/ActionHistoryRecord.php | 11 +- .../Espo/Services/AdminNotifications.php | 32 ++--- application/Espo/Services/Attachment.php | 10 +- application/Espo/Services/AuthLogRecord.php | 5 - application/Espo/Services/AuthToken.php | 4 - .../Espo/Services/DashboardTemplate.php | 6 +- application/Espo/Services/DataPrivacy.php | 130 ++++++------------ application/Espo/Services/Record.php | 29 ++-- 18 files changed, 210 insertions(+), 180 deletions(-) create mode 100644 application/Espo/Core/Record/Collection.php rename application/Espo/Core/Services/{Record.php => Crud.php} (95%) diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 6ba20c434f..c16fd473c5 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -33,9 +33,12 @@ use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\NotFound; use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Utils\Util; use Espo\Core\Exceptions\ForbiddenSilent; +use Espo\Core\Utils\Util; +use Espo\Core\Utils\ControllerUtil; +use Espo\Core\Record\Collection as RecordCollection; + class Record extends Base { const MAX_SIZE_LIMIT = 200; @@ -130,14 +133,21 @@ class Record extends Base $result = $this->getRecordService()->find($params); + if ($result instanceof RecordCollection) { + return (object) [ + 'total' => $result->getTotal(), + 'list' => $result->getValueMapList(), + ]; + } + if (is_array($result)) { - return [ + return (object) [ 'total' => $result['total'], 'list' => isset($result['collection']) ? $result['collection']->getValueMapList() : $result['list'] ]; } - return [ + return (object) [ 'total' => $result->total, 'list' => isset($result->collection) ? $result->collection->getValueMapList() : $result->list ]; @@ -171,7 +181,7 @@ class Record extends Base protected function fetchListParamsFromRequest(&$params, $request, $data) { - \Espo\Core\Utils\ControllerUtil::fetchListParamsFromRequest($params, $request, $data); + ControllerUtil::fetchListParamsFromRequest($params, $request, $data); } public function actionListLinked($params, $data, $request) @@ -192,6 +202,13 @@ class Record extends Base $result = $this->getRecordService()->findLinked($id, $link, $params); + if ($result instanceof RecordCollection) { + return (object) [ + 'total' => $result->getTotal(), + 'list' => $result->getValueMapList(), + ]; + } + if (is_array($result)) { return [ 'total' => $result['total'], diff --git a/application/Espo/Core/Controllers/RecordTree.php b/application/Espo/Core/Controllers/RecordTree.php index 5917f4bcf8..9f98dfa24f 100644 --- a/application/Espo/Core/Controllers/RecordTree.php +++ b/application/Espo/Core/Controllers/RecordTree.php @@ -29,18 +29,16 @@ namespace Espo\Core\Controllers; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\NotFound; -use \Espo\Core\Exceptions\BadRequest; -use \Espo\Core\Utils\Util; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\NotFound; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Utils\Util; class RecordTree extends Record { public static $defaultAction = 'list'; - // protected $defaultRecordServiceName = 'RecordTree'; - public function actionListTree($params, $data, $request) { if (!$this->getAcl()->check($this->name, 'read')) { @@ -52,14 +50,14 @@ class RecordTree extends Record $maxDepth = $request->get('maxDepth'); $onlyNotEmpty = $request->get('onlyNotEmpty'); - $collection = $this->getRecordService()->getTree($parentId, array( + $collection = $this->getRecordService()->getTree($parentId, [ 'where' => $where, 'onlyNotEmpty' => $onlyNotEmpty - ), 0, $maxDepth); - return array( + ], 0, $maxDepth); + return (object) [ 'list' => $collection->toArray(), - 'path' => $this->getRecordService()->getTreeItemPath($parentId) - ); + 'path' => $this->getRecordService()->getTreeItemPath($parentId), + ]; } public function getActionLastChildrenIdList($params, $data, $request) diff --git a/application/Espo/Core/Record/Collection.php b/application/Espo/Core/Record/Collection.php new file mode 100644 index 0000000000..3edf43692d --- /dev/null +++ b/application/Espo/Core/Record/Collection.php @@ -0,0 +1,74 @@ +collection = $collection; + $this->total = $total; + } + + public function getTotal() : ?int + { + return $this->total; + } + + public function getCollection() : ICollection + { + return $this->collection; + } + + public function getValueMapList() : array + { + if (!$this->collection->getEntityType()) { + $list = []; + foreach ($this->collection as $e) { + $item = $e->getValueMap(); + $item->_scope = $e->getEntityType(); + $list[] = $item; + } + return $list; + } + + return $this->collection->getValueMapList(); + } +} diff --git a/application/Espo/Core/RecordServiceContainer.php b/application/Espo/Core/RecordServiceContainer.php index 0d1592c7ca..2a3ac5ef0a 100644 --- a/application/Espo/Core/RecordServiceContainer.php +++ b/application/Espo/Core/RecordServiceContainer.php @@ -34,7 +34,7 @@ use Espo\Core\Exceptions\Error; use Espo\Core\ServiceFactory; use Espo\Core\Utils\Metadata; -use Espo\Core\Services\Record; +use Espo\Core\Services\Crud; /** * Container for record services. Lazy loading is used. @@ -58,7 +58,7 @@ class RecordServiceContainer $this->metadata = $metadata; } - public function get(string $entityType) : Record + public function get(string $entityType) : Crud { $name = $entityType; diff --git a/application/Espo/Core/Services/Record.php b/application/Espo/Core/Services/Crud.php similarity index 95% rename from application/Espo/Core/Services/Record.php rename to application/Espo/Core/Services/Crud.php index b1e8deb3bd..a48366e951 100644 --- a/application/Espo/Core/Services/Record.php +++ b/application/Espo/Core/Services/Crud.php @@ -33,7 +33,7 @@ use Espo\ORM\Entity; use StdClass; -interface Record +interface Crud { public function create(StdClass $data) : Entity; @@ -42,6 +42,4 @@ interface Record public function update(string $id, StdClass $data) : Entity; public function delete(string $id); - - public function find(array $params) : StdClass; } diff --git a/application/Espo/Modules/Crm/Services/MassEmail.php b/application/Espo/Modules/Crm/Services/MassEmail.php index 2b86b1693c..fd4b1db036 100644 --- a/application/Espo/Modules/Crm/Services/MassEmail.php +++ b/application/Espo/Modules/Crm/Services/MassEmail.php @@ -40,7 +40,7 @@ use Espo\Modules\Crm\Entities\Campaign; use Espo\Core\Mail\Sender; use Laminas\Mail\Message; -use StdClass; +use Espo\Core\Record\Collection as RecordCollection; class MassEmail extends \Espo\Services\Record { @@ -530,7 +530,7 @@ class MassEmail extends \Espo\Services\Record return $this->campaignService; } - protected function findLinkedQueueItems(string $id, array $params) : StdClass + protected function findLinkedQueueItems(string $id, array $params) : RecordCollection { $link = 'queueItems'; @@ -557,10 +557,7 @@ class MassEmail extends \Espo\Services\Record $total = $this->getRepository()->countRelated($entity, $link, $selectParams); - return (object) [ - 'total' => $total, - 'collection' => $collection - ]; + return new RecordCollection($collection, $total); } public function getSmtpAccountDataList() diff --git a/application/Espo/Modules/Crm/Services/TargetList.php b/application/Espo/Modules/Crm/Services/TargetList.php index 6bf4fd8c94..e7a6cdff87 100644 --- a/application/Espo/Modules/Crm/Services/TargetList.php +++ b/application/Espo/Modules/Crm/Services/TargetList.php @@ -35,7 +35,7 @@ use Espo\Core\Exceptions\NotFound; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Forbidden; -use StdClass; +use Espo\Core\Record\Collection as RecordCollection; class TargetList extends \Espo\Services\Record { @@ -234,7 +234,7 @@ class TargetList extends \Espo\Services\Record } } - protected function findLinkedOptedOut(string $id, array $params) : StdClass + protected function findLinkedOptedOut(string $id, array $params) : RecordCollection { $pdo = $this->getEntityManager()->getPDO(); $query = $this->getEntityManager()->getQuery(); @@ -299,10 +299,15 @@ class TargetList extends \Espo\Services\Record $row = $sth->fetch(\PDO::FETCH_ASSOC); $count = $row['count']; - return (object) [ - 'total' => $count, - 'list' => $arr - ]; + $collection = $this->getEntityManager()->createCollection(); + + foreach ($arr as $row) { + $e = $this->getEntityManager()->getEntity($row['_scope']); + $e->set($row); + $collection[] = $e; + } + + return new RecordCollection($collection, $count); } public function optOut(string $id, string $targetType, string $targetId) diff --git a/application/Espo/ORM/EntityCollection.php b/application/Espo/ORM/EntityCollection.php index dcbcef34a1..bde112f3f0 100644 --- a/application/Espo/ORM/EntityCollection.php +++ b/application/Espo/ORM/EntityCollection.php @@ -254,7 +254,7 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable return $arr; } - public function getValueMapList() + public function getValueMapList() : array { return $this->toArray(true); } @@ -269,7 +269,7 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable $this->isFetched = false; } - public function isFetched() + public function isFetched() : bool { return $this->isFetched; } diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 1f320da378..a0258fa889 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -231,7 +231,7 @@ class EntityManager return $this->pdo; } - public function createCollection(?string $entityType = null, $data = []) + public function createCollection(?string $entityType = null, array $data = []) { $collection = new EntityCollection($data, $entityType, $this->entityFactory); return $collection; diff --git a/application/Espo/ORM/ICollection.php b/application/Espo/ORM/ICollection.php index 7850fbeb9d..f094d9a653 100644 --- a/application/Espo/ORM/ICollection.php +++ b/application/Espo/ORM/ICollection.php @@ -31,5 +31,5 @@ namespace Espo\ORM; interface ICollection { - + public function getValueMapList() : array; } diff --git a/application/Espo/Services/ActionHistoryRecord.php b/application/Espo/Services/ActionHistoryRecord.php index 896cc5a837..ee519dd866 100644 --- a/application/Espo/Services/ActionHistoryRecord.php +++ b/application/Espo/Services/ActionHistoryRecord.php @@ -29,9 +29,11 @@ namespace Espo\Services; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Exceptions\NotFound; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\NotFound; + +use Espo\ORM\Entity; class ActionHistoryRecord extends Record { @@ -41,7 +43,7 @@ class ActionHistoryRecord extends Record protected $forceSelectAllAttributes = true; - public function loadParentNameFields(\Espo\ORM\Entity $entity) + public function loadParentNameFields(Entity $entity) { if ($entity->get('targetId') && $entity->get('targetType')) { $repository = $this->getEntityManager()->getRepository($entity->get('targetType')); @@ -58,4 +60,3 @@ class ActionHistoryRecord extends Record } } } - diff --git a/application/Espo/Services/AdminNotifications.php b/application/Espo/Services/AdminNotifications.php index 467306c220..46ca5f2502 100644 --- a/application/Espo/Services/AdminNotifications.php +++ b/application/Espo/Services/AdminNotifications.php @@ -29,18 +29,22 @@ namespace Espo\Services; -class AdminNotifications extends \Espo\Core\Services\Base +use Espo\Core\Di; + +class AdminNotifications implements + + Di\ConfigAware, + Di\EntityManagerAware { + use Di\ConfigSetter; + use Di\EntityManagerSetter; + /** - * Job for checking a new version of EspoCRM - * - * @param object $data - * - * @return boolean + * Job for checking a new version of EspoCRM. */ public function jobCheckNewVersion($data) { - $config = $this->getConfig(); + $config = $this->config; if (!$config->get('adminNotifications') || !$config->get('adminNotificationsNewVersion')) { return true; @@ -72,7 +76,7 @@ class AdminNotifications extends \Espo\Core\Services\Base } /** - * Job for cheking a new version of installed extensions + * Job for cheking a new version of installed extensions. * * @param object $data * @@ -80,13 +84,13 @@ class AdminNotifications extends \Espo\Core\Services\Base */ public function jobCheckNewExtensionVersion($data) { - $config = $this->getConfig(); + $config = $this->config; if (!$config->get('adminNotifications') || !$config->get('adminNotificationsNewExtensionVersion')) { return true; } - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); $query = " SELECT id, name, version, check_version_url as url @@ -153,13 +157,8 @@ class AdminNotifications extends \Espo\Core\Services\Base /** * Get latest version - * - * @param string $url - * @param array $requestData - * - * @return array|null */ - protected function getLatestRelease($url = null, array $requestData = [], $urlPath = 'release/latest') + protected function getLatestRelease(?string $url = null, array $requestData = [], string $urlPath = 'release/latest') { if (function_exists('curl_version')) { $ch = curl_init(); @@ -183,5 +182,6 @@ class AdminNotifications extends \Espo\Core\Services\Base } } } + return null; } } diff --git a/application/Espo/Services/Attachment.php b/application/Espo/Services/Attachment.php index 6ba67492b1..dec6574b58 100644 --- a/application/Espo/Services/Attachment.php +++ b/application/Espo/Services/Attachment.php @@ -29,12 +29,12 @@ namespace Espo\Services; -use \Espo\ORM\Entity; +use Espo\ORM\Entity; -use \Espo\Core\Exceptions\BadRequest; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Exceptions\NotFound; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\NotFound; class Attachment extends Record { diff --git a/application/Espo/Services/AuthLogRecord.php b/application/Espo/Services/AuthLogRecord.php index 3d4609b893..a4ba61685f 100644 --- a/application/Espo/Services/AuthLogRecord.php +++ b/application/Espo/Services/AuthLogRecord.php @@ -29,10 +29,6 @@ namespace Espo\Services; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Exceptions\NotFound; - class AuthLogRecord extends Record { protected $internalAttributeList = []; @@ -40,5 +36,4 @@ class AuthLogRecord extends Record protected $actionHistoryDisabled = true; protected $forceSelectAllAttributes = true; - } diff --git a/application/Espo/Services/AuthToken.php b/application/Espo/Services/AuthToken.php index e8d9dae92a..c117d8daed 100644 --- a/application/Espo/Services/AuthToken.php +++ b/application/Espo/Services/AuthToken.php @@ -29,10 +29,6 @@ namespace Espo\Services; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Exceptions\NotFound; - class AuthToken extends Record { protected $actionHistoryDisabled = true; diff --git a/application/Espo/Services/DashboardTemplate.php b/application/Espo/Services/DashboardTemplate.php index 9e18bbae28..64d30daa58 100644 --- a/application/Espo/Services/DashboardTemplate.php +++ b/application/Espo/Services/DashboardTemplate.php @@ -29,10 +29,10 @@ namespace Espo\Services; -use \Espo\ORM\Entity; +use Espo\ORM\Entity; -use \Espo\Core\Exceptions\NotFound; -use \Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\NotFound; +use Espo\Core\Exceptions\Forbidden; class DashboardTemplate extends Record { diff --git a/application/Espo/Services/DataPrivacy.php b/application/Espo/Services/DataPrivacy.php index d2f1d0ca30..f736276945 100644 --- a/application/Espo/Services/DataPrivacy.php +++ b/application/Espo/Services/DataPrivacy.php @@ -29,79 +29,55 @@ namespace Espo\Services; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\NotFound; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\NotFound; use Espo\ORM\Entity; -use \Espo\Core\Htmlizer\Htmlizer; +use Espo\Core\Di; -class DataPrivacy extends \Espo\Core\Services\Base +class DataPrivacy implements + + Di\AclAware, + Di\AclManagerAware, + Di\MetadataAware, + Di\ServiceFactoryAware, + Di\EntityManagerAware, + Di\FieldManagerUtilAware, + Di\UserAware { - protected function init() - { - $this->addDependency('fileManager'); - $this->addDependency('acl'); - $this->addDependency('aclManager'); - $this->addDependency('metadata'); - $this->addDependency('serviceFactory'); - $this->addDependency('dateTime'); - $this->addDependency('number'); - $this->addDependency('entityManager'); - $this->addDependency('defaultLanguage'); - $this->addDependency('fieldManagerUtil'); - $this->addDependency('user'); - } + use Di\AclSetter; + use Di\AclManagerSetter; + use Di\MetadataSetter; + use Di\ServiceFactorySetter; + use Di\EntityManagerSetter; + use Di\FieldManagerUtilSetter; + use Di\UserSetter; - protected function getAcl() + public function erase(string $entityType, string $id, array $fieldList) { - return $this->getInjection('acl'); - } - - protected function getMetadata() - { - return $this->getInjection('metadata'); - } - - protected function getServiceFactory() - { - return $this->getInjection('serviceFactory'); - } - - protected function getFileManager() - { - return $this->getInjection('fileManager'); - } - - protected function getEntityManager() - { - return $this->getInjection('entityManager'); - } - - public function erase($entityType, $id, array $fieldList) - { - if ($this->getAcl()->get('dataPrivacyPermission') === 'no') { + if ($this->acl->get('dataPrivacyPermission') === 'no') { throw new Forbidden(); } - if ($this->getServiceFactory()->checkExists($entityType)) { - $service = $this->getServiceFactory()->create($entityType); + if ($this->serviceFactory->checkExists($entityType)) { + $service = $this->serviceFactory->create($entityType); } else { - $service = $this->getServiceFactory()->create('Record'); + $service = $this->serviceFactory->create('Record'); $service->setEntityType($entityType); } - $entity = $this->getEntityManager()->getEntity($entityType, $id); + $entity = $this->entityManager->getEntity($entityType, $id); if (!$entity) { throw new NotFound(); } - if (!$this->getAcl()->check($entity, 'edit')) { + if (!$this->acl->check($entity, 'edit')) { throw new Forbidden("No edit access."); } - $forbiddenFieldList = $this->getAcl()->getScopeForbiddenFieldList($entityType, 'edit'); + $forbiddenFieldList = $this->acl->getScopeForbiddenFieldList($entityType, 'edit'); foreach ($fieldList as $field) { if (in_array($field, $forbiddenFieldList)) { @@ -111,10 +87,10 @@ class DataPrivacy extends \Espo\Core\Services\Base $service->loadAdditionalFields($entity); - $filedManager = $this->getInjection('fieldManagerUtil'); + $filedManager = $this->fieldManagerUtil; foreach ($fieldList as $field) { - $type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']); + $type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']); $attributeList = $filedManager->getActualAttributeList($entityType, $field); if ($type === 'email') { @@ -122,13 +98,13 @@ class DataPrivacy extends \Espo\Core\Services\Base foreach ($emailAddressList as $emailAddress) { if ( $this - ->getInjection('aclManager') - ->getImplementation('EmailAddress') - ->checkEditInEntity($this->getInjection('user'), $emailAddress, $entity) + ->aclManager + ->getImplementation('EmailAddress') + ->checkEditInEntity($this->user, $emailAddress, $entity) ) { $emailAddress->set('name', 'ERASED:' . $emailAddress->id); $emailAddress->set('optOut', true); - $this->getEntityManager()->saveEntity($emailAddress); + $this->entityManager->saveEntity($emailAddress); } } @@ -142,12 +118,12 @@ class DataPrivacy extends \Espo\Core\Services\Base foreach ($phoneNumberList as $phoneNumber) { if ( $this - ->getInjection('aclManager') - ->getImplementation('PhoneNumber') - ->checkEditInEntity($this->getInjection('user'), $phoneNumber, $entity) + ->aclManager + ->getImplementation('PhoneNumber') + ->checkEditInEntity($this->user, $phoneNumber, $entity) ) { $phoneNumber->set('name', 'ERASED:' . $phoneNumber->id); - $this->getEntityManager()->saveEntity($phoneNumber); + $this->entityManager->saveEntity($phoneNumber); } } @@ -159,15 +135,15 @@ class DataPrivacy extends \Espo\Core\Services\Base else if ($type === 'file' || $type === 'image') { $attachmentId = $entity->get($field . 'Id'); if ($attachmentId) { - $attachment = $this->getEntityManager()->getEntity('Attachment', $attachmentId); - $this->getEntityManager()->removeEntity($attachment); + $attachment = $this->entityManager->getEntity('Attachment', $attachmentId); + $this->entityManager->removeEntity($attachment); } } else if ($type === 'attachmentMultiple') { $attachmentList = $entity->get($field); foreach ($attachmentList as $attachment) { - $this->getEntityManager()->removeEntity($attachment); + $this->entityManager->removeEntity($attachment); } } @@ -180,32 +156,8 @@ class DataPrivacy extends \Espo\Core\Services\Base } } - $this->getEntityManager()->saveEntity($entity); + $this->entityManager->saveEntity($entity); return true; } - - public function exportPdf() - { - - - $htmlizer = new Htmlizer( - $this->getFileManager(), - $this->getInjection('dateTime'), - $this->getInjection('number'), - $this->getAcl(), - $this->getInjection('entityManager'), - $this->getInjection('metadata'), - $this->getInjection('defaultLanguage') - ); - - $pdf = new \Espo\Core\Pdf\Tcpdf(); - - $fontFace = $this->getConfig()->get('pdfFontFace', $this->fontFace); - - $pdf->setFont($fontFace, '', $this->fontSize, '', true); - $pdf->setPrintHeader(false); - - } } - diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 94febb4084..af44866637 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -47,7 +47,8 @@ use Espo\Core\{ ORM\EntityManager, AclManager, Utils\Util, - Services\Record as RecordServiceInterface, + Services\Crud, + Record\Collection as RecordCollection, }; use Espo\Core\Di; @@ -58,7 +59,7 @@ use StdClass; * A layer between Controller and Repository. For CRUD and other operations with records. * If a service with the name of an entity type exists then it will be used instead this one. */ -class Record implements RecordServiceInterface, +class Record implements Crud, Di\ConfigAware, Di\ServiceFactoryAware, @@ -1129,7 +1130,7 @@ class Record implements RecordServiceInterface, return $this->find($params); } - public function find(array $params) : StdClass + public function find(array $params) : RecordCollection { $disableCount = false; if ( @@ -1183,10 +1184,7 @@ class Record implements RecordServiceInterface, } } - return (object) [ - 'total' => $total, - 'collection' => $collection, - ]; + return new RecordCollection($collection, $total); } public function getListKanban(array $params) : StdClass @@ -1336,7 +1334,7 @@ class Record implements RecordServiceInterface, return $this->findLinked($id, $link, $params); } - public function findLinked(string $id, string $link, array $params) : StdClass + public function findLinked(string $id, string $link, array $params) : RecordCollection { $entity = $this->getRepository()->get($id); if (!$entity) { @@ -1450,10 +1448,7 @@ class Record implements RecordServiceInterface, } } - return (object) [ - 'total' => $total, - 'collection' => $collection, - ]; + return new RecordCollection($collection, $total); } public function linkEntity($id, $link, $foreignId) //TODO Remove in 5.8 @@ -2684,8 +2679,9 @@ class Record implements RecordServiceInterface, return $list; } - protected function convertEntityCurrency(Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null) - { + protected function convertEntityCurrency( + Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null + ) { if (!$this->getAcl()->check($entity, 'edit')) return; $data = $this->getConvertCurrencyValues($entity, $targetCurrency, $baseCurrency, $rates, $allFields, $fieldList); @@ -2697,8 +2693,9 @@ class Record implements RecordServiceInterface, } } - public function getConvertCurrencyValues(Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null) - { + public function getConvertCurrencyValues( + Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null + ) { $fieldList = $fieldList ?? $this->getConvertCurrencyFieldList(); $data = (object) [];