Skip to content

Commit

Permalink
improve: add refiner for null values and refiner that keep data refin…
Browse files Browse the repository at this point in the history
…ed clean
  • Loading branch information
ugoliniriccardo committed Jul 26, 2022
1 parent 6915a47 commit 88dd1b6
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
101 changes: 101 additions & 0 deletions Refiner/Json/CopyFromOriginalOrNullRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\CrtUtils\Refiner\Json;

use GhostUnicorns\CrtBase\Api\RefinerInterface;
use GhostUnicorns\CrtBase\Exception\CrtException;
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface;
use GhostUnicorns\CrtEntity\Model\EntityModel;
use GhostUnicorns\CrtUtils\Model\DotConvention;
use Magento\Framework\Serialize\Serializer\Json;

class CopyFromOriginalOrNullRefiner implements RefinerInterface
{
/**
* @var Json
*/
private $serializer;

/**
* @var DotConvention
*/
private $dotConvention;

/**
* @var string
*/
private $source;

/**
* @var string
*/
private $destination;

/**
* @param Json $serializer
* @param DotConvention $dotConvention
* @param string $source
* @param string $destination
*/
public function __construct(
Json $serializer,
DotConvention $dotConvention,
string $source,
string $destination
) {
$this->serializer = $serializer;
$this->dotConvention = $dotConvention;
$this->source = $source;
$this->destination = $destination;
}

/**
* @param int $activityId
* @param string $refinerType
* @param string $entityIdentifier
* @param EntityInterface[] $entities
* @throws CrtException
*/
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void
{
$sourceIdentifier = $this->dotConvention->getFirst($this->source);
$source = $this->dotConvention->getFromSecondInDotConvention($this->source);

if (!array_key_exists($sourceIdentifier, $entities)) {
throw new CrtException(__('Invalid sourceIdentifier for class:%1', self::class));
}

$destinationIdentifier = $this->dotConvention->getFirst($this->destination);
$destination = $this->dotConvention->getFromSecondInDotConvention($this->destination);

if (!array_key_exists($destinationIdentifier, $entities)) {
throw new CrtException(__('Invalid destinationIdentifier for class:%1', self::class));
}

$entity = $entities[$sourceIdentifier];
$dataOriginal = $entity->getDataOriginal();
$dataRefined = $entity->getData(EntityModel::DATA_REFINED) ?? [];

if ($dataRefined) {
$dataRefined = $this->serializer->unserialize($dataRefined);
}

$dataOriginal = $this->serializer->unserialize($dataOriginal);

try {
$value = $this->dotConvention->getValue($dataOriginal, $source);
$this->dotConvention->setValue($dataRefined, $destination, $value);
}catch(\Exception $exception) {
$dataRefined[$destination] = null;
}

$data = $this->serializer->serialize($dataRefined);
$entity->setDataRefined($data);
}
}
96 changes: 96 additions & 0 deletions Refiner/Json/CopyFromOriginalRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\CrtUtils\Refiner\Json;

use GhostUnicorns\CrtBase\Api\RefinerInterface;
use GhostUnicorns\CrtBase\Exception\CrtException;
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface;
use GhostUnicorns\CrtEntity\Model\EntityModel;
use GhostUnicorns\CrtUtils\Model\DotConvention;
use Magento\Framework\Serialize\Serializer\Json;

class CopyFromOriginalRefiner implements RefinerInterface
{
/**
* @var Json
*/
private $serializer;

/**
* @var DotConvention
*/
private $dotConvention;

/**
* @var string
*/
private $source;

/**
* @var string
*/
private $destination;

/**
* @param Json $serializer
* @param DotConvention $dotConvention
* @param string $source
* @param string $destination
*/
public function __construct(
Json $serializer,
DotConvention $dotConvention,
string $source,
string $destination
) {
$this->serializer = $serializer;
$this->dotConvention = $dotConvention;
$this->source = $source;
$this->destination = $destination;
}

/**
* @param int $activityId
* @param string $refinerType
* @param string $entityIdentifier
* @param EntityInterface[] $entities
* @throws CrtException
*/
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void
{
$sourceIdentifier = $this->dotConvention->getFirst($this->source);
$source = $this->dotConvention->getFromSecondInDotConvention($this->source);

if (!array_key_exists($sourceIdentifier, $entities)) {
throw new CrtException(__('Invalid sourceIdentifier for class:%1', self::class));
}

$destinationIdentifier = $this->dotConvention->getFirst($this->destination);
$destination = $this->dotConvention->getFromSecondInDotConvention($this->destination);

if (!array_key_exists($destinationIdentifier, $entities)) {
throw new CrtException(__('Invalid destinationIdentifier for class:%1', self::class));
}

$entity = $entities[$sourceIdentifier];
$dataOriginal = $entity->getDataOriginal();
$dataRefined = $entity->getData(EntityModel::DATA_REFINED) ?? [];

if ($dataRefined) {
$dataRefined = $this->serializer->unserialize($dataRefined);
}

$dataOriginal = $this->serializer->unserialize($dataOriginal);
$value = $this->dotConvention->getValue($dataOriginal, $source);
$this->dotConvention->setValue($dataRefined, $destination, $value);

$data = $this->serializer->serialize($dataRefined);
$entity->setDataRefined($data);
}
}
82 changes: 82 additions & 0 deletions Refiner/Json/FixedNullValueRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\CrtUtils\Refiner\Json;

use GhostUnicorns\CrtBase\Api\RefinerInterface;
use GhostUnicorns\CrtBase\Exception\CrtException;
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface;
use GhostUnicorns\CrtUtils\Model\DotConvention;
use Magento\Framework\Serialize\Serializer\Json;

class FixedNullValueRefiner implements RefinerInterface
{
/**
* @var Json
*/
private $serializer;

/**
* @var DotConvention
*/
private $dotConvention;

/**
* @var string
*/
private $destination;

/**
* @var mixed
*/
private $value;

/**
* @param Json $serializer
* @param DotConvention $dotConvention
* @param string $destination
* @param $value
*/
public function __construct(
Json $serializer,
DotConvention $dotConvention,
string $destination,
$value
) {
$this->serializer = $serializer;
$this->dotConvention = $dotConvention;
$this->destination = $destination;
$this->value = $value;
}

/**
* @param int $activityId
* @param string $refinerType
* @param string $entityIdentifier
* @param EntityInterface[] $entities
* @throws CrtException
*/
public function execute(int $activityId, string $refinerType, string $entityIdentifier, array $entities): void
{
$collectorIdentifier = $this->dotConvention->getFirst($this->destination);
$identifiers = $this->dotConvention->getFromSecondInDotConvention($this->destination);

if (!array_key_exists($collectorIdentifier, $entities)) {
throw new CrtException(__('Invalid collectorIdentifier for class:%1', self::class));
}

$entity = $entities[$collectorIdentifier];
$data = $entity->getDataRefined();
$data = $this->serializer->unserialize($data);

$data[$identifiers] = null;

$data = $this->serializer->serialize($data);
$entity->setDataRefined($data);
}
}

0 comments on commit 88dd1b6

Please sign in to comment.