Skip to content

Commit

Permalink
Merge pull request #10 from dachcom-digital/version_reycle_fix
Browse files Browse the repository at this point in the history
Allow Object Versioning and Recycling
  • Loading branch information
solverat authored Jan 18, 2021
2 parents 55edf9a + fc37bd5 commit c0d55bc
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 25 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Just click the "update" button or execute the migration command to finish the bu
#### Update from Version 1.0.0 to Version 1.1.0
- **[ENHANCEMENT]**: Drop Symfony 3.4 Support [#6](https://github.com/dachcom-digital/pimcore-jobs/issues/6)
- **[BUGFIX]** Fix copy object issue [#4](https://github.com/dachcom-digital/pimcore-jobs/issues/4)
- **[BUGFIX]** Fix versioning / recycling object [#9](https://github.com/dachcom-digital/pimcore-jobs/issues/9)
76 changes: 72 additions & 4 deletions src/JobsBundle/CoreExtension/JobConnectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
use JobsBundle\Manager\ConnectorManagerInterface;
use JobsBundle\Manager\LogManager;
use JobsBundle\Manager\LogManagerInterface;
use JobsBundle\Model\ConnectorContextItem;
use JobsBundle\Model\ConnectorContextItemInterface;
use JobsBundle\Model\ConnectorEngineInterface;
use JobsBundle\Model\ContextDefinitionInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\ValidationException;
use Symfony\Component\Serializer\Serializer;

class JobConnectorContext extends Data implements Data\CustomResourcePersistingInterface
class JobConnectorContext extends Data implements
Data\CustomResourcePersistingInterface,
Data\CustomVersionMarshalInterface,
Data\CustomRecyclingMarshalInterface
{
/**
* Static type of this element.
Expand Down Expand Up @@ -53,6 +58,14 @@ private function getLogManager()
return \Pimcore::getContainer()->get(LogManager::class);
}

/**
* @return Serializer
*/
protected function getSerializer()
{
return \Pimcore::getContainer()->get('serializer');
}

/**
* @param mixed $object
*
Expand Down Expand Up @@ -124,7 +137,11 @@ public function getDataForEditmode($data, $object = null, $params = [])
return $data;
}

return $this->getConnectorContextManager()->generateConnectorContextConfigForObject($object->getId());
if (!is_array($data)) {
$data = [];
}

return $this->getConnectorContextManager()->generateConnectorContextConfig($data);
}

/**
Expand Down Expand Up @@ -169,6 +186,7 @@ public function getDataFromEditmode($data, $object = null, $params = [])
}

foreach ($contextItems as $contextConfig) {

if ($contextConfig['active'] === false) {
continue;
}
Expand All @@ -184,8 +202,6 @@ public function getDataFromEditmode($data, $object = null, $params = [])
$item->setObjectId($object->getId());
$item->setContextDefinition($this->getConnectorContextManager()->getContextDefinition($contextConfig['id']));

$this->getConnectorContextManager()->update($item);

$items[] = $item;
}
}
Expand Down Expand Up @@ -326,6 +342,58 @@ protected function markAsLoaded($object)
$object->markLazyKeyAsLoaded($this->getName());
}

/**
* {@inheritdoc}
*/
public function marshalVersion($object, $data)
{
if (!is_array($data)) {
return [];
}

return $this->getSerializer()->normalize($data, 'array', ['groups' => ['Version']]);
}

/**
* {@inheritdoc}
*/
public function unmarshalVersion($object, $data)
{
if (!is_array($data)) {
return [];
}

return array_filter(
$this->getSerializer()->denormalize($data, sprintf('%s[]', ConnectorContextItem::class))
);
}

/**
* {@inheritdoc}
*/
public function marshalRecycleData($object, $data)
{
if (!is_array($data)) {
return [];
}

return $this->getSerializer()->normalize($data, 'array', ['groups' => ['Version']]);
}

/**
* {@inheritdoc}
*/
public function unmarshalRecycleData($object, $data)
{
if (!is_array($data)) {
return [];
}

return array_filter(
$this->getSerializer()->denormalize($data, sprintf('%s[]', ConnectorContextItem::class))
);
}

/**
* {@inheritdoc}
*/
Expand Down
3 changes: 1 addition & 2 deletions src/JobsBundle/Manager/ConnectorContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function delete(ConnectorContextItemInterface $connectorContextItem)
/**
* {@inheritdoc}
*/
public function generateConnectorContextConfigForObject(int $objectId)
public function generateConnectorContextConfig(array $connectorContextItems)
{
$context = [];
$onlineConnectors = [];
Expand All @@ -144,7 +144,6 @@ public function generateConnectorContextConfigForObject(int $objectId)
'context_definitions' => $contextDefinitions
];

$connectorContextItems = $this->getForObject($objectId);
$onlineConnectorDefinitions = $this->connectorManager->getAllConnectorDefinitions(true);

foreach ($onlineConnectorDefinitions as $connectorDefinition) {
Expand Down
4 changes: 2 additions & 2 deletions src/JobsBundle/Manager/ConnectorContextManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function update(ConnectorContextItemInterface $connectorContextItem);
public function delete(ConnectorContextItemInterface $connectorContextItem);

/**
* @param int $objectId
* @param array $connectorContextItems
*
* @return ConnectorContextItemInterface[]
*/
public function generateConnectorContextConfigForObject(int $objectId);
public function generateConnectorContextConfig(array $connectorContextItems);
}
65 changes: 65 additions & 0 deletions src/JobsBundle/Normalizer/EntityNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace JobsBundle\Normalizer;

use Doctrine\ORM\EntityManagerInterface;
use JobsBundle\Model\ConnectorContextItem;
use JobsBundle\Model\ConnectorEngine;
use JobsBundle\Model\ContextDefinition;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

class EntityNormalizer implements DenormalizerInterface
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;

/**
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* {@inheritDoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return \is_a($type, ConnectorContextItem::class, true);
}

/**
* {@inheritDoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
$connectorContextItem = null;

if (is_numeric($data['id'])) {
$connectorContextItem = $this->entityManager->find(ConnectorContextItem::class, $data['id']);
}

if (!$connectorContextItem instanceof ConnectorContextItem) {
$connectorContextItem = new ConnectorContextItem();
}

$connectorEngine = $this->entityManager->find(ConnectorEngine::class, $data['connectorEngine']['id']);
if (!$connectorEngine instanceof ConnectorEngine) {
return null;
}

$contextDefinition = $this->entityManager->find(ContextDefinition::class, $data['contextDefinition']['id']);
if (!$contextDefinition instanceof ContextDefinition) {
return null;
}

$connectorContextItem->setObjectId($data['objectId']);
$connectorContextItem->setConnectorEngine($connectorEngine);
$connectorContextItem->setContextDefinition($contextDefinition);

return $connectorContextItem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
JobsBundle\Model\ConnectorContextItem:
attributes:
id:
groups: [ Default, Version ]
objectId:
groups: [ Default, Version ]
connectorEngine:
groups: [ Default, Version ]
contextDefinition:
groups: [ Default, Version ]
14 changes: 14 additions & 0 deletions src/JobsBundle/Resources/config/serialization/ConnectorEngine.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
JobsBundle\Model\ConnectorEngine:
attributes:
id:
groups: [ Default, Version ]
name:
groups: [ Default ]
enabled:
groups: [ Default ]
token:
groups: [ Default ]
configuration:
groups: [ Default ]
feedIds:
groups: [ Default ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
JobsBundle\Model\ContextDefinition:
attributes:
id:
groups: [ Version ]
host:
groups: [ Default ]
locale:
groups: [ Default ]
6 changes: 5 additions & 1 deletion src/JobsBundle/Resources/config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ services:
JobsBundle\Service\LinkGeneratorService: ~

JobsBundle\Service\EnvironmentServiceInterface: '@JobsBundle\Service\EnvironmentService'
JobsBundle\Service\EnvironmentService: ~
JobsBundle\Service\EnvironmentService: ~

JobsBundle\Normalizer\EntityNormalizer:
tags:
- serializer.normalizer
Loading

0 comments on commit c0d55bc

Please sign in to comment.