Skip to content

Commit

Permalink
MM-42: Test and fix functionality (#66)
Browse files Browse the repository at this point in the history
- Added functionality that catches shifting of items between pages
- Moved styles to SCSS file
- Removed unneeded todos
- Implemented functionality to schedule sync on changing integration
- Implemented functionality to remove non-existed records
- Removed unneeded logic
  • Loading branch information
alexandr-parkhomenko authored and 24198 committed Aug 12, 2020
1 parent c9525af commit 0530503
Show file tree
Hide file tree
Showing 36 changed files with 963 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use Marello\Bundle\SalesBundle\Entity\Repository\SalesChannelRepository;
use Oro\Bundle\FormBundle\Autocomplete\SearchHandler;

/**
* @todo Cover with functional test
*/
class SalesChannelInGroupHandler extends SearchHandler
{
private const DELIMITER = ';';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(array $data)
/**
* @return int
*/
public function getOriginWebsiteId(): int
public function getWebsiteOriginId(): int
{
return $this->data['originWebsiteId'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;

/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="Marello\Bundle\Magento2Bundle\Entity\Repository\ProductTaxClassRepository")
* @ORM\Table(name="marello_m2_product_tax_class")
* @Config()
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Marello\Bundle\Magento2Bundle\Entity\Repository;

use Doctrine\Persistence\ObjectRepository;

interface NotInOriginIdsInterface extends ObjectRepository
{
/**
* @param array $existedRecordsOriginIds
* @param int $integrationId
* @return object[]
*/
public function getEntitiesNotInOriginIdsInGivenIntegration(
array $existedRecordsOriginIds,
int $integrationId
): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Marello\Bundle\Magento2Bundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;

class ProductTaxClassRepository extends EntityRepository implements NotInOriginIdsInterface
{
/**
* {@inheritDoc}
*/
public function getEntitiesNotInOriginIdsInGivenIntegration(
array $existedRecordsOriginIds,
int $integrationId
): array {
$qb = $this->createQueryBuilder('m2pt');
$qb
->select('m2pt')
->where($qb->expr()->notIn('m2pt.originId', ':existedRecordsOriginIds'))
->andWhere($qb->expr()->eq('m2pt.channel', ':integrationId'))
->setParameter('existedRecordsOriginIds', $existedRecordsOriginIds)
->setParameter('integrationId', $integrationId);

return $qb->getQuery()->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Doctrine\ORM\EntityRepository;

class StoreRepository extends EntityRepository
class StoreRepository extends EntityRepository implements NotInOriginIdsInterface
{
/**
* @param int $websiteId
Expand All @@ -22,4 +22,22 @@ public function getOriginStoreIdsByWebsiteId(int $websiteId): array

return \array_column($result, 'originId');
}

/**
* {@inheritDoc}
*/
public function getEntitiesNotInOriginIdsInGivenIntegration(
array $existedRecordsOriginIds,
int $integrationId
): array {
$qb = $this->createQueryBuilder('m2s');
$qb
->select('m2s')
->where($qb->expr()->notIn('m2s.originId', ':existedRecordsOriginIds'))
->andWhere($qb->expr()->eq('m2s.channel', ':integrationId'))
->setParameter('existedRecordsOriginIds', $existedRecordsOriginIds)
->setParameter('integrationId', $integrationId);

return $qb->getQuery()->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\EntityRepository;
use Marello\Bundle\Magento2Bundle\Model\SalesChannelInfo;

class WebsiteRepository extends EntityRepository
class WebsiteRepository extends EntityRepository implements NotInOriginIdsInterface
{
/**
* @return SalesChannelInfo[]
Expand Down Expand Up @@ -61,4 +61,40 @@ public function getWebsitesIdsByIntegrationId(int $integrationId): array

return \array_column($result, 'id');
}

/**
* {@inheritDoc}
*/
public function getEntitiesNotInOriginIdsInGivenIntegration(
array $existedRecordsOriginIds,
int $integrationId
): array {
$qb = $this->createQueryBuilder('m2w');
$qb
->select('m2w')
->where($qb->expr()->notIn('m2w.originId', ':existedRecordsOriginIds'))
->andWhere($qb->expr()->eq('m2w.channel', ':integrationId'))
->setParameter('existedRecordsOriginIds', $existedRecordsOriginIds)
->setParameter('integrationId', $integrationId)
;

return $qb->getQuery()->getResult();
}

/**
* @param int $integrationId
* @return array
*/
public function getOriginIdsByIntegrationId(int $integrationId): array
{
$qb = $this->createQueryBuilder('m2w');
$qb
->select('m2w.originId')
->where($qb->expr()->eq('m2w.channel', ':integrationId'))
->setParameter('integrationId', $integrationId);

$result = $qb->getQuery()->getArrayResult();

return \array_column($result, 'originId');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WebsiteIntegrationStatus

/**
* @todo Rename this to innerStatus
* @todo Update repository after that
*
* @var Status
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Marello\Bundle\Magento2Bundle\EventListener\Doctrine;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Marello\Bundle\Magento2Bundle\Entity\Magento2Transport;
use Oro\Bundle\IntegrationBundle\Manager\GenuineSyncScheduler;

class Magento2TransportListener
{
/** @var GenuineSyncScheduler */
protected $genuineSyncScheduler;

/** @var array */
protected $trackedTransportPropertyNames = [];

/** @var array */
protected $integrationIdsOnSync = [];

/**
* @param GenuineSyncScheduler $genuineSyncScheduler
* @param array $trackedTransportPropertyNames
*/
public function __construct(
GenuineSyncScheduler $genuineSyncScheduler,
array $trackedTransportPropertyNames
) {
$this->genuineSyncScheduler = $genuineSyncScheduler;
$this->trackedTransportPropertyNames = $trackedTransportPropertyNames;
}

/**
* @param Magento2Transport $transport
* @param LifecycleEventArgs $args
*/
public function postUpdate(Magento2Transport $transport, LifecycleEventArgs $args)
{
if (false === $transport->getChannel()->isEnabled()) {
return;
}

$changeSet = $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($transport);
$isTrackablePropertiesyChanged = [] !== \array_intersect(
$this->trackedTransportPropertyNames,
\array_keys($changeSet)
);

if (!$isTrackablePropertiesyChanged) {
return;
}

$this->integrationIdsOnSync[$transport->getChannel()->getId()] = $transport->getChannel()->getId();
}

public function postFlush()
{
foreach ($this->integrationIdsOnSync as $integrationId) {
$this->genuineSyncScheduler->schedule($integrationId);
}

$this->integrationIdsOnSync = [];
}

public function onClear()
{
$this->integrationIdsOnSync = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Marello\Bundle\Magento2Bundle\Stack\ProductChangesByChannelStack;
use Marello\Bundle\ProductBundle\Entity\Repository\ProductRepository;
use Marello\Bundle\SalesBundle\Entity\SalesChannel;
use Oro\Bundle\IntegrationBundle\Manager\GenuineSyncScheduler;
use Oro\Component\MessageQueue\Client\MessageProducerInterface;
use Oro\Component\MessageQueue\Transport\Exception\Exception;

Expand All @@ -32,6 +33,9 @@ class SalesChannelReverseSyncListener
/** @var MessageProducerInterface */
protected $producer;

/** @var GenuineSyncScheduler */
protected $genuineSyncScheduler;

/** @var array */
protected $integrationChannelIdsWithProductIds = [];

Expand All @@ -43,17 +47,20 @@ class SalesChannelReverseSyncListener
* @param ProductChangesByChannelStack $changesByChannelStack
* @param ProductRepository $productRepository
* @param MessageProducerInterface $producer
* @param GenuineSyncScheduler $genuineSyncScheduler
*/
public function __construct(
TrackedSalesChannelProvider $salesChannelInfosProvider,
ProductChangesByChannelStack $changesByChannelStack,
ProductRepository $productRepository,
MessageProducerInterface $producer
MessageProducerInterface $producer,
GenuineSyncScheduler $genuineSyncScheduler
) {
$this->salesChannelProvider = $salesChannelInfosProvider;
$this->changesByChannelStack = $changesByChannelStack;
$this->productRepository = $productRepository;
$this->producer = $producer;
$this->genuineSyncScheduler = $genuineSyncScheduler;
}

/**
Expand Down Expand Up @@ -112,6 +119,23 @@ protected function loadProductIdsOfRemovedSalesChannels(UnitOfWork $unitOfWork)
public function postFlush(PostFlushEventArgs $args)
{
$this->salesChannelProvider->clearCache();
$this->processIntegrationChannelIdsWithProductIds();
$this->processSalesChannelWithUpdatedActiveField();
}

/**
* Clear object storage when error was occurred during UOW#Commit
*
* @param OnClearEventArgs $args
*/
public function onClear(OnClearEventArgs $args)
{
$this->salesChannelsWithUpdatedActiveField = [];
$this->integrationChannelIdsWithProductIds = [];
}

protected function processIntegrationChannelIdsWithProductIds(): void
{
foreach ($this->integrationChannelIdsWithProductIds as $integrationId => $modifiedProductIds) {
if (empty($modifiedProductIds)) {
continue;
Expand Down Expand Up @@ -142,11 +166,11 @@ public function postFlush(PostFlushEventArgs $args)
}

$this->integrationChannelIdsWithProductIds = [];
}

/**
* @todo Extend this logic to call initial sync on integration
* that has enabled sales channels
*/
protected function processSalesChannelWithUpdatedActiveField(): void
{
$integrationIdsOnSync = [];
foreach ($this->salesChannelsWithUpdatedActiveField as $salesChannel) {
$integrationId = $this->salesChannelProvider->getIntegrationIdBySalesChannelId(
$salesChannel->getId(),
Expand All @@ -160,6 +184,13 @@ public function postFlush(PostFlushEventArgs $args)
continue;
}

/**
* In case when new sales channel enabled, we should send message on sync
*/
if ($salesChannel->getActive()) {
$integrationIdsOnSync[] = $integrationId;
}

$modifiedProductIds = $this->productRepository->getProductIdsBySalesChannelIds([$salesChannel->getId()]);
if (empty($modifiedProductIds)) {
continue;
Expand Down Expand Up @@ -198,18 +229,11 @@ public function postFlush(PostFlushEventArgs $args)
);
}

$this->salesChannelsWithUpdatedActiveField = [];
}
foreach ($integrationIdsOnSync as $integrationId) {
$this->genuineSyncScheduler->schedule($integrationId);
}

/**
* Clear object storage when error was occurred during UOW#Commit
*
* @param OnClearEventArgs $args
*/
public function onClear(OnClearEventArgs $args)
{
$this->salesChannelsWithUpdatedActiveField = [];
$this->integrationChannelIdsWithProductIds = [];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
]
);

/**
* @todo Schedule initial sync on changing this value
*/
$builder->add(
'initialSyncStartDate',
OroDateType::class,
Expand Down Expand Up @@ -128,9 +125,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
]
);

/**
* @todo Schedule initial sync on changing this value
*/
$builder->add(
'websiteToSalesChannelMapping',
WebsiteToSalesChannelMappingType::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public function getBlockPrefix()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
/**
* @todo Implement transforming data to DTO for intermidiate validation
*/
// $builder->addModelTransformer(new ArrayToJsonTransformer());
$builder->addViewTransformer(new ArrayToJsonTransformer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public function getCheckResponse(
}

/**
* @todo Replace with validator
*
* @param Magento2Transport $transportEntity
* @return bool
*/
Expand Down
Loading

0 comments on commit 0530503

Please sign in to comment.