From b660b204c95fcf9868ccedaa3a51e4f75a1f1902 Mon Sep 17 00:00:00 2001 From: Divesh Pahuja Date: Wed, 28 Dec 2022 17:09:21 +0100 Subject: [PATCH] Use DI for services & remove aliases - resolves #205 --- src/Controller/Admin/ActivitiesController.php | 2 + src/Controller/Admin/DuplicatesController.php | 1 + .../Api/SegmentsOfCustomersController.php | 13 +++- .../DefaultCustomerSaveValidator.php | 6 +- .../DefaultMariaDbDuplicatesIndex.php | 47 +++----------- src/Resources/config/services.yml | 27 +++----- src/SegmentManager/DefaultSegmentManager.php | 61 +++++-------------- 7 files changed, 48 insertions(+), 109 deletions(-) diff --git a/src/Controller/Admin/ActivitiesController.php b/src/Controller/Admin/ActivitiesController.php index 0cac965c..65654617 100644 --- a/src/Controller/Admin/ActivitiesController.php +++ b/src/Controller/Admin/ActivitiesController.php @@ -22,6 +22,7 @@ use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface; use Knp\Component\Pager\PaginatorInterface; use Pimcore\Controller\KernelControllerEventInterface; +use Pimcore\Model\Listing\AbstractListing; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\Routing\Annotation\Route; @@ -57,6 +58,7 @@ public function listAction( DefaultActivityView $defaultActivityView ) { if ($customer = $customerProvider->getById($request->get('customerId'))) { + /** @var AbstractListing $list */ $list = $activityStore->getActivityList(); $list->setCondition('customerId = ' . $customer->getId()); $list->setOrderKey('activityDate'); diff --git a/src/Controller/Admin/DuplicatesController.php b/src/Controller/Admin/DuplicatesController.php index e5ff2b19..514f9194 100644 --- a/src/Controller/Admin/DuplicatesController.php +++ b/src/Controller/Admin/DuplicatesController.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\Service\Attribute\Required; /** * @Route("/duplicates") diff --git a/src/Controller/Rest/Api/SegmentsOfCustomersController.php b/src/Controller/Rest/Api/SegmentsOfCustomersController.php index 1f1c5802..96e5c30b 100644 --- a/src/Controller/Rest/Api/SegmentsOfCustomersController.php +++ b/src/Controller/Rest/Api/SegmentsOfCustomersController.php @@ -17,14 +17,17 @@ use CustomerManagementFrameworkBundle\Controller\Rest\RestHandlerController; use CustomerManagementFrameworkBundle\RESTApi\Exception\ExceptionInterface; +use CustomerManagementFrameworkBundle\RESTApi\SegmentsOfCustomerHandler; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\Service\Attribute\Required; /** * @Route("/segments-of-customers") */ class SegmentsOfCustomersController extends RestHandlerController { + protected SegmentsOfCustomerHandler $handler; /** * @param Request $request * @Route("", methods={"PUT", "POST"}) @@ -47,10 +50,16 @@ public function updateRecordsAction(Request $request) } /** - * @return \CustomerManagementFrameworkBundle\RESTApi\SegmentsOfCustomerHandler + * @return SegmentsOfCustomerHandler */ protected function getHandler() { - return \Pimcore::getContainer()->get('cmf.rest.segments_of_customer_handler'); + return $this->handler; + } + + #[Required] + protected function setHandler(SegmentsOfCustomerHandler $handler) + { + $this->handler = $handler; } } diff --git a/src/CustomerSaveValidator/DefaultCustomerSaveValidator.php b/src/CustomerSaveValidator/DefaultCustomerSaveValidator.php index 027598cf..94905dc2 100644 --- a/src/CustomerSaveValidator/DefaultCustomerSaveValidator.php +++ b/src/CustomerSaveValidator/DefaultCustomerSaveValidator.php @@ -17,6 +17,7 @@ use CustomerManagementFrameworkBundle\CustomerDuplicatesService\CustomerDuplicatesServiceInterface; use CustomerManagementFrameworkBundle\CustomerSaveValidator\Exception\DuplicateCustomerException; +use CustomerManagementFrameworkBundle\Model\AbstractCustomer; use CustomerManagementFrameworkBundle\Model\CustomerInterface; use Pimcore\Model\Element\ValidationException; @@ -113,8 +114,9 @@ protected function validateDuplicates(CustomerInterface $customer) ); if (!is_null($duplicates) && $duplicates->getCount()) { $ex = new DuplicateCustomerException('Duplicate customer found: ID '.$duplicates->current()); - - $ex->setDuplicateCustomer($duplicates->current()); + /** @var AbstractCustomer $duplicate */ + $duplicate = $duplicates->current(); + $ex->setDuplicateCustomer($duplicate); $ex->setMatchedDuplicateFields( $this->customerDuplicatesService->getMatchedDuplicateFields() ); diff --git a/src/DuplicatesIndex/DefaultMariaDbDuplicatesIndex.php b/src/DuplicatesIndex/DefaultMariaDbDuplicatesIndex.php index f1b064dc..b8d2d9be 100644 --- a/src/DuplicatesIndex/DefaultMariaDbDuplicatesIndex.php +++ b/src/DuplicatesIndex/DefaultMariaDbDuplicatesIndex.php @@ -19,6 +19,7 @@ use CustomerManagementFrameworkBundle\DataTransformer\DataTransformerInterface; use CustomerManagementFrameworkBundle\DataTransformer\DuplicateIndex\Standard; use CustomerManagementFrameworkBundle\Factory; +use CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItemInterface; use CustomerManagementFrameworkBundle\Model\CustomerInterface; use CustomerManagementFrameworkBundle\Traits\LoggerAware; use Knp\Component\Pager\PaginatorInterface; @@ -38,49 +39,18 @@ class DefaultMariaDbDuplicatesIndex implements DuplicatesIndexInterface const POTENTIAL_DUPLICATES_TABLE = 'plugin_cmf_potential_duplicates'; const FALSE_POSITIVES_TABLE = 'plugin_cmf_duplicates_false_positives'; - /** - * @var array - */ - protected $duplicateCheckFields; - - /** - * @var array - */ - protected $dataTransformers; - - /** - * @var bool - */ - protected $enableDuplicatesIndex; - /** * @var bool */ protected $analyzeFalsePositives = false; - /** - * @var PaginatorInterface - */ - protected $paginator; - - /** - * DefaultMariaDbDuplicatesIndex constructor. - * - * @param PaginatorInterface $paginator - * @param bool $enableDuplicatesIndex - * @param array $duplicateCheckFields - * @param array $dataTransformers - */ public function __construct( - PaginatorInterface $paginator, - $enableDuplicatesIndex = false, - array $duplicateCheckFields = [], - array $dataTransformers = [] + protected PaginatorInterface $paginator, + protected PotentialDuplicateItemInterface $potentialDuplicateItem, + protected bool $enableDuplicatesIndex = false, + protected array $duplicateCheckFields = [], + protected array $dataTransformers = [] ) { - $this->paginator = $paginator; - $this->enableDuplicatesIndex = $enableDuplicatesIndex; - $this->duplicateCheckFields = $duplicateCheckFields; - $this->dataTransformers = $dataTransformers; } public function recreateIndex() @@ -271,10 +241,7 @@ public function getPotentialDuplicates($page, $pageSize = 100, $declined = false $items = $paginator->getItems(); foreach ($items as &$row) { - /** - * @var \CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItemInterface $item - */ - $item = \Pimcore::getContainer()->get('cmf.potential_duplicate_item'); + $item = $this->potentialDuplicateItem; $customers = []; foreach (explode(',', $row['duplicateCustomerIds']) as $customerId) { diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index a29ba309..5fe30251 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -173,12 +173,13 @@ services: package: pimcore/customer-management-framework-bundle version: 3.0 - cmf.segment_manager.segment_merger: + CustomerManagementFrameworkBundle\SegmentManager\SegmentMerger\SegmentMergerInterface: class: CustomerManagementFrameworkBundle\SegmentManager\SegmentMerger\DefaultSegmentMerger + lazy: true CustomerManagementFrameworkBundle\SegmentManager\SegmentMerger\DefaultSegmentMerger\MetadataFiller: ~ - cmf.customer_duplicates_view: + CustomerManagementFrameworkBundle\CustomerDuplicatesView\CustomerDuplicatesViewInterface: class: CustomerManagementFrameworkBundle\CustomerDuplicatesView\DefaultCustomerDuplicatesView arguments: - '%pimcore_customer_management_framework.customer_duplicates_services.duplicates_view.listFields%' @@ -191,6 +192,7 @@ services: cmf.customer_view: alias: 'CustomerManagementFrameworkBundle\CustomerView\CustomerViewInterface' + public: true CustomerManagementFrameworkBundle\CustomerView\FilterDefinition\Maintenance: ~ @@ -198,7 +200,7 @@ services: CustomerManagementFrameworkBundle\CustomerList\SearchHelper: ~ - cmf.potential_duplicate_item: + CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItemInterface: class: CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItem shared: false @@ -208,14 +210,6 @@ services: arguments: - '%pimcore_customer_management_framework.customer_list.exporters%' - cmf.customer_exporter_manager: - alias: CustomerManagementFrameworkBundle\CustomerList\ExporterManagerInterface - deprecated: - message: 'The "%alias_id%" alias is deprecated. Use "CustomerManagementFrameworkBundle\CustomerList\ExporterManagerInterface" instead' - package: pimcore/customer-management-framework-bundle - version: 3.0 - - CustomerManagementFrameworkBundle\ActionTrigger\Queue\QueueInterface: class: CustomerManagementFrameworkBundle\ActionTrigger\Queue\DefaultQueue @@ -230,11 +224,9 @@ services: calls: - [setApiResourceRoute, ['customermanagementframework_rest_api_customers_readrecord']] - cmf.rest.segments_of_customer_handler: - class: CustomerManagementFrameworkBundle\RESTApi\SegmentsOfCustomerHandler + CustomerManagementFrameworkBundle\RESTApi\SegmentsOfCustomerHandler: ~ - cmf.rest.segments_handler: - class: CustomerManagementFrameworkBundle\RESTApi\SegmentsHandler + CustomerManagementFrameworkBundle\RESTApi\SegmentsHandler: calls: - [setApiResourceRoute, ['customermanagementframework_rest_api_segments_readrecord']] @@ -242,8 +234,7 @@ services: calls: - [setApiResourceRoute, ['customermanagementframework_rest_api_segmentgroups_readrecord']] - cmf.rest.deletions_handler: - class: CustomerManagementFrameworkBundle\RESTApi\DeletionsHandler + CustomerManagementFrameworkBundle\RESTApi\DeletionsHandler: ~ cmf.encryption_service: '@CustomerManagementFrameworkBundle\Encryption\EncryptionServiceInterface' CustomerManagementFrameworkBundle\Encryption\EncryptionServiceInterface: '@CustomerManagementFrameworkBundle\Encryption\DefaultEncryptionService' @@ -275,6 +266,6 @@ services: class: Pimcore\Document\Newsletter\DefaultAddressSourceAdapterFactory arguments: - 'CustomerManagementFrameworkBundle\Newsletter\AddressSource\SegmentAddressSource' - + public: true CustomerManagementFrameworkBundle\Helper\JsConfigService: ~ diff --git a/src/SegmentManager/DefaultSegmentManager.php b/src/SegmentManager/DefaultSegmentManager.php index 72840bd3..529f6172 100644 --- a/src/SegmentManager/DefaultSegmentManager.php +++ b/src/SegmentManager/DefaultSegmentManager.php @@ -24,6 +24,7 @@ use CustomerManagementFrameworkBundle\SegmentAssignment\TypeMapper\TypeMapperInterface; use CustomerManagementFrameworkBundle\SegmentBuilder\SegmentBuilderInterface; use CustomerManagementFrameworkBundle\SegmentManager\SegmentExtractor\SegmentExtractorInterface; +use CustomerManagementFrameworkBundle\SegmentManager\SegmentMerger\SegmentMergerInterface; use CustomerManagementFrameworkBundle\Traits\LoggerAware; use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\DataObject\CustomerSegment; @@ -37,59 +38,25 @@ class DefaultSegmentManager implements SegmentManagerInterface { use LoggerAware; - /** - * @var string|\Pimcore\Model\DataObject\Folder - */ - protected $segmentFolderCalculated; - - /** - * @var string|\Pimcore\Model\DataObject\Folder - */ - protected $segmentFolderManual; - - /** - * @var CustomerSaveManagerInterface - */ - protected $customerSaveManager; - /** * @var SegmentBuilderInterface[] */ protected $segmentBuilders = []; /** - * @var CustomerProviderInterface + * @param TypeMapperInterface $typeMapper maps actual types of elements implementing ElementInterface + * to type strings used with db tables */ - protected $customerProvider; - - /** - * maps actual types of elements implementing ElementInterface to type strings used with db tables - * - * @var TypeMapperInterface - */ - protected $typeMapper = null; - - /** - * @var StoredFunctionsInterface - */ - protected $storedFunctions = null; - - /** - * @param string|\Pimcore\Model\DataObject\Folder $segmentFolderCalculated - * @param string|\Pimcore\Model\DataObject\Folder $segmentFolderManual - * @param CustomerSaveManagerInterface $customerSaveManager - * @param CustomerProviderInterface $customerProvider - * @param TypeMapperInterface $typeMapper - * @param StoredFunctionsInterface $storedFunctions - */ - public function __construct($segmentFolderCalculated, $segmentFolderManual, CustomerSaveManagerInterface $customerSaveManager, CustomerProviderInterface $customerProvider, TypeMapperInterface $typeMapper, StoredFunctionsInterface $storedFunctions) + public function __construct( + protected string|Folder $segmentFolderCalculated, + protected string|Folder $segmentFolderManual, + protected CustomerSaveManagerInterface $customerSaveManager, + protected CustomerProviderInterface $customerProvider, + protected TypeMapperInterface $typeMapper, + protected StoredFunctionsInterface $storedFunctions, + protected SegmentMergerInterface $segmentMerger + ) { - $this->segmentFolderCalculated = $segmentFolderCalculated; - $this->segmentFolderManual = $segmentFolderManual; - - $this->customerSaveManager = $customerSaveManager; - $this->customerProvider = $customerProvider; - $this->setTypeMapper($typeMapper); $this->setStoredFunctions($storedFunctions); } @@ -619,7 +586,7 @@ public function mergeSegments( $segmentCreatedTimestamp = null, $segmentApplicationCounter = null ) { - \Pimcore::getContainer()->get('cmf.segment_manager.segment_merger')->mergeSegments( + $this->segmentMerger->mergeSegments( $customer, $addSegments, $deleteSegments, @@ -634,7 +601,7 @@ public function mergeSegments( */ public function saveMergedSegments(CustomerInterface $customer) { - \Pimcore::getContainer()->get('cmf.segment_manager.segment_merger')->saveMergedSegments($customer); + $this->segmentMerger->saveMergedSegments($customer); } public function addSegmentBuilder(SegmentBuilderInterface $segmentBuilder)