Skip to content

Commit 6357c46

Browse files
author
Alexander Smaga
committed
Merge pull request #1 from akuzmenko-magecore/master
Integration with PrestaShop
2 parents e1185cc + 01776cd commit 6357c46

18 files changed

+690
-24
lines changed

OroTutorial/Bundle/PrestashopBundle/DependencyInjection/OroTutorialPrestashopExtension.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ public function load(array $configs, ContainerBuilder $container)
1616
{
1717
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
1818
$loader->load('services.yml');
19+
$loader->load('importexport.yml');
1920
}
2021
}

OroTutorial/Bundle/PrestashopBundle/Entity/Customer.php

+69
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
use Doctrine\ORM\Mapping as ORM;
66

7+
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
78
use Oro\Bundle\BusinessEntitiesBundle\Entity\BasePerson;
9+
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;
810
use Oro\Bundle\IntegrationBundle\Model\IntegrationEntityTrait;
911

12+
use OroCRM\Bundle\AccountBundle\Entity\Account;
13+
use OroCRM\Bundle\ContactBundle\Entity\Contact;
14+
1015
/**
1116
* @ORM\Entity
1217
* @ORM\Table(
1318
* name="ot_prestashop_customer",
1419
* uniqueConstraints={@ORM\UniqueConstraint(name="unq_remote_id_channel_id", columns={"remote_id", "channel_id"})}
1520
* )
21+
* @Config()
1622
*/
1723
class Customer extends BasePerson
1824
{
@@ -26,10 +32,33 @@ class Customer extends BasePerson
2632
/**
2733
* @var integer
2834
*
35+
* @ConfigField(
36+
* defaultValues={
37+
* "importexport"={
38+
* "identity"=true
39+
* }
40+
* }
41+
* )
2942
* @ORM\Column(name="remote_id", type="integer", options={"unsigned"=true}, nullable=false)
3043
*/
3144
protected $remoteId;
3245

46+
/**
47+
* @var Contact
48+
*
49+
* @ORM\ManyToOne(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact", cascade="PERSIST")
50+
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="SET NULL")
51+
*/
52+
protected $contact;
53+
54+
/**
55+
* @var Account
56+
*
57+
* @ORM\ManyToOne(targetEntity="OroCRM\Bundle\AccountBundle\Entity\Account", cascade="PERSIST")
58+
* @ORM\JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL")
59+
*/
60+
protected $account;
61+
3362
/**
3463
* @param int $remoteId
3564
*
@@ -47,4 +76,44 @@ public function getRemoteId()
4776
{
4877
return $this->remoteId;
4978
}
79+
80+
/**
81+
* @param Contact $contact
82+
*
83+
* @return Customer
84+
*/
85+
public function setContact($contact)
86+
{
87+
$this->contact = $contact;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* @return Contact
94+
*/
95+
public function getContact()
96+
{
97+
return $this->contact;
98+
}
99+
100+
/**
101+
* @param Account $account
102+
*
103+
* @return Customer
104+
*/
105+
public function setAccount($account)
106+
{
107+
$this->account = $account;
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* @return Account
114+
*/
115+
public function getAccount()
116+
{
117+
return $this->account;
118+
}
50119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace OroTutorial\Bundle\PrestashopBundle\ImportExport\Converter;
4+
5+
use Oro\Bundle\ImportExportBundle\Converter\AbstractTableDataConverter;
6+
7+
class CustomerDataConverter extends AbstractTableDataConverter
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
protected function getHeaderConversionRules()
13+
{
14+
return [
15+
'id' => 'remoteId',
16+
'email' => 'email',
17+
'firstname' => 'firstName',
18+
'lastname' => 'lastName',
19+
'birthday' => 'birthday',
20+
'date_add' => 'createdAt',
21+
'date_upd' => 'updatedAt',
22+
];
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function getBackendHeader()
29+
{
30+
// will be implemented for bidirectional sync
31+
throw new \Exception('Normalization is not implemented!');
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace OroTutorial\Bundle\PrestashopBundle\ImportExport\Serializer;
4+
5+
use Symfony\Bridge\Doctrine\RegistryInterface;
6+
7+
use Oro\Bundle\ImportExportBundle\Field\FieldHelper;
8+
use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\ConfigurableEntityNormalizer;
9+
10+
use OroTutorial\Bundle\PrestashopBundle\Entity\Customer;
11+
12+
class CustomerNormalizer extends ConfigurableEntityNormalizer
13+
{
14+
/** @var RegistryInterface */
15+
protected $registry;
16+
17+
/**
18+
* @param FieldHelper $fieldHelper
19+
* @param RegistryInterface $registry
20+
*/
21+
public function __construct(FieldHelper $fieldHelper, RegistryInterface $registry)
22+
{
23+
parent::__construct($fieldHelper);
24+
$this->registry = $registry;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function supportsNormalization($data, $format = null, array $context = array())
31+
{
32+
return $data instanceof Customer;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function supportsDenormalization($data, $type, $format = null, array $context = array())
39+
{
40+
return $type == 'OroTutorial\\Bundle\\PrestashopBundle\\Entity\\Customer';
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function denormalize($data, $class, $format = null, array $context = array())
47+
{
48+
/** @var Customer $customer */
49+
$customer = parent::denormalize($data, $class, $format, $context);
50+
51+
$integration = $this->getIntegrationFromContext($context);
52+
$customer->setChannel($integration);
53+
54+
return $customer;
55+
}
56+
57+
/**
58+
* @param array $context
59+
*
60+
* @return Integration
61+
* @throws \LogicException
62+
*/
63+
public function getIntegrationFromContext(array $context)
64+
{
65+
if (!isset($context['channel'])) {
66+
throw new \LogicException('Context should contain reference to channel');
67+
}
68+
69+
return $this->registry
70+
->getRepository('OroIntegrationBundle:Channel')
71+
->getOrLoadById($context['channel']);
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace OroTutorial\Bundle\PrestashopBundle\ImportExport\Serializer;
4+
5+
use Symfony\Component\Serializer\Exception\RuntimeException;
6+
7+
use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\DateTimeNormalizer as BaseNormalizer;
8+
use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\DenormalizerInterface;
9+
use Oro\Bundle\ImportExportBundle\Serializer\Normalizer\NormalizerInterface;
10+
use Oro\Bundle\ImportExportBundle\Serializer\Serializer;
11+
12+
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
13+
{
14+
public function __construct()
15+
{
16+
$this->prestashopNormalizer = new BaseNormalizer('Y-m-d H:i:s', 'Y-m-d', 'H:i:s', 'UTC');
17+
$this->isoNormalizer = new BaseNormalizer(\DateTime::ISO8601, 'Y-m-d', 'H:i:s', 'UTC');
18+
$this->dateNormalizer = new BaseNormalizer('Y-m-d', 'Y-m-d', 'H:i:s', 'UTC');
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function denormalize($data, $class, $format = null, array $context = array())
25+
{
26+
try {
27+
return $this->prestashopNormalizer->denormalize($data, $class, $format, $context);
28+
} catch (RuntimeException $e) {
29+
return $this->dateNormalizer->denormalize($data, $class, $format, $context);
30+
}
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function normalize($object, $format = null, array $context = array())
37+
{
38+
return $this->prestashopNormalizer->normalize($object, $format, $context);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function supportsDenormalization($data, $type, $format = null, array $context = array())
45+
{
46+
return $this->prestashopNormalizer->supportsDenormalization($data, $type, $format, $context)
47+
&& !empty($context[Serializer::PROCESSOR_ALIAS_KEY])
48+
&& strpos($context[Serializer::PROCESSOR_ALIAS_KEY], 'presta_shop') !== false;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function supportsNormalization($data, $format = null, array $context = array())
55+
{
56+
return $this->prestashopNormalizer->supportsNormalization($data, $format, $context)
57+
&& !empty($context[Serializer::PROCESSOR_ALIAS_KEY])
58+
&& strpos($context[Serializer::PROCESSOR_ALIAS_KEY], 'presta_shop') !== false;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace OroTutorial\Bundle\PrestashopBundle\ImportExport\Strategy;
4+
5+
use Psr\Log\LoggerAwareInterface;
6+
use Psr\Log\LoggerInterface;
7+
8+
use Doctrine\Common\Util\ClassUtils;
9+
10+
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy;
11+
12+
class ImportStrategy extends ConfigurableAddOrReplaceStrategy implements
13+
LoggerAwareInterface
14+
{
15+
/**
16+
* @var LoggerInterface
17+
*/
18+
protected $logger;
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function setLogger(LoggerInterface $logger)
24+
{
25+
$this->logger = $logger;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function beforeProcessEntity($entity)
32+
{
33+
if ($this->logger) {
34+
$this->logger->info('Syncing PrestaShop Customer [origin_id=' . $entity->getRemoteId() . ']');
35+
}
36+
37+
return parent::beforeProcessEntity($entity);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function process($entity)
44+
{
45+
$this->assertEnvironment($entity);
46+
47+
$this->cachedEntities = array();
48+
$entity = $this->beforeProcessEntity($entity);
49+
$entity = $this->processEntity($entity, false, true, $this->context->getValue('itemData'));
50+
$entity = $this->afterProcessEntity($entity);
51+
$entity = $this->validateAndUpdateContext($entity);
52+
53+
return $entity;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function findExistingEntity($entity, array $searchContext = array())
60+
{
61+
$entityName = ClassUtils::getClass($entity);
62+
$existingEntity = null;
63+
64+
// find by identity fields
65+
if (!$searchContext || $this->databaseHelper->getIdentifier(current($searchContext))
66+
) {
67+
$identityValues = $searchContext;
68+
$identityValues += $this->fieldHelper->getIdentityValues($entity);
69+
// add channel filter for finding existing entity
70+
$identityValues += ['channel' => $entity->getChannel()];
71+
$existingEntity = $this->findEntityByIdentityValues($entityName, $identityValues);
72+
}
73+
74+
return $existingEntity;
75+
}
76+
}

OroTutorial/Bundle/PrestashopBundle/Migrations/Schema/OroTutorialPrestashopBundleInstaller.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OroTutorial\Bundle\PrestashopBundle\Migrations\Schema\v1_0\OroTutorialPrestashopBundle as v1_0;
1111
use OroTutorial\Bundle\PrestashopBundle\Migrations\Schema\v1_1\OroTutorialPrestashopBundle as v1_1;
12+
use OroTutorial\Bundle\PrestashopBundle\Migrations\Schema\v1_2\OroTutorialPrestashopBundle as v1_2;
1213

1314
class OroTutorialPrestashopBundleInstaller implements Installation
1415
{
@@ -17,7 +18,7 @@ class OroTutorialPrestashopBundleInstaller implements Installation
1718
*/
1819
public function getMigrationVersion()
1920
{
20-
return 'v1_1';
21+
return 'v1_2';
2122
}
2223

2324
/**
@@ -27,5 +28,6 @@ public function up(Schema $schema, QueryBag $queries)
2728
{
2829
v1_0::customerTable($schema);
2930
v1_1::restTransportTable($schema);
31+
v1_2::addContactAndAccountRelations($schema);
3032
}
3133
}

OroTutorial/Bundle/PrestashopBundle/Migrations/Schema/v1_0/OroTutorialPrestashopBundle.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function customerTable(Schema $schema)
2727
/** Generate table ot_prestashop_customer **/
2828
$table = $schema->createTable('ot_prestashop_customer');
2929
$table->addColumn('id', 'integer', ['autoincrement' => true]);
30-
$table->addColumn('channel_id', 'smallint', ['notnull' => false]);
30+
$table->addColumn('channel_id', 'integer', ['notnull' => false]);
3131
$table->addColumn('remote_id', 'integer', ['unsigned' => true]);
3232
$table->addColumn('name_prefix', 'string', ['notnull' => false, 'length' => 255]);
3333
$table->addColumn('first_name', 'string', ['notnull' => false, 'length' => 255]);

OroTutorial/Bundle/PrestashopBundle/Migrations/Schema/v1_1/OroTutorialPrestashopBundle.php

-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public function up(Schema $schema, QueryBag $queries)
2424
*/
2525
public static function restTransportTable(Schema $schema)
2626
{
27-
/** Generate table oro_integration_transport **/
2827
$table = $schema->getTable('oro_integration_transport');
2928
$table->addColumn('prestashop_rest_endpoint', 'string', ['notnull' => false, 'length' => 255]);
3029
$table->addColumn('prestashop_rest_api_key', 'string', ['notnull' => false, 'length' => 255]);
31-
/** End of generate foreign keys for table oro_integration_transport **/
3230
}
3331
}

0 commit comments

Comments
 (0)