Skip to content

Commit

Permalink
improve: add refiners
Browse files Browse the repository at this point in the history
  • Loading branch information
ugoliniriccardo committed Sep 29, 2022
1 parent 73d80c4 commit 343efdc
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 2 deletions.
118 changes: 118 additions & 0 deletions Refiner/Json/CopyFromOriginalOrEmptyStringRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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 CopyFromOriginalOrEmptyStringRefiner implements RefinerInterface
{
/**
* @var Json
*/
private $serializer;

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

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

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

private $destinationType;

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

/**
* @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);
switch ($this->destinationType) {
case 'float':
$value = (float)$value;
break;
case 'int':
$value = (int)$value;
break;
}

if (!$value) {
$value = '';
}

$this->dotConvention->setValue($dataRefined, $destination, $value);
} catch(\Exception $exception) {
$dataRefined[$destination] = '';
}

$data = $this->serializer->serialize($dataRefined);
$entity->setDataRefined($data);
}
}
16 changes: 14 additions & 2 deletions Refiner/Json/CopyFromOriginalOrNullRefiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class CopyFromOriginalOrNullRefiner implements RefinerInterface
*/
private $destination;

private $destinationType;

/**
* @param Json $serializer
* @param DotConvention $dotConvention
Expand All @@ -47,12 +49,14 @@ public function __construct(
Json $serializer,
DotConvention $dotConvention,
string $source,
string $destination
string $destination,
$destinationType = 'string'
) {
$this->serializer = $serializer;
$this->dotConvention = $dotConvention;
$this->source = $source;
$this->destination = $destination;
$this->destinationType = $destinationType;
}

/**
Expand Down Expand Up @@ -90,8 +94,16 @@ public function execute(int $activityId, string $refinerType, string $entityIden

try {
$value = $this->dotConvention->getValue($dataOriginal, $source);
switch ($this->destinationType) {
case 'float':
$value = (float)$value;
break;
case 'int':
$value = (int)$value;
break;
}
$this->dotConvention->setValue($dataRefined, $destination, $value);
}catch(\Exception $exception) {
} catch(\Exception $exception) {
$dataRefined[$destination] = null;
}

Expand Down
75 changes: 75 additions & 0 deletions Refiner/Json/FixedEmptyStringValueRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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 FixedEmptyStringValueRefiner implements RefinerInterface
{
/**
* @var Json
*/
private $serializer;

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

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

/**
* @param Json $serializer
* @param DotConvention $dotConvention
* @param string $destination
* @param $value
*/
public function __construct(
Json $serializer,
DotConvention $dotConvention,
string $destination
) {
$this->serializer = $serializer;
$this->dotConvention = $dotConvention;
$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
{
$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);

$this->dotConvention->setValue($data, $identifiers, '');

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

0 comments on commit 343efdc

Please sign in to comment.