Skip to content

Commit

Permalink
Use DI for services & remove aliases - resolves #205
Browse files Browse the repository at this point in the history
  • Loading branch information
dvesh3 committed Dec 28, 2022
1 parent dcd0fbe commit b660b20
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 109 deletions.
2 changes: 2 additions & 0 deletions src/Controller/Admin/ActivitiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions src/Controller/Admin/DuplicatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 11 additions & 2 deletions src/Controller/Rest/Api/SegmentsOfCustomersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand All @@ -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;
}
}
6 changes: 4 additions & 2 deletions src/CustomerSaveValidator/DefaultCustomerSaveValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
);
Expand Down
47 changes: 7 additions & 40 deletions src/DuplicatesIndex/DefaultMariaDbDuplicatesIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 9 additions & 18 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand All @@ -191,14 +192,15 @@ services:

cmf.customer_view:
alias: 'CustomerManagementFrameworkBundle\CustomerView\CustomerViewInterface'
public: true

CustomerManagementFrameworkBundle\CustomerView\FilterDefinition\Maintenance: ~

CustomerManagementFrameworkBundle\CustomerView\Customer\Maintenance: ~

CustomerManagementFrameworkBundle\CustomerList\SearchHelper: ~

cmf.potential_duplicate_item:
CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItemInterface:
class: CustomerManagementFrameworkBundle\Model\CustomerDuplicates\PotentialDuplicateItem
shared: false

Expand All @@ -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

Expand All @@ -230,20 +224,17 @@ 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']]

CustomerManagementFrameworkBundle\RESTApi\SegmentGroupsHandler:
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'
Expand Down Expand Up @@ -275,6 +266,6 @@ services:
class: Pimcore\Document\Newsletter\DefaultAddressSourceAdapterFactory
arguments:
- 'CustomerManagementFrameworkBundle\Newsletter\AddressSource\SegmentAddressSource'

public: true

CustomerManagementFrameworkBundle\Helper\JsConfigService: ~
61 changes: 14 additions & 47 deletions src/SegmentManager/DefaultSegmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit b660b20

Please sign in to comment.