Commit 7202b867 authored by Piotr Borek's avatar Piotr Borek Committed by Damian Kryger
Browse files

#805 Product changes in log

parent cbc60301
......@@ -35,6 +35,8 @@
<directory>./src/*/Constraint</directory>
<directory>./src/PcmtRulesBundle/Connector/Job/DefaultValueProvider</directory>
<directory>./src/PcmtRulesBundle/Connector/Job/ConstraintCollectionProvider</directory>
<directory>./src/PcmtRulesBundle/Event</directory>
<directory>./src/PcmtRulesBundle/Provider</directory>
<file>./src/PcmtDraftBundle/Controller/DraftController.php</file>
</exclude>
</whitelist>
......
......@@ -22,6 +22,7 @@ class PcmtRulesExtension extends Extension
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('controllers.yml');
$loader->load('datagrid_listeners.yml');
$loader->load('event_listeners.yml');
$loader->load('job_constraints.yml');
$loader->load('job_defaults.yml');
$loader->load('jobs.yml');
......
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Event;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ValueInterface;
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;
use Symfony\Component\EventDispatcher\Event;
class ProductChangedEvent extends Event
{
public const NAME = 'PRODUCT_VALUE_UPDATED';
/** @var ProductInterface */
private $product;
/** @var AttributeInterface */
private $attribute;
/** @var ?string */
private $localeCode;
/** @var ?string */
private $scopeCode;
/** @var ?ValueInterface */
private $previousValue;
/** @var ValueInterface */
private $newValue;
public function __construct(ProductInterface $product, AttributeInterface $attribute, ?string $localeCode, ?string $scopeCode, ?ValueInterface $previousValue, ValueInterface $newValue)
{
$this->product = $product;
$this->attribute = $attribute;
$this->localeCode = $localeCode;
$this->scopeCode = $scopeCode;
$this->previousValue = $previousValue;
$this->newValue = $newValue;
}
public function getProduct(): ProductInterface
{
return $this->product;
}
public function getAttribute(): AttributeInterface
{
return $this->attribute;
}
public function getLocaleCode(): ?string
{
return $this->localeCode;
}
public function getScopeCode(): ?string
{
return $this->scopeCode;
}
public function getPreviousValue(): ?ValueInterface
{
return $this->previousValue;
}
public function getNewValue(): ValueInterface
{
return $this->newValue;
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Event;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ValueInterface;
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;
use Symfony\Component\EventDispatcher\Event;
class ProductModelChangedEvent extends Event
{
public const NAME = 'PRODUCT_MODEL_VALUE_UPDATED';
/** @var ProductModelInterface */
private $productModel;
/** @var AttributeInterface */
private $attribute;
/** @var ?string */
private $localeCode;
/** @var ?string */
private $scopeCode;
/** @var ?ValueInterface */
private $previousValue;
/** @var ValueInterface */
private $newValue;
public function __construct(ProductModelInterface $productModel, AttributeInterface $attribute, ?string $localeCode, ?string $scopeCode, ?ValueInterface $previousValue, ValueInterface $newValue)
{
$this->productModel = $productModel;
$this->attribute = $attribute;
$this->localeCode = $localeCode;
$this->scopeCode = $scopeCode;
$this->previousValue = $previousValue;
$this->newValue = $newValue;
}
public function getProductModel(): ProductModelInterface
{
return $this->productModel;
}
public function getAttribute(): AttributeInterface
{
return $this->attribute;
}
public function getLocaleCode(): ?string
{
return $this->localeCode;
}
public function getScopeCode(): ?string
{
return $this->scopeCode;
}
public function getPreviousValue(): ?ValueInterface
{
return $this->previousValue;
}
public function getNewValue(): ValueInterface
{
return $this->newValue;
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Listener;
use PcmtRulesBundle\Event\ProductChangedEvent;
use PcmtRulesBundle\Event\ProductModelChangedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LoggerSubscriber implements EventSubscriberInterface
{
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
ProductChangedEvent::NAME => 'productValueUpdated',
ProductModelChangedEvent::NAME => 'productModelValueUpdated',
];
}
public function productValueUpdated(ProductChangedEvent $event): void
{
$this->logger->info(sprintf(
'Product: %s, attribute: %s, locale: %s, scope: %s, previousValue: %s, newValue: %s',
$event->getProduct()->getIdentifier(),
$event->getAttribute()->getCode(),
$event->getLocaleCode(),
$event->getScopeCode(),
$event->getPreviousValue(),
$event->getNewValue()
));
}
public function productModelValueUpdated(ProductModelChangedEvent $event): void
{
$this->logger->info(sprintf(
'Product model: %s, attribute: %s, locale: %s, scope: %s, previousValue: %s, newValue: %s',
$event->getProductModel()->getCode(),
$event->getAttribute()->getCode(),
$event->getLocaleCode(),
$event->getScopeCode(),
$event->getPreviousValue(),
$event->getNewValue()
));
}
}
services:
pcmt_rules.event_listener.logger_subscriber:
class: PcmtRulesBundle\Listener\LoggerSubscriber
arguments:
- '@logger'
tags:
- { name: kernel.event_subscriber }
- { name: monolog.logger, channel: batch }
......@@ -31,6 +31,7 @@ services:
- '@pim_enrich.filter.product_attribute_filter'
- '@pim_enrich.filter.product_model_attribute_filter'
- '@pim_internal_api_serializer'
- '@event_dispatcher'
pcmt_rules.datagrid.provider.job:
class: PcmtRulesBundle\Provider\PcmtJobDatagridProvider
......
......@@ -14,11 +14,15 @@ use Akeneo\Channel\Component\Repository\ChannelRepositoryInterface;
use Akeneo\Channel\Component\Repository\LocaleRepositoryInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface;
use Akeneo\Pim\Enrichment\Component\Product\ProductModel\Filter\ProductAttributeFilter;
use Akeneo\Pim\Enrichment\Component\Product\ProductModel\Filter\ProductModelAttributeFilter;
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;
use Akeneo\Tool\Component\StorageUtils\Updater\PropertyCopierInterface;
use PcmtRulesBundle\Event\ProductChangedEvent;
use PcmtRulesBundle\Event\ProductModelChangedEvent;
use PcmtRulesBundle\Value\AttributeMapping;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class RuleProcessorCopier
......@@ -41,13 +45,17 @@ class RuleProcessorCopier
/** @var NormalizerInterface */
private $normalizer;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
public function __construct(
PropertyCopierInterface $propertyCopier,
ChannelRepositoryInterface $channelRepository,
LocaleRepositoryInterface $localeRepository,
ProductAttributeFilter $productAttributeFilter,
ProductModelAttributeFilter $productModelAttributeFilter,
NormalizerInterface $normalizer
NormalizerInterface $normalizer,
EventDispatcherInterface $eventDispatcher
) {
$this->propertyCopier = $propertyCopier;
$this->channelRepository = $channelRepository;
......@@ -55,6 +63,7 @@ class RuleProcessorCopier
$this->productAttributeFilter = $productAttributeFilter;
$this->productModelAttributeFilter = $productModelAttributeFilter;
$this->normalizer = $normalizer;
$this->eventDispatcher = $eventDispatcher;
}
public function copy(
......@@ -110,13 +119,40 @@ class RuleProcessorCopier
'from_scope' => $scopeCode,
'to_scope' => $scopeCode,
];
$this->propertyCopier->copyData(
$sourceProduct,
$destinationProduct,
$sourceAttribute->getCode(),
$destinationAttribute->getCode(),
$options
);
$previousValue = $destinationProduct->getValue($destinationAttribute->getCode(), $localeCode, $scopeCode);
$newValue = $sourceProduct->getValue($sourceAttribute->getCode(), $localeCode, $scopeCode);
if ((!$previousValue && $newValue) || ($previousValue && !$previousValue->isEqual($newValue))) {
$this->propertyCopier->copyData(
$sourceProduct,
$destinationProduct,
$sourceAttribute->getCode(),
$destinationAttribute->getCode(),
$options
);
if ($destinationProduct instanceof ProductModelInterface) {
$event = new ProductModelChangedEvent(
$destinationProduct,
$destinationAttribute,
$localeCode,
$scopeCode,
$previousValue,
$newValue
);
$this->eventDispatcher->dispatch(ProductModelChangedEvent::NAME, $event);
} else {
$event = new ProductChangedEvent(
$destinationProduct,
$destinationAttribute,
$localeCode,
$scopeCode,
$previousValue,
$newValue
);
$this->eventDispatcher->dispatch(ProductChangedEvent::NAME, $event);
}
}
}
}
}
......
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Tests\Listener;
use PcmtRulesBundle\Listener\LoggerSubscriber;
use PcmtRulesBundle\Tests\TestDataBuilder\ProductChangedEventBuilder;
use PcmtRulesBundle\Tests\TestDataBuilder\ProductModelChangedEventBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class LoggerSubscriberTest extends TestCase
{
/** @var LoggerInterface|MockObject */
private $loggerMock;
protected function setUp(): void
{
$this->loggerMock = $this->createMock(LoggerInterface::class);
}
public function testProductValueUpdated(): void
{
$this->loggerMock->expects($this->once())->method('info');
$s = new LoggerSubscriber($this->loggerMock);
$s->productValueUpdated(
(new ProductChangedEventBuilder())->build()
);
}
public function testProductModelValueUpdated(): void
{
$this->loggerMock->expects($this->once())->method('info');
$s = new LoggerSubscriber($this->loggerMock);
$s->productModelValueUpdated(
(new ProductModelChangedEventBuilder())->build()
);
}
public function testGetSubscribedEvents(): void
{
$this->assertIsArray(LoggerSubscriber::getSubscribedEvents());
}
}
\ No newline at end of file
......@@ -24,6 +24,7 @@ use PcmtRulesBundle\Tests\TestDataBuilder\ProductBuilder;
use PcmtRulesBundle\Tests\TestDataBuilder\ProductModelBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class RuleProcessorCopierTest extends TestCase
......@@ -46,6 +47,9 @@ class RuleProcessorCopierTest extends TestCase
/** @var NormalizerInterface|MockObject */
private $normalizerMock;
/** @var EventDispatcherInterface|MockObject */
private $eventDispatcherMock;
protected function setUp(): void
{
$this->propertyCopierMock = $this->createMock(PropertyCopierInterface::class);
......@@ -54,6 +58,7 @@ class RuleProcessorCopierTest extends TestCase
$this->productAttributeFilterMock = $this->createMock(ProductAttributeFilter::class);
$this->productModelAttributeFilterMock = $this->createMock(ProductModelAttributeFilter::class);
$this->normalizerMock = $this->createMock(NormalizerInterface::class);
$this->eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
}
public function dataCopy(): array
......@@ -126,7 +131,8 @@ class RuleProcessorCopierTest extends TestCase
$this->localeRepositoryMock,
$this->productAttributeFilterMock,
$this->productModelAttributeFilterMock,
$this->normalizerMock
$this->normalizerMock,
$this->eventDispatcherMock
);
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Tests\TestDataBuilder;
use PcmtRulesBundle\Event\ProductChangedEvent;
class ProductChangedEventBuilder
{
/** @var ProductChangedEvent */
private $event;
public function __construct()
{
$this->event = new ProductChangedEvent(
(new ProductBuilder())->build(),
(new AttributeBuilder())->build(),
'en',
'channel',
(new ValueBuilder())->build(),
(new ValueBuilder())->build()
);
}
public function build(): ProductChangedEvent
{
return $this->event;
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021, VillageReach
* Licensed under the Non-Profit Open Software License version 3.0.
* SPDX-License-Identifier: NPOSL-3.0
*/
namespace PcmtRulesBundle\Tests\TestDataBuilder;
use PcmtRulesBundle\Event\ProductModelChangedEvent;
class ProductModelChangedEventBuilder
{
/** @var ProductModelChangedEvent */
private $event;
public function __construct()
{
$this->event = new ProductModelChangedEvent(
(new ProductModelBuilder())->build(),
(new AttributeBuilder())->build(),
'en',
'channel',
(new ValueBuilder())->build(),
(new ValueBuilder())->build()
);
}
public function build(): ProductModelChangedEvent
{
return $this->event;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment