Skip to content

Commit c44c198

Browse files
MM-29: Add functionality to map relation between website and sales_channel on the integration form (#54)
1 parent fe27b16 commit c44c198

File tree

42 files changed

+1938
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1938
-183
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Marello\Bundle\Magento2Bundle\Autocomplete;
4+
5+
use Marello\Bundle\SalesBundle\Entity\Repository\SalesChannelRepository;
6+
use Oro\Bundle\FormBundle\Autocomplete\SearchHandler;
7+
8+
/**
9+
* @todo Cover with functional test
10+
*/
11+
class SalesChannelInGroupHandler extends SearchHandler
12+
{
13+
private const DELIMITER = ';';
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function checkAllDependenciesInjected()
19+
{
20+
if (!$this->entityRepository || !$this->idFieldName) {
21+
throw new \RuntimeException('Search handler is not fully configured');
22+
}
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function searchEntities($search, $firstResult, $maxResults)
29+
{
30+
$parts = explode(self::DELIMITER, $search);
31+
if (3 !== count($parts)) {
32+
return [];
33+
}
34+
35+
$searchTerm = $parts[0];
36+
$salesChannelGroupId = (int) $parts[1];
37+
$skippedSalesChannelIds = '' !== $parts[2] ? explode(',', $parts[2]) : [];
38+
39+
$resultEntities = [];
40+
if (0 !== $salesChannelGroupId) {
41+
/** @var SalesChannelRepository $repository */
42+
$repository = $this->entityRepository;
43+
$queryBuilder = $repository->getActiveSalesChannelBySearchTermLimitedWithGroupIdQB(
44+
$searchTerm,
45+
$salesChannelGroupId,
46+
$skippedSalesChannelIds
47+
);
48+
49+
$queryBuilder
50+
->setFirstResult($firstResult)
51+
->setMaxResults($maxResults);
52+
53+
$resultEntities = $this->aclHelper->apply($queryBuilder->getQuery())->getResult();
54+
}
55+
56+
return $resultEntities;
57+
}
58+
}

src/Marello/Bundle/Magento2Bundle/Controller/IntegrationConfigController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function checkAction(
4343
$transportEntity ?? new Magento2Transport()
4444
);
4545

46-
$response = $this->getUpdateSuccessResponse($response);
46+
if ($response['success']) {
47+
$response = $this->getUpdateSuccessResponse($response);
48+
}
4749
} catch (\Exception $e) {
4850
$response = $this->logErrorAndGetResponse($e);
4951
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Marello\Bundle\Magento2Bundle\DTO;
4+
5+
class WebsiteToSalesChannelMappingItemDTO implements \JsonSerializable
6+
{
7+
/** @var string[] */
8+
public const REQUIRED_KEYS = [
9+
'originWebsiteId',
10+
'websiteName',
11+
'salesChannelId',
12+
'salesChannelName'
13+
];
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $originData = [];
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $data = [];
24+
25+
/**
26+
* @param array $data
27+
*/
28+
public function __construct(array $data)
29+
{
30+
$notExistedKeys = \array_diff_key(\array_flip(self::REQUIRED_KEYS), $data);
31+
if (!empty($notExistedKeys)) {
32+
throw new \LogicException(
33+
'The website to sales channel mapping item must contains all required keys.',
34+
[
35+
'missedKeys' => $notExistedKeys,
36+
'originalData' => $data
37+
]
38+
);
39+
}
40+
41+
$this->originData = $data;
42+
$this->data = $data;
43+
}
44+
45+
/**
46+
* @return int
47+
*/
48+
public function getOriginWebsiteId(): int
49+
{
50+
return $this->data['originWebsiteId'];
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getWebsiteName(): string
57+
{
58+
return $this->data['websiteName'];
59+
}
60+
61+
/**
62+
* @param string $websiteName
63+
*/
64+
public function setWebsiteName(string $websiteName): void
65+
{
66+
$this->data['websiteName'] = $websiteName;
67+
}
68+
69+
/**
70+
* @return int
71+
*/
72+
public function getSalesChannelId(): int
73+
{
74+
return $this->data['salesChannelId'];
75+
}
76+
77+
/**
78+
* @return string
79+
*/
80+
public function getSalesChannelName(): string
81+
{
82+
return $this->data['salesChannelName'];
83+
}
84+
85+
/**
86+
* @param string $salesChannelName
87+
*/
88+
public function setSalesChannelName(string $salesChannelName): void
89+
{
90+
$this->data['salesChannelName'] = $salesChannelName;
91+
}
92+
93+
/**
94+
* @return array
95+
*/
96+
public function getOriginData(): array
97+
{
98+
return $this->originData;
99+
}
100+
101+
/**
102+
* @return array
103+
*/
104+
public function getData(): array
105+
{
106+
return $this->data;
107+
}
108+
109+
/**
110+
* @inheritDoc
111+
*/
112+
public function jsonSerialize()
113+
{
114+
return $this->data;
115+
}
116+
}

src/Marello/Bundle/Magento2Bundle/Entity/Magento2Transport.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ class Magento2Transport extends Transport
4242

4343
/**
4444
* @var array
45-
* [
46-
* [
47-
* 'website_code' => string <website_code>,
48-
* 'sales_chanel_code' => string <sales_channel_code>
49-
* ],
50-
* ...
51-
* ]
45+
*
46+
* The structure described in constant REQUIRED_KEYS of @see WebsiteToSalesChannelMappingItemDTO
5247
*
5348
* @ORM\Column(name="m2_websites_sales_channel_map", type="json", nullable=true)
5449
*/

src/Marello/Bundle/Magento2Bundle/EventListener/Doctrine/SalesChannelReverseSyncListener.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use Oro\Component\MessageQueue\Client\MessageProducerInterface;
1717
use Oro\Component\MessageQueue\Transport\Exception\Exception;
1818

19-
/**
20-
* @todo Block changes currency and block changes in sales channel code
21-
*/
2219
class SalesChannelReverseSyncListener
2320
{
2421
private const ACTIVE_PROPERTY_NAME = 'active';

src/Marello/Bundle/Magento2Bundle/EventListener/Doctrine/WebsiteSalesChannelReverseSyncListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ protected function loadCreatedWebsites(UnitOfWork $unitOfWork)
341341
protected function loadUpdatedWebsites(UnitOfWork $unitOfWork)
342342
{
343343
foreach ($unitOfWork->getScheduledEntityUpdates() as $entityUpdate) {
344-
if ($entityUpdate instanceof Website && $this->isApplicableWebsite($entityUpdate)) {
344+
if ($entityUpdate instanceof Website) {
345345
$changeSet = $unitOfWork->getEntityChangeSet($entityUpdate);
346346

347347
if (!\array_key_exists(self::SALES_CHANNEL_FIELD_NAME, $changeSet)) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Marello\Bundle\Magento2Bundle\Form\Type;
4+
5+
use Oro\Bundle\FormBundle\Form\Type\OroJquerySelect2HiddenType;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Component\Form\FormView;
9+
use Symfony\Component\OptionsResolver\OptionsResolver;
10+
11+
class SalesChannelInGroupSelectType extends AbstractType
12+
{
13+
private const BLOCK_PREFIX = 'marello_magento2_sales_channel_in_group_select';
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function configureOptions(OptionsResolver $resolver)
19+
{
20+
$resolver->setDefaults(
21+
[
22+
'componentName' => 'salesChannelInGroupSelectComponent',
23+
'autocomplete_alias' => 'magento2_saleschannels_in_group',
24+
'configs' => [
25+
'allowClear' => false,
26+
'component' => 'autocomplete-magento2-sales-channel-in-group',
27+
'placeholder' => 'marello.sales.saleschannel.form.select_saleschannel',
28+
'result_template_twig' => 'MarelloMagento2Bundle:SalesChannel:Autocomplete/result.html.twig',
29+
'selection_template_twig' => 'MarelloMagento2Bundle:SalesChannel:Autocomplete/selection.html.twig'
30+
],
31+
'attr' => [
32+
'data-role' => 'sales-channel-in-group-select'
33+
]
34+
]
35+
);
36+
}
37+
38+
public function buildView(FormView $view, FormInterface $form, array $options)
39+
{
40+
parent::buildView($view, $form, $options);
41+
42+
$vars = [
43+
'attr' => [
44+
'data-page-component-name' => $options['componentName']
45+
]
46+
];
47+
48+
$view->vars = array_replace_recursive($view->vars, $vars);
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getParent()
55+
{
56+
return OroJquerySelect2HiddenType::class;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function getBlockPrefix()
63+
{
64+
return self::BLOCK_PREFIX;
65+
}
66+
}

src/Marello/Bundle/Magento2Bundle/Form/Type/TransportCheckButtonType.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,44 @@
33
namespace Marello\Bundle\Magento2Bundle\Form\Type;
44

55
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
6+
use Symfony\Component\Form\FormInterface;
7+
use Symfony\Component\Form\FormView;
68
use Symfony\Component\OptionsResolver\OptionsResolver;
79

810
class TransportCheckButtonType extends ButtonType
911
{
10-
public const NAME = 'marello_magento2_transport_check_button';
12+
private const BLOCK_PREFIX = 'marello_magento2_transport_check_button';
1113

1214
/**
13-
* {@inheritdoc}
15+
* {@inheritDoc}
1416
*/
15-
public function getName()
17+
public function getBlockPrefix()
1618
{
17-
return $this->getBlockPrefix();
19+
return self::BLOCK_PREFIX;
1820
}
1921

2022
/**
21-
* {@inheritdoc}
23+
* {@inheritDoc}
2224
*/
23-
public function getBlockPrefix()
25+
public function configureOptions(OptionsResolver $resolver)
2426
{
25-
return self::NAME;
27+
parent::configureOptions($resolver);
28+
29+
$resolver->setRequired(['selectorForFieldsRequiredReCheckConnection']);
30+
$resolver->setDefaults(['attr' => ['class' => 'btn btn-primary']]);
2631
}
2732

2833
/**
2934
* {@inheritDoc}
3035
*/
31-
public function configureOptions(OptionsResolver $resolver)
36+
public function buildView(FormView $view, FormInterface $form, array $options)
3237
{
33-
parent::configureOptions($resolver);
38+
parent::buildView($view, $form, $options);
3439

35-
$resolver->setRequired(['websiteToSalesChannelMappingSelector']);
36-
$resolver->setRequired(['salesGroupSelector']);
37-
$resolver->setDefaults(['attr' => ['class' => 'btn btn-primary']]);
40+
$view->vars = \array_replace_recursive($view->vars, [
41+
'component_options' => [
42+
'selectorForFieldsRequiredReCheckConnection' => $options['selectorForFieldsRequiredReCheckConnection']
43+
]
44+
]);
3845
}
3946
}

0 commit comments

Comments
 (0)