From 200fa4dbd3b08bb609b4e6cdaad44d50d7f15698 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 27 Nov 2023 15:20:00 +0200 Subject: [PATCH] ref --- .../RealEstate/Controllers/Contact.php | 1 - .../RealEstate/Controllers/Opportunity.php | 1 - .../Controllers/RealEstateLocation.php | 5 +- .../RealEstateMatchingConfiguration.php | 40 ++++++++--- .../Controllers/RealEstateProperty.php | 22 +++++-- .../Controllers/RealEstateRequest.php | 22 +++++-- .../RealEstateProperty/EmailRequester.php | 8 +-- .../RealEstateRequest/EmailRequester.php | 8 +-- .../Jobs/PropertyMatchingUpdate.php | 2 +- .../RealEstate/Jobs/SendPropertyMatches.php | 2 +- .../RealEstate/Repositories/Contact.php | 1 - .../Repositories/RealEstateLocation.php | 5 +- .../Repositories/RealEstateProperty.php | 16 ++--- .../Repositories/RealEstateRequest.php | 22 ++++--- .../Services/ActivitiesRealEstateProperty.php | 27 ++++---- .../Services/ActivitiesRealEstateRequest.php | 22 +++---- .../Modules/RealEstate/Services/Contact.php | 1 - .../RealEstate/Services/Opportunity.php | 1 - .../RealEstateMatchingConfiguration.php | 21 ++---- .../Services/RealEstateProperty.php | 66 +++++++++++++------ .../RealEstate/Services/RealEstateRequest.php | 65 +++++++++++------- .../Services/RealEstateSendMatches.php | 66 ++++++++----------- 22 files changed, 243 insertions(+), 181 deletions(-) diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/Contact.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/Contact.php index bf6fe2b..e2ec4e9 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/Contact.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/Contact.php @@ -31,5 +31,4 @@ class Contact extends \Espo\Modules\Crm\Controllers\Contact { - } diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/Opportunity.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/Opportunity.php index 1588f9b..3844a40 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/Opportunity.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/Opportunity.php @@ -31,5 +31,4 @@ class Opportunity extends \Espo\Modules\Crm\Controllers\Opportunity { - } diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateLocation.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateLocation.php index 837c6d5..d78e6b3 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateLocation.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateLocation.php @@ -29,7 +29,8 @@ namespace Espo\Modules\RealEstate\Controllers; -class RealEstateLocation extends \Espo\Core\Templates\Controllers\CategoryTree -{ +use Espo\Core\Templates\Controllers\CategoryTree; +class RealEstateLocation extends CategoryTree +{ } diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateMatchingConfiguration.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateMatchingConfiguration.php index aafd6fd..4e7e7a4 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateMatchingConfiguration.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateMatchingConfiguration.php @@ -29,24 +29,44 @@ namespace Espo\Modules\RealEstate\Controllers; -use \Espo\Core\Exceptions\Forbidden; -use \Espo\Core\Exceptions\BadRequest; -use \Espo\Core\Exceptions\NotFound; +use Espo\Core\Api\Request; +use Espo\Core\DataManager; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\ServiceFactory; +use Espo\Entities\User; -class RealEstateMatchingConfiguration extends \Espo\Core\Controllers\Base +class RealEstateMatchingConfiguration { - protected function checkControllerAccess() - { - if (!$this->getUser()->isAdmin()) { + /** + * @throws Forbidden + */ + public function __construct( + private ServiceFactory $serviceFactory, + private User $user, + private DataManager $dataManager + ) { + + if (!$this->user->isAdmin()) { throw new Forbidden(); } } - public function putActionUpdate($params, $data, $request) + /** + * @throws Forbidden + * @throws Error + */ + public function putActionUpdate(Request $request): bool { - $this->getServiceFactory()->create('RealEstateMatchingConfiguration')->setMatchingParameters($data); + if (!$this->user->isAdmin()) { + throw new Forbidden(); + } + + $data = $request->getParsedBody(); + + $this->serviceFactory->create('RealEstateMatchingConfiguration')->setMatchingParameters($data); - $this->getContainer()->get('dataManager')->rebuild(); + $this->dataManager->rebuild(); return true; } diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateProperty.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateProperty.php index f4a0321..c645d03 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateProperty.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateProperty.php @@ -29,28 +29,40 @@ namespace Espo\Modules\RealEstate\Controllers; +use Espo\Core\Api\Request; +use Espo\Core\Controllers\Record; use Espo\Core\Exceptions\BadRequest; -class RealEstateProperty extends \Espo\Core\Templates\Controllers\Base +class RealEstateProperty extends Record { - public function postActionSetNotInterested($params, $data, $request) + /** + * @throws BadRequest + */ + public function postActionSetNotInterested(Request $request): bool { + $data = $request->getParsedBody(); + if (empty($data->requestId) || empty($data->propertyId)) { throw new BadRequest(); } - $this->getRecordService()->setNotIntereseted($data->propertyId, $data->requestId); + $this->getRecordService()->setNotInterested($data->propertyId, $data->requestId); return true; } - public function postActionUnsetNotInterested($params, $data, $request) + /** + * @throws BadRequest + */ + public function postActionUnsetNotInterested(Request $request): bool { + $data = $request->getParsedBody(); + if (empty($data->requestId) || empty($data->propertyId)) { throw new BadRequest(); } - $this->getRecordService()->unsetNotIntereseted($data->propertyId, $data->requestId); + $this->getRecordService()->unsetNotInterested($data->propertyId, $data->requestId); return true; } diff --git a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateRequest.php b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateRequest.php index 4895c2e..45eb3b2 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateRequest.php +++ b/src/files/custom/Espo/Modules/RealEstate/Controllers/RealEstateRequest.php @@ -29,28 +29,40 @@ namespace Espo\Modules\RealEstate\Controllers; +use Espo\Core\Api\Request; +use Espo\Core\Controllers\Record; use Espo\Core\Exceptions\BadRequest; -class RealEstateRequest extends \Espo\Core\Templates\Controllers\Base +class RealEstateRequest extends Record { - public function postActionSetNotInterested($params, $data, $request) + /** + * @throws BadRequest + */ + public function postActionSetNotInterested(Request $request): bool { + $data = $request->getParsedBody(); + if (empty($data->requestId) || empty($data->propertyId)) { throw new BadRequest(); } - $this->getRecordService()->setNotIntereseted($data->requestId, $data->propertyId); + $this->getRecordService()->setNotInterested($data->requestId, $data->propertyId); return true; } - public function postActionUnsetNotInterested($params, $data, $request) + /** + * @throws BadRequest + */ + public function postActionUnsetNotInterested(Request $request): bool { + $data = $request->getParsedBody(); + if (empty($data->requestId) || empty($data->propertyId)) { throw new BadRequest(); } - $this->getRecordService()->unsetNotIntereseted($data->requestId, $data->propertyId); + $this->getRecordService()->unsetNotInterested($data->requestId, $data->propertyId); return true; } diff --git a/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateProperty/EmailRequester.php b/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateProperty/EmailRequester.php index 34c0e17..738bd80 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateProperty/EmailRequester.php +++ b/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateProperty/EmailRequester.php @@ -38,11 +38,9 @@ class EmailRequester { public static $order = 16; - private $config; - - private $metadata; - - private $entityManager; + private Config $config; + private Metadata $metadata; + private EntityManager $entityManager; public function __construct(Config $config, Metadata $metadata, EntityManager $entityManager) { diff --git a/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateRequest/EmailRequester.php b/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateRequest/EmailRequester.php index 7b654ee..52d9ef6 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateRequest/EmailRequester.php +++ b/src/files/custom/Espo/Modules/RealEstate/Hooks/RealEstateRequest/EmailRequester.php @@ -38,11 +38,9 @@ class EmailRequester { public static $order = 16; - private $config; - - private $metadata; - - private $entityManager; + private Config $config; + private Metadata $metadata; + private EntityManager $entityManager; public function __construct(Config $config, Metadata $metadata, EntityManager $entityManager) { diff --git a/src/files/custom/Espo/Modules/RealEstate/Jobs/PropertyMatchingUpdate.php b/src/files/custom/Espo/Modules/RealEstate/Jobs/PropertyMatchingUpdate.php index 2aa3bc4..42147ad 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Jobs/PropertyMatchingUpdate.php +++ b/src/files/custom/Espo/Modules/RealEstate/Jobs/PropertyMatchingUpdate.php @@ -34,7 +34,7 @@ class PropertyMatchingUpdate implements JobDataLess { - private $serviceFactory; + private ServiceFactory $serviceFactory; public function __construct(ServiceFactory $serviceFactory) { diff --git a/src/files/custom/Espo/Modules/RealEstate/Jobs/SendPropertyMatches.php b/src/files/custom/Espo/Modules/RealEstate/Jobs/SendPropertyMatches.php index 5f5840d..26c9cc8 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Jobs/SendPropertyMatches.php +++ b/src/files/custom/Espo/Modules/RealEstate/Jobs/SendPropertyMatches.php @@ -34,7 +34,7 @@ class SendPropertyMatches implements JobDataLess { - protected $serviceFactory; + private ServiceFactory $serviceFactory; public function __construct(ServiceFactory $serviceFactory) { diff --git a/src/files/custom/Espo/Modules/RealEstate/Repositories/Contact.php b/src/files/custom/Espo/Modules/RealEstate/Repositories/Contact.php index ecdb838..c6c51f3 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Repositories/Contact.php +++ b/src/files/custom/Espo/Modules/RealEstate/Repositories/Contact.php @@ -31,5 +31,4 @@ class Contact extends \Espo\Modules\Crm\Repositories\Contact { - } diff --git a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateLocation.php b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateLocation.php index fe3d6a0..880fdd9 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateLocation.php +++ b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateLocation.php @@ -29,7 +29,8 @@ namespace Espo\Modules\RealEstate\Repositories; -class RealEstateLocation extends \Espo\Core\Repositories\CategoryTree -{ +use Espo\Core\Repositories\CategoryTree; +class RealEstateLocation extends CategoryTree +{ } diff --git a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php index 0c0fa1b..92bf484 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php +++ b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php @@ -29,25 +29,19 @@ namespace Espo\Modules\RealEstate\Repositories; +use Espo\Core\Repositories\Database; use Espo\ORM\Entity; -class RealEstateProperty extends \Espo\Core\Templates\Repositories\Base +class RealEstateProperty extends Database { - protected function init() - { - parent::init(); - - $this->addDependency('serviceFactory'); - } - public function beforeSave(Entity $entity, array $options = []) { $propertyType = $entity->get('type'); - $fieldList = $this->getMetadata() + $fieldList = $this->metadata ->get(['entityDefs', 'RealEstateProperty', 'propertyTypes', $propertyType, 'fieldList'], []); - $fieldDefs = $this->getMetadata()->get(['entityDefs', 'RealEstateProperty', 'fields'], []); + $fieldDefs = $this->metadata->get(['entityDefs', 'RealEstateProperty', 'fields'], []); foreach ($fieldDefs as $field => $defs) { if (empty($defs['isMatching'])) { @@ -80,6 +74,6 @@ public function beforeSave(Entity $entity, array $options = []) $entity->set('name', $name); - return parent::beforeSave($entity, $options); + parent::beforeSave($entity, $options); } } diff --git a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php index 7e6421c..10d1479 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php +++ b/src/files/custom/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php @@ -29,18 +29,19 @@ namespace Espo\Modules\RealEstate\Repositories; +use Espo\Core\Repositories\Database; use Espo\ORM\Entity; -class RealEstateRequest extends \Espo\Core\Templates\Repositories\Base +class RealEstateRequest extends Database { public function beforeSave(Entity $entity, array $options = []) { $propertyType = $entity->get('propertyType'); - $fieldList = $this->getMetadata() + $fieldList = $this->metadata ->get(['entityDefs', 'RealEstateProperty', 'propertyTypes', $propertyType, 'fieldList'], []); - $fieldDefs = $this->getMetadata()->get(['entityDefs', 'RealEstateProperty', 'fields'], []); + $fieldDefs = $this->metadata->get(['entityDefs', 'RealEstateProperty', 'fields'], []); foreach ($fieldDefs as $field => $defs) { if (empty($defs['isMatching'])) { @@ -53,31 +54,32 @@ public function beforeSave(Entity $entity, array $options = []) } } - return parent::beforeSave($entity, $options); + parent::beforeSave($entity, $options); } public function afterSave(Entity $entity, array $options = []) { - $result = parent::afterSave($entity, $options); + parent::afterSave($entity, $options); - $this->handleAfterSaveContacts($entity, $options); + $this->handleAfterSaveContacts($entity); if ($entity->isNew() && !$entity->get('name')) { - $e = $this->get($entity->getId()); + $e = $this->getById($entity->getId()); + $name = strval($e->get('number')); $name = str_pad($name, 6, '0', STR_PAD_LEFT); $name = 'R ' . $name; $e->set('name', $name); + $this->save($e); + $entity->set('name', $name); $entity->set('number', $e->get('number')); } - - return $result; } - protected function handleAfterSaveContacts(Entity $entity, array $options = []) + private function handleAfterSaveContacts(Entity $entity): void { $contactIdChanged = $entity->has('contactId') && $entity->get('contactId') != $entity->getFetched('contactId'); diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateProperty.php b/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateProperty.php index c95bca7..6ccf8b1 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateProperty.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateProperty.php @@ -29,33 +29,29 @@ namespace Espo\Modules\RealEstate\Services; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Forbidden; use Espo\ORM\Entity; use Espo\ORM\Query\Select; use Espo\ORM\Query\SelectBuilder; - -use Espo\Core\Select\SelectManagerFactory; use Espo\Core\Select\SelectBuilderFactory; -use Espo\Core\Utils\Metadata; - class ActivitiesRealEstateProperty { - protected $metadata; - - protected $selectManagerFactory; - - private $selectBuilderFactory; + private SelectBuilderFactory $selectBuilderFactory; public function __construct( - Metadata $metadata, - SelectManagerFactory $selectManagerFactory, SelectBuilderFactory $selectBuilderFactory ) { - $this->metadata = $metadata; - $this->selectManagerFactory = $selectManagerFactory; $this->selectBuilderFactory = $selectBuilderFactory; } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function getActivitiesMeetingQuery(Entity $entity, array $statusList): Select { $builder = $this->selectBuilderFactory @@ -109,6 +105,11 @@ public function getActivitiesMeetingQuery(Entity $entity, array $statusList): Se return $builder->build(); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function getActivitiesCallQuery(Entity $entity, array $statusList): Select { $builder = $this->selectBuilderFactory diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateRequest.php b/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateRequest.php index 92d78b4..2beee31 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateRequest.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/ActivitiesRealEstateRequest.php @@ -29,33 +29,29 @@ namespace Espo\Modules\RealEstate\Services; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Forbidden; use Espo\ORM\Entity; use Espo\ORM\Query\Select; use Espo\ORM\Query\SelectBuilder; - -use Espo\Core\Select\SelectManagerFactory; use Espo\Core\Select\SelectBuilderFactory; -use Espo\Core\Utils\Metadata; - class ActivitiesRealEstateRequest { - protected $metadata; - - protected $selectManagerFactory; - - private $selectBuilderFactory; + private SelectBuilderFactory $selectBuilderFactory; public function __construct( - Metadata $metadata, - SelectManagerFactory $selectManagerFactory, SelectBuilderFactory $selectBuilderFactory ) { - $this->metadata = $metadata; - $this->selectManagerFactory = $selectManagerFactory; $this->selectBuilderFactory = $selectBuilderFactory; } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function getActivitiesMeetingQuery(Entity $entity, array $statusList): Select { $builder = $this->selectBuilderFactory diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/Contact.php b/src/files/custom/Espo/Modules/RealEstate/Services/Contact.php index 08dc8a1..eacbb51 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/Contact.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/Contact.php @@ -31,5 +31,4 @@ class Contact extends \Espo\Modules\Crm\Services\Contact { - } diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/Opportunity.php b/src/files/custom/Espo/Modules/RealEstate/Services/Opportunity.php index 5ecf692..ae334ae 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/Opportunity.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/Opportunity.php @@ -31,5 +31,4 @@ class Opportunity extends \Espo\Modules\Crm\Services\Opportunity { - } diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateMatchingConfiguration.php b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateMatchingConfiguration.php index 26795ba..e59a5fc 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateMatchingConfiguration.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateMatchingConfiguration.php @@ -29,21 +29,15 @@ namespace Espo\Modules\RealEstate\Services; -use Espo\Core\Exceptions\Forbidden; -use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\NotFound; +use Espo\Core\Utils\Language; +use Espo\Core\Utils\Metadata; -use Espo\Core\{ - Utils\Metadata, - Utils\Language, -}; - -use StdClass; +use stdClass; class RealEstateMatchingConfiguration { - protected $metadata; - protected $baseLanguage; + private Metadata $metadata; + private Language $baseLanguage; public function __construct(Metadata $metadata, Language $baseLanguage) { @@ -51,9 +45,8 @@ public function __construct(Metadata $metadata, Language $baseLanguage) $this->baseLanguage = $baseLanguage; } - public function setMatchingParameters(StdClass $data) + public function setMatchingParameters(stdClass $data) { - $isMetadataChanged = false; $isLanguageChanged = false; $matchingFieldList = []; @@ -133,8 +126,6 @@ public function setMatchingParameters(StdClass $data) if (!empty($entityDefsData)) { $this->metadata->set('entityDefs', 'RealEstateProperty', $entityDefsData); - - $isMetadataChanged = true; } $requestFieldDefs = []; diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateProperty.php b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateProperty.php index 91cab1e..ccd5c69 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateProperty.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateProperty.php @@ -29,7 +29,10 @@ namespace Espo\Modules\RealEstate\Services; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\ForbiddenSilent; use Espo\Core\Exceptions\NotFound; use Espo\ORM\Entity; @@ -43,16 +46,17 @@ use Espo\Core\Select\Where\Item as WhereItem; use Espo\Core\Record\Collection as RecordCollection; use Espo\Core\Record\FindParams; +use Espo\Services\Record; -class RealEstateProperty extends \Espo\Core\Templates\Services\Base +class RealEstateProperty extends Record { protected $readOnlyAttributeList = [ 'matchingRequestCount', ]; - public function find(SearchParams $params, ?FindParams $findParams = null): RecordCollection + public function find(SearchParams $searchParams, ?FindParams $findParams = null): RecordCollection { - $where = $params->getWhere(); + $where = $searchParams->getWhere(); $itemList = []; @@ -72,20 +76,25 @@ public function find(SearchParams $params, ?FindParams $findParams = null): Reco ->setItemList(array_values($itemList)) ->build(); - $params = $params->withWhere($where); + $searchParams = $searchParams->withWhere($where); if (!$item->getValue() || $item->getType() !== 'equals') { continue; } - return $this->getServiceFactory() + return $this->serviceFactory ->create('RealEstateRequest') - ->findLinkedMatchingProperties($item->getValue(), $params, true); + ->findLinkedMatchingProperties($item->getValue(), $searchParams, true); } - return parent::find($params); + return parent::find($searchParams); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function getMatchingRequestsQuery(Entity $entity, SearchParams $params): Select { $builder = $this->selectBuilderFactory->create(); @@ -181,7 +190,7 @@ public function getMatchingRequestsQuery(Entity $entity, SearchParams $params): ]); } - $fieldDefs = $this->getMetadata()->get(['entityDefs', 'RealEstateProperty', 'fields'], []); + $fieldDefs = $this->metadata->get(['entityDefs', 'RealEstateProperty', 'fields'], []); foreach ($fieldDefs as $field => $defs) { if (empty($defs['isMatching'])) { @@ -227,14 +236,13 @@ public function getMatchingRequestsQuery(Entity $entity, SearchParams $params): } if ($entity->get('price') !== null) { - $defaultCurrency = $this->getConfig()->get('defaultCurrency'); + $defaultCurrency = $this->config->get('defaultCurrency'); $price = $entity->get('price'); $priceCurrency = $entity->get('priceCurrency'); if ($defaultCurrency !== $priceCurrency) { - $rates = $this->getConfig()->get('currencyRates'); - $rate1 = $this->getConfig()->get('currencyRates.' . $priceCurrency, 1.0); + $rates = $this->config->get('currencyRates'); $rate1 = 1.0; @@ -287,13 +295,18 @@ public function getMatchingRequestsQuery(Entity $entity, SearchParams $params): return $queryBuilder->build(); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function findLinkedMatchingRequests( string $id, SearchParams $params, bool $customOrder = false ): RecordCollection { - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); $this->loadAdditionalFields($entity); @@ -310,7 +323,7 @@ public function findLinkedMatchingRequests( ->build(); } - $collection = $this->getEntityManager() + $collection = $this->entityManager ->getRDBRepository('RealEstateRequest') ->clone($query) ->find(); @@ -322,7 +335,7 @@ public function findLinkedMatchingRequests( $recordService->prepareEntityForOutput($e); } - $total = $this->getEntityManager() + $total = $this->entityManager ->getRDBRepository('RealEstateRequest') ->clone($query) ->count(); @@ -340,7 +353,12 @@ public function findLinkedMatchingRequests( return new RecordCollection($collection, $total); } - public function setNotIntereseted(string $propertyId, string $requestId) + /** + * @throws Forbidden + * @throws NotFound + * @throws ForbiddenSilent + */ + public function setNotInterested(string $propertyId, string $requestId): void { $property = $this->getEntity($propertyId); @@ -358,7 +376,12 @@ public function setNotIntereseted(string $propertyId, string $requestId) ->relateById($requestId, ['interestDegree' => 0]); } - public function unsetNotIntereseted(string $propertyId, string $requestId) + /** + * @throws Forbidden + * @throws NotFound + * @throws ForbiddenSilent + */ + public function unsetNotInterested(string $propertyId, string $requestId): void { $property = $this->getEntity($propertyId); @@ -366,7 +389,7 @@ public function unsetNotIntereseted(string $propertyId, string $requestId) throw new NotFound(); } - if (!$this->getAcl()->check($property, 'edit')) { + if (!$this->acl->check($property, 'edit')) { throw new Forbidden(); } @@ -394,9 +417,14 @@ protected function beforeUpdateEntity(Entity $entity, $data) $entity->set('matchingRequestCount', $matchingRequestCount); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function updateMatchingCount() { - $repository = $this->getEntityManager()->getRDBRepository('RealEstateProperty'); + $repository = $this->entityManager->getRDBRepository('RealEstateProperty'); $notActualList = $repository ->select(['id', 'matchingRequestCount']) @@ -427,7 +455,7 @@ public function updateMatchingCount() $query = $this->getMatchingRequestsQuery($e, SearchParams::create()); - $matchingRequestCount = $this->getEntityManager() + $matchingRequestCount = $this->entityManager ->getRDBRepository('RealEstateRequest') ->clone($query) ->count(); diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateRequest.php b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateRequest.php index 547d692..9273d7d 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateRequest.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateRequest.php @@ -29,6 +29,9 @@ namespace Espo\Modules\RealEstate\Services; +use Espo\Core\Exceptions\BadRequest; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\ForbiddenSilent; use Espo\ORM\Entity; use Espo\Core\Exceptions\Forbidden; @@ -44,8 +47,9 @@ use Espo\Core\Select\Where\Item as WhereItem; use Espo\Core\Record\Collection as RecordCollection; use Espo\Core\Record\FindParams; +use Espo\Services\Record; -class RealEstateRequest extends \Espo\Core\Templates\Services\Base +class RealEstateRequest extends Record { protected $readOnlyAttributeList = [ 'matchingPropertyCount', @@ -69,7 +73,7 @@ protected function beforeUpdateEntity(Entity $entity, $data) if ($entity->isActual()) { $query = $this->getMatchingPropertiesQuery($entity, SearchParams::create()); - $matchingPropertyCount = $this->getEntityManager() + $matchingPropertyCount = $this->entityManager ->getRDBRepository('RealEstateProperty') ->clone($query) ->count(); @@ -78,9 +82,9 @@ protected function beforeUpdateEntity(Entity $entity, $data) $entity->set('matchingPropertyCount', $matchingPropertyCount); } - public function find(SearchParams $params, ?FindParams $findParams = null): RecordCollection + public function find(SearchParams $searchParams, ?FindParams $findParams = null): RecordCollection { - $where = $params->getWhere(); + $where = $searchParams->getWhere(); $itemList = []; @@ -100,20 +104,25 @@ public function find(SearchParams $params, ?FindParams $findParams = null): Reco ->setItemList(array_values($itemList)) ->build(); - $params = $params->withWhere($where); + $searchParams = $searchParams->withWhere($where); if (!$item->getValue() || $item->getType() !== 'equals') { continue; } - return $this->getServiceFactory() + return $this->serviceFactory ->create('RealEstateProperty') - ->findLinkedMatchingRequests($item->getValue(), $params, true); + ->findLinkedMatchingRequests($item->getValue(), $searchParams, true); } - return parent::find($params); + return parent::find($searchParams, $findParams); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params): Select { $builder = $this->selectBuilderFactory->create(); @@ -145,8 +154,6 @@ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params) $queryBuilder = $builder->buildQueryBuilder(); - //$selectParams['leftJoins'] = $selectParams['leftJoins'] ?? []; - if (count($locationIdList)) { $queryBuilder ->distinct() @@ -199,7 +206,7 @@ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params) ]); } - $fieldDefs = $this->getMetadata()->get(['entityDefs', 'RealEstateProperty', 'fields'], []); + $fieldDefs = $this->metadata->get(['entityDefs', 'RealEstateProperty', 'fields'], []); foreach ($fieldDefs as $field => $defs) { if (empty($defs['isMatching'])) { @@ -219,14 +226,14 @@ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params) } } - $defaultCurrency = $this->getConfig()->get('defaultCurrency'); + $defaultCurrency = $this->config->get('defaultCurrency'); if ($entity->get('fromPrice') !== null) { $fromPrice = $entity->get('fromPrice'); $fromPriceCurrency = $entity->get('fromPriceCurrency'); - $rates = $this->getConfig()->get('currencyRates'); - $rate1 = $this->getConfig()->get('currencyRates.' . $fromPriceCurrency, 1.0); + $rates = $this->config->get('currencyRates'); + $rate1 = $this->config->get('currencyRates.' . $fromPriceCurrency, 1.0); $rate1 = 1.0; @@ -252,8 +259,7 @@ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params) $toPrice = $entity->get('toPrice'); $toPriceCurrency = $entity->get('toPriceCurrency'); - $rates = $this->getConfig()->get('currencyRates'); - $rate1 = $this->getConfig()->get('currencyRates.' . $toPriceCurrency, 1.0); + $rates = $this->config->get('currencyRates'); $rate1 = 1.0; @@ -278,6 +284,11 @@ public function getMatchingPropertiesQuery(Entity $entity, SearchParams $params) return $queryBuilder->build(); } + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + */ public function findLinkedMatchingProperties( string $id, SearchParams $params, @@ -301,7 +312,7 @@ public function findLinkedMatchingProperties( ->build(); } - $collection = $this->getEntityManager() + $collection = $this->entityManager ->getRDBRepository('RealEstateProperty') ->clone($query) ->find(); @@ -313,7 +324,7 @@ public function findLinkedMatchingProperties( $recordService->prepareEntityForOutput($e); } - $total = $this->getEntityManager() + $total = $this->entityManager ->getRDBRepository('RealEstateProperty') ->clone($query) ->count(); @@ -331,7 +342,12 @@ public function findLinkedMatchingProperties( return new RecordCollection($collection, $total); } - public function setNotIntereseted($requestId, $propertyId) + /** + * @throws Forbidden + * @throws ForbiddenSilent + * @throws NotFound + */ + public function setNotInterested($requestId, $propertyId) { $request = $this->getEntity($requestId); @@ -339,7 +355,7 @@ public function setNotIntereseted($requestId, $propertyId) throw new NotFound(); } - if (!$this->getAcl()->check($request, 'edit')) { + if (!$this->acl->check($request, 'edit')) { throw new Forbidden(); } @@ -349,7 +365,12 @@ public function setNotIntereseted($requestId, $propertyId) ->relateById($propertyId, ['interestDegree' => 0]); } - public function unsetNotIntereseted($requestId, $propertyId) + /** + * @throws Forbidden + * @throws ForbiddenSilent + * @throws NotFound + */ + public function unsetNotInterested($requestId, $propertyId) { $request = $this->getEntity($requestId); @@ -357,7 +378,7 @@ public function unsetNotIntereseted($requestId, $propertyId) throw new NotFound(); } - if (!$this->getAcl()->check($request, 'edit')) { + if (!$this->acl->check($request, 'edit')) { throw new Forbidden(); } diff --git a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateSendMatches.php b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateSendMatches.php index 616fae1..1227d81 100644 --- a/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateSendMatches.php +++ b/src/files/custom/Espo/Modules/RealEstate/Services/RealEstateSendMatches.php @@ -32,54 +32,44 @@ use Espo\Core\Exceptions\NotFound; use Espo\Core\Exceptions\Error; -use Espo\Core\{ - ServiceFactory, - Mail\EmailSender, - Utils\Config, - ORM\EntityManager, -}; +use Espo\Core\Mail\EmailSender; +use Espo\Core\Mail\Exceptions\SendingError; +use Espo\Core\ORM\EntityManager; +use Espo\Core\ServiceFactory; +use Espo\Core\Utils\Config; use Espo\Core\Select\SearchParams; +use Espo\Entities\Email; use Espo\ORM\Query\Part\Order; use Espo\ORM\Query\Part\Expression as Expr; -use Espo\Entities\Preferences; - use Exception; use DateTime; use stdClass; class RealEstateSendMatches { - protected $serviceFactory; - - protected $emailSender; - - protected $preferences; - - protected $config; - - protected $entityManager; + private ServiceFactory $serviceFactory; + private EmailSender $emailSender; + private Config $config; + private EntityManager $entityManager; public function __construct( ServiceFactory $serviceFactory, EmailSender $emailSender, - Preferences $preferences, Config $config, EntityManager $entityManager ) { $this->serviceFactory = $serviceFactory; $this->emailSender = $emailSender; - $this->preferences = $preferences; $this->config = $config; $this->entityManager = $entityManager; } - protected function getSmptParams() - { - return $this->preferences->getSmtpParams(); - } - + /** + * @throws NotFound + * @throws Error + */ public function processRequestJob(stdClass $data): void { if (empty($data->targetId)) { @@ -114,7 +104,7 @@ public function processRequestJob(stdClass $data): void foreach ($propertyList as $property) { if ( $this->entityManager - ->getRepository('RealEstateSendMatchesQueueItem') + ->getRDBRepository('RealEstateSendMatchesQueueItem') ->where([ 'requestId' => $entity->getId(), 'propertyId' => $property->getId() @@ -135,6 +125,10 @@ public function processRequestJob(stdClass $data): void } } + /** + * @throws NotFound + * @throws Error + */ public function processPropertyJob(stdClass $data): void { if (empty($data->targetId)) { @@ -173,7 +167,7 @@ public function processPropertyJob(stdClass $data): void if ( $this->entityManager - ->getRepository('RealEstateSendMatchesQueueItem') + ->getRDBRepository('RealEstateSendMatchesQueueItem') ->where([ 'propertyId' => $entity->getId(), 'requestId' => $request->getId(), @@ -199,7 +193,7 @@ public function processSendingQueue(): void $limit = $this->config->get('realEstateEmailSendingPortionSize', 30); $itemList = $this->entityManager - ->getRepository('RealEstateSendMatchesQueueItem') + ->getRDBRepository('RealEstateSendMatchesQueueItem') ->order('createdAt') ->where([ 'isProcessed' => false @@ -224,7 +218,7 @@ public function processSendingQueue(): void 'propertyId' => $item->get('propertyId'), ]); } - catch (Exception $e) {} + catch (Exception) {} } $this->processCleanup(); @@ -238,7 +232,7 @@ public function processCleanup(): void $datetime->modify($period); $itemList = $this->entityManager - ->getRepository('RealEstateSendMatchesQueueItem') + ->getRDBRepository('RealEstateSendMatchesQueueItem') ->where([ 'isProcessed' => true, 'createdAt<' => $datetime->format('Y-m-d H:i:s') @@ -252,6 +246,11 @@ public function processCleanup(): void } } + /** + * @throws Error + * @throws SendingError + * @throws NotFound + */ public function sendMatchesEmail($data): void { if (empty($data['requestId']) || empty($data['propertyId'])) { @@ -271,8 +270,6 @@ public function sendMatchesEmail($data): void throw new Error('RealEstate EmailSending[' . $request->getId() . ']: No Template in config'); } - $emailBody = ''; - $requestService = $this->serviceFactory->create($request->getEntityType()); $requestService->loadAdditionalFields($request); @@ -363,18 +360,13 @@ public function sendMatchesEmail($data): void } } + /** @var Email $email */ $email = $this->entityManager->getEntity('Email'); $email->set($emailData); $emailSender = $this->emailSender->create(); - $smtpParams = $this->getSmptParams(); - - if ($smtpParams) { - $emailSender->withSmtpParams($smtpParams); - } - $emailSender ->withAttachments($attachmentList) ->send($email);