Skip to content

Commit

Permalink
Merge pull request #101 from gyKa/multiple_shops
Browse files Browse the repository at this point in the history
Fix multiple shop id functionality.
  • Loading branch information
GrandLTU committed Jan 19, 2015
2 parents 1d053b0 + 3f74904 commit 938dcb4
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 356 deletions.
5 changes: 4 additions & 1 deletion Resources/config/extractor.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
parameters:
ongr_connections.extractor.doctrine_extractor.class: ONGR\ConnectionsBundle\Sync\Extractor\DoctrineExtractor
ongr_connections.extractor.passthrough_extractor.class: ONGR\ConnectionsBundle\Sync\Extractor\PassthroughExtractor
ongr_connections.sync.relations_collection.class: ONGR\ConnectionsBundle\Sync\Extractor\Relation\RelationsCollection

services:
ongr_connections.sync.extractor.doctrine_extractor:
class: ONGR\ConnectionsBundle\Sync\Extractor\DoctrineExtractor
class: %ongr_connections.extractor.doctrine_extractor.class%
calls:
- [ setRelationsCollection, [ @ongr_connections.sync.relations_collection ] ]
- [ setConnection, [ @database_connection ] ]
- [ setStorageFacility, [ @ongr_connections.sync.sync_storage ] ]
- [ setContainer, [ @service_container ] ]

ongr_connections.sync.extractor.passthrough_extractor:
class: %ongr_connections.extractor.passthrough_extractor.class%
calls:
- [ setStorageFacility, [ @ongr_connections.sync.sync_storage ] ]
- [ setContainer, [ @service_container ] ]

ongr_connections.sync.relations_collection:
class: %ongr_connections.sync.relations_collection.class%
50 changes: 50 additions & 0 deletions Sync/Extractor/AbstractExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ConnectionsBundle\Sync\Extractor;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/**
* Common actions required for all extractors.
*/
abstract class AbstractExtractor
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* Gets ids of shops from the configuration.
*
* @return array
*/
protected function getShopIds()
{
try {
$shops = $this->container->getParameter('shop_ids');
} catch (InvalidArgumentException $e) {
$shops = [];
}

return $shops;
}
}
13 changes: 10 additions & 3 deletions Sync/Extractor/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Extractor that joins entities for insertion to SyncStorage.
*/
class DoctrineExtractor implements ExtractorInterface
class DoctrineExtractor extends AbstractExtractor implements ExtractorInterface
{
/**
* @var SyncStorageInterface
Expand Down Expand Up @@ -69,7 +69,13 @@ public function extract(BaseDiffItem $item)
$itemId = $itemRow[$idFieldName];

$storage = $this->getStorageFacility();
$storage->save($action, $insertList[JobTableFields::TYPE]['value'], $itemId, $item->getTimestamp());
$storage->save(
$action,
$insertList[JobTableFields::TYPE]['value'],
$itemId,
$item->getTimestamp(),
$this->getShopIds()
);

$statements = $relation->getStatements();
foreach ($statements as $statement) {
Expand Down Expand Up @@ -201,7 +207,8 @@ protected function saveResult(BaseDiffItem $item, Statement $results, $action =
$action,
$row[JobTableFields::TYPE],
$row[JobTableFields::ID],
$item->getTimestamp()
$item->getTimestamp(),
$this->getShopIds()
);
}
}
Expand Down
43 changes: 22 additions & 21 deletions Sync/Extractor/PassthroughExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Very simple data extractor for data synchronization.
*/
class PassthroughExtractor implements ExtractorInterface
class PassthroughExtractor extends AbstractExtractor implements ExtractorInterface
{
/**
* @var SyncStorageInterface
Expand All @@ -34,34 +34,18 @@ class PassthroughExtractor implements ExtractorInterface
*/
public function extract(BaseDiffItem $item)
{
$itemId = $item->getItemId();
if (!is_numeric($itemId)) {
if (!is_numeric($item->getItemId())) {
throw new InvalidArgumentException('No valid item ID provided.');
}

if ($item instanceof CreateDiffItem) {
$this->storage->save(
ActionTypes::CREATE,
$item->getCategory(),
$itemId,
$item->getTimestamp()
);
$this->saveResult($item, ActionTypes::CREATE);
}
if ($item instanceof UpdateDiffItem) {
$this->storage->save(
ActionTypes::UPDATE,
$item->getCategory(),
$itemId,
$item->getTimestamp()
);
$this->saveResult($item, ActionTypes::UPDATE);
}
if ($item instanceof DeleteDiffItem) {
$this->storage->save(
ActionTypes::DELETE,
$item->getCategory(),
$itemId,
$item->getTimestamp()
);
$this->saveResult($item, ActionTypes::DELETE);
}
}

Expand All @@ -80,4 +64,21 @@ public function getStorageFacility()
{
return $this->storage;
}

/**
* Save results to storage.
*
* @param BaseDiffItem $item
* @param string $action
*/
private function saveResult(BaseDiffItem $item, $action)
{
$this->storage->save(
$action,
$item->getCategory(),
$item->getItemId(),
$item->getTimestamp(),
$this->getShopIds()
);
}
}
11 changes: 6 additions & 5 deletions Sync/StorageManager/MysqlStorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public function createStorage($shopId = null, $connection = null)
{
$connection = $connection ? : $this->getConnection();
$schemaManager = $connection->getSchemaManager();
$tableName = $this->getTableName($shopId);

if ($schemaManager->tablesExist([$this->getTableName($shopId)])) {
if ($schemaManager->tablesExist([$tableName])) {
return true;
}

$table = new Table($this->getTableName($shopId));
$table = new Table($tableName);
$this->buildTable($table);
$schemaManager->createTable($table);

Expand Down Expand Up @@ -85,12 +86,11 @@ public function getTableName($shopId = null)
{
$tableName = parent::getTableName();

$suffix = null;
if ($shopId !== null) {
$suffix = '_' . $shopId;
$tableName .= '_' . $shopId;
}

return $tableName . $suffix;
return $tableName;
}

/**
Expand All @@ -101,6 +101,7 @@ public function addRecord($operationType, $documentType, $documentId, DateTime $
if (empty($shopIds)) {
$shopIds = [null];
}

$connection = $this->getConnection();

foreach ($shopIds as $shopId) {
Expand Down
Loading

0 comments on commit 938dcb4

Please sign in to comment.