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 Jan 2, 2023
1 parent d84f9d2 commit a7c5e59
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
26 changes: 19 additions & 7 deletions src/RESTApi/ActivitiesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace CustomerManagementFrameworkBundle\RESTApi;

use CustomerManagementFrameworkBundle\ActivityStore\ActivityStoreInterface;
use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
use CustomerManagementFrameworkBundle\Filter\ExportActivitiesFilterParams;
use CustomerManagementFrameworkBundle\Model\Activity\GenericActivity;
use CustomerManagementFrameworkBundle\Model\ActivityInterface;
Expand All @@ -24,6 +26,7 @@
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResourceUrlGenerator;
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResponseGenerator;
use CustomerManagementFrameworkBundle\Traits\LoggerAware;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;

class ActivitiesHandler extends AbstractHandler implements CrudHandlerInterface
Expand All @@ -32,6 +35,15 @@ class ActivitiesHandler extends AbstractHandler implements CrudHandlerInterface
use ResponseGenerator;
use ResourceUrlGenerator;

public function __construct(
PaginatorInterface $paginator,
protected ActivityStoreInterface $activityStore,
protected CustomerProviderInterface $customerProvider
)
{
parent::__construct($paginator);
}

/**
* GET /activities
*
Expand All @@ -48,7 +60,7 @@ public function listRecords(Request $request)
$pageSize = intval($request->get('pageSize', 100));
$page = intval($request->get('page', 1));

$paginator = \Pimcore::getContainer()->get('cmf.activity_store')->getActivitiesDataForWebservice(
$paginator = $this->activityStore->getActivitiesDataForWebservice(
$pageSize,
$page,
$param
Expand Down Expand Up @@ -121,7 +133,7 @@ public function createRecord(Request $request)

if ($activity && $activity->cmfWebserviceUpdateAllowed()) {
if (!$activity->getCustomer()) {
if (!$customer = \Pimcore::getContainer()->get('cmf.customer_provider')->getById(
if (!$customer = $this->customerProvider->getById(
$data['customerId']
)
) {
Expand All @@ -135,8 +147,8 @@ public function createRecord(Request $request)
$activity->save();
}

$entry = \Pimcore::getContainer()->get('cmf.activity_store')->insertActivityIntoStore($activity);
$entry = \Pimcore::getContainer()->get('cmf.activity_store')->getEntryById($entry->getId());
$entry = $this->activityStore->insertActivityIntoStore($activity);
$entry = $this->activityStore->getEntryById($entry->getId());
} else {
return $this->createErrorResponse(
sprintf(
Expand Down Expand Up @@ -182,7 +194,7 @@ public function updateRecord(Request $request)
$activity->save();
}

\Pimcore::getContainer()->get('cmf.activity_store')->updateActivityInStore($activity, $entry);
$this->activityStore->updateActivityInStore($activity, $entry);
$entry = $this->loadActivityStoreEntry($request->get('id'));
} else {
return $this->createErrorResponse(
Expand Down Expand Up @@ -218,7 +230,7 @@ public function deleteRecord(Request $request)
$activity->delete();
}

\Pimcore::getContainer()->get('cmf.activity_store')->deleteEntry($entry);
$this->activityStore->deleteEntry($entry);
} else {
return $this->createErrorResponse(
sprintf(
Expand Down Expand Up @@ -256,7 +268,7 @@ protected function loadActivityStoreEntry($id)
$id = (int)$id;
}

$entry = \Pimcore::getContainer()->get('cmf.activity_store')->getEntryById($id);
$entry = $this->activityStore->getEntryById($id);
if (!$entry) {
throw new ResourceNotFoundException(sprintf('Activity with ID %d was not found', $id));
}
Expand Down
21 changes: 11 additions & 10 deletions src/RESTApi/CustomersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

namespace CustomerManagementFrameworkBundle\RESTApi;

use CustomerManagementFrameworkBundle\ActivityStore\ActivityStoreInterface;
use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
use CustomerManagementFrameworkBundle\Filter\ExportCustomersFilterParams;
use CustomerManagementFrameworkBundle\Model\CustomerInterface;
use CustomerManagementFrameworkBundle\RESTApi\Exception\ResourceNotFoundException;
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResourceUrlGenerator;
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResponseGenerator;
use CustomerManagementFrameworkBundle\SegmentManager\SegmentManagerInterface;
use CustomerManagementFrameworkBundle\Traits\LoggerAware;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\Customer;
Expand All @@ -33,18 +35,17 @@ class CustomersHandler extends AbstractHandler implements CrudHandlerInterface
use ResponseGenerator;
use ResourceUrlGenerator;

/**
* @var CustomerProviderInterface
*/
protected $customerProvider;

/**
* @param CustomerProviderInterface $customerProvider
*/
public function __construct(PaginatorInterface $paginator, CustomerProviderInterface $customerProvider)
public function __construct(
PaginatorInterface $paginator,
protected CustomerProviderInterface $customerProvider,
protected SegmentManagerInterface $segmentManager,
protected ActivityStoreInterface $activityStore
)
{
parent::__construct($paginator);
$this->customerProvider = $customerProvider;
}

/**
Expand All @@ -60,12 +61,12 @@ public function listRecords(Request $request)

if ($params->getSegments()) {
/** @var Customer\Listing $customers */
$customers = \Pimcore::getContainer()->get('cmf.segment_manager')->getCustomersBySegmentIds(
$customers = $this->segmentManager->getCustomersBySegmentIds(
$params->getSegments()
);
} else {
/** @var Customer\Listing $customers */
$customers = \Pimcore::getContainer()->get('cmf.customer_provider')->getList();
$customers = $this->customerProvider->getList();
}
$idField = Service::getVersionDependentDatabaseColumnName('id');
$modificationDateField = Service::getVersionDependentDatabaseColumnName('modificationDate');
Expand Down Expand Up @@ -244,7 +245,7 @@ protected function hydrateCustomer(CustomerInterface $customer, ExportCustomersF
$data = $customer->cmfToArray();

if ($params->getIncludeActivities()) {
$data['activities'] = \Pimcore::getContainer()->get('cmf.activity_store')->getActivityDataForCustomer(
$data['activities'] = $this->activityStore->getActivityDataForCustomer(
$customer
);
}
Expand Down
18 changes: 14 additions & 4 deletions src/RESTApi/SegmentGroupsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use CustomerManagementFrameworkBundle\RESTApi\Exception\ResourceNotFoundException;
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResourceUrlGenerator;
use CustomerManagementFrameworkBundle\RESTApi\Traits\ResponseGenerator;
use CustomerManagementFrameworkBundle\SegmentManager\SegmentManagerInterface;
use CustomerManagementFrameworkBundle\Service\ObjectToArray;
use CustomerManagementFrameworkBundle\Traits\LoggerAware;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\CustomerSegmentGroup;
use Pimcore\Model\DataObject\Service;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -30,6 +32,14 @@ class SegmentGroupsHandler extends AbstractHandler implements CrudHandlerInterfa
use ResponseGenerator;
use ResourceUrlGenerator;

public function __construct(
PaginatorInterface $paginator,
protected SegmentManagerInterface $segmentManager
)
{
parent::__construct($paginator);
}

/**
* GET /segment-groups
*
Expand Down Expand Up @@ -99,7 +109,7 @@ public function createRecord(Request $request)
);
}

if ($data['reference'] && \Pimcore::getContainer()->get('cmf.segment_manager')->getSegmentGroupByReference(
if ($data['reference'] && $this->segmentManager->getSegmentGroupByReference(
$data['reference'],
(bool)$data['calculated']
)
Expand All @@ -116,7 +126,7 @@ public function createRecord(Request $request)
);
}

$segmentGroup = \Pimcore::getContainer()->get('cmf.segment_manager')->createSegmentGroup(
$segmentGroup = $this->segmentManager->createSegmentGroup(
$data['name'],
$data['reference'],
isset($data['calculated']) ? (bool)$data['calculated'] : false,
Expand Down Expand Up @@ -152,7 +162,7 @@ public function updateRecord(Request $request)
);
}

if (!$segmentGroup = \Pimcore::getContainer()->get('cmf.segment_manager')->getSegmentGroupById(
if (!$segmentGroup = $this->segmentManager->getSegmentGroupById(
$request->get('id')
)
) {
Expand All @@ -165,7 +175,7 @@ public function updateRecord(Request $request)
);
}

\Pimcore::getContainer()->get('cmf.segment_manager')->updateSegmentGroup($segmentGroup, $data);
$this->segmentManager->updateSegmentGroup($segmentGroup, $data);

$result = $this->hydrateSegmentGroup($segmentGroup);
$result['success'] = true;
Expand Down
22 changes: 17 additions & 5 deletions src/RESTApi/SegmentsOfCustomerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@

namespace CustomerManagementFrameworkBundle\RESTApi;

use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
use CustomerManagementFrameworkBundle\SegmentManager\SegmentManagerInterface;
use CustomerManagementFrameworkBundle\Traits\LoggerAware;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouteCollection;

class SegmentsOfCustomerHandler extends AbstractHandler
{
use LoggerAware;

public function __construct(
PaginatorInterface $paginator,
protected CustomerProviderInterface $customerProvider,
protected SegmentManagerInterface $segmentManager
)
{
parent::__construct($paginator);
}

protected function getRoutes()
{
$routes = new RouteCollection();
Expand Down Expand Up @@ -54,7 +66,7 @@ public function updateRecords(Request $request)
);
}

if (!$customer = \Pimcore::getContainer()->get('cmf.customer_provider')->getById($data['customerId'])) {
if (!$customer = $this->customerProvider->getById($data['customerId'])) {
return new Response(
[
'success' => false,
Expand All @@ -67,7 +79,7 @@ public function updateRecords(Request $request)
$addSegments = [];
if (is_array($data['addSegments'])) {
foreach ($data['addSegments'] as $segmentId) {
if ($segment = \Pimcore::getContainer()->get('cmf.segment_manager')->getSegmentById($segmentId)) {
if ($segment = $this->segmentManager->getSegmentById($segmentId)) {
$addSegments[] = $segment;
}
}
Expand All @@ -76,19 +88,19 @@ public function updateRecords(Request $request)
$deleteSegments = [];
if (is_array($data['removeSegments'])) {
foreach ($data['removeSegments'] as $segmentId) {
if ($segment = \Pimcore::getContainer()->get('cmf.segment_manager')->getSegmentById($segmentId)) {
if ($segment = $this->segmentManager->getSegmentById($segmentId)) {
$deleteSegments[] = $segment;
}
}
}

\Pimcore::getContainer()->get('cmf.segment_manager')->mergeSegments(
$this->segmentManager->mergeSegments(
$customer,
$addSegments,
$deleteSegments,
'REST update API: segments-of-customer action'
);
\Pimcore::getContainer()->get('cmf.segment_manager')->saveMergedSegments($customer);
$this->segmentManager->saveMergedSegments($customer);

return new Response(['success' => true], Response::RESPONSE_CODE_OK);
}
Expand Down
16 changes: 7 additions & 9 deletions src/View/Formatter/DefaultViewFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@

use Carbon\Carbon;
use CustomerManagementFrameworkBundle\Model\CustomerSegmentInterface;
use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Symfony\Contracts\Translation\TranslatorInterface;

class DefaultViewFormatter implements ViewFormatterInterface
{
/**
* @var TranslatorInterface
*/
protected $translator;
protected $locale;

public function __construct(TranslatorInterface $translator)
public function __construct(
protected TranslatorInterface $translator,
protected LocaleServiceInterface $localeService
)
{
$this->translator = $translator;
}

protected $locale;

/**
* @param string $messageId
* @param mixed $parameters
Expand Down Expand Up @@ -189,7 +187,7 @@ protected function getLanguageFromLocale($locale)
*/
protected function applyLocale()
{
$locale = $this->getLocale() ?: \Pimcore::getContainer()->get('pimcore.locale')->getLocale();
$locale = $this->getLocale() ?: $this->localeService->getLocale();

$dateLocaleMap = [
'de' => 'de_AT',
Expand Down

0 comments on commit a7c5e59

Please sign in to comment.