Skip to content

Commit

Permalink
Change resource modification to webapi message group.
Browse files Browse the repository at this point in the history
  • Loading branch information
meng-tian committed Jan 19, 2017
1 parent 84a2abb commit 95f3dd2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions Model/DispatchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ class DispatchPlugin
private $response;
private $lockService;
private $resourceConnection;
private $modificationTimeRepo;
private $messageGroupRepository;
private $errorProcessor;

public function __construct(
Request $request,
Response $response,
LockService $lockService,
ResourceConnection $resourceConnection,
ResourceModificationTimeRepository $resourceTimestampRepository,
WebApiMessageGroupRepository $messageGroupRepository,
ErrorProcessor $errorProcessor
) {
$this->request = $request;
$this->response = $response;
$this->lockService = $lockService;
$this->resourceConnection = $resourceConnection;
$this->modificationTimeRepo = $resourceTimestampRepository;
$this->messageGroupRepository = $messageGroupRepository;
$this->errorProcessor = $errorProcessor;
}

Expand All @@ -57,11 +57,11 @@ public function aroundDispatch(
}

try {
$lastModification = $this->modificationTimeRepo->getLastModification($messageGroupId);
$lastModificationTime = $lastModification['timestamp'] ?? null;
$lastVersion = $lastModification['version'] ?? null;
$messageGroup = $this->messageGroupRepository->getMessageGroup($messageGroupId);
$lastTimestamp = $messageGroup['timestamp'] ?? null;
$lastVersion = $messageGroup['version'] ?? null;

if ($lastModificationTime !== null && $messageTimestamp < $lastModificationTime) {
if ($lastTimestamp !== null && $messageTimestamp < $lastTimestamp) {
$this->response->setStatusCode(412);
return $this->response;
}
Expand All @@ -79,11 +79,11 @@ public function aroundDispatch(
return $response;
}

$this->modificationTimeRepo->updateModificationTime(
$this->messageGroupRepository->updateModificationTime(
$messageGroupId,
$messageTimestamp,
$lastVersion,
$lastModificationTime
$lastTimestamp
);
$connection->commit();
return $response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Magento\Framework\Model\ResourceModel\Db\Context;

class ResourceModificationTimeRepository
class WebApiMessageGroupRepository
{
private $dbConnection;

Expand All @@ -13,38 +13,38 @@ public function __construct(Context $dbContext, $connectionName = null)
$this->dbConnection = $dbContext->getResources()->getConnection($connectionName);
}

public function getLastModification(string $identifier)
public function getMessageGroup(string $id)
{
$identifier = md5($identifier);
$id = md5($id);
$select = $this->dbConnection->select()
->from(['t' => $this->dbConnection->getTableName('webapi_resource_modification_log')], ['timestamp', 'version'])
->where('t.identifier = ?', $identifier);
->from(['t' => $this->dbConnection->getTableName('webapi_message_group')], ['timestamp', 'version'])
->where('t.id = ?', $id);
$result = $this->dbConnection->fetchAssoc($select);

return array_shift($result);
}

public function updateModificationTime(
string $identifier,
string $id,
string $modificationTime,
int $expectedVersion = null,
string $expectedModificationTime = null
) {
$identifier = md5($identifier);
$id = md5($id);

if ($expectedModificationTime !== null) {
$rowsAffected = $this->dbConnection->update(
$this->dbConnection->getTableName('webapi_resource_modification_log'),
$this->dbConnection->getTableName('webapi_message_group'),
[
'timestamp' => $modificationTime,
'version' => $expectedVersion + 1
],
['identifier = ?' => $identifier, 'timestamp = ?' => $expectedModificationTime, 'version = ?' => $expectedVersion]
['id = ?' => $id, 'timestamp = ?' => $expectedModificationTime, 'version = ?' => $expectedVersion]
);
} else {
$rowsAffected = $this->dbConnection->insert(
$this->dbConnection->getTableName('webapi_resource_modification_log'),
['identifier' => $identifier, 'timestamp' => $modificationTime, 'version' => 1]
$this->dbConnection->getTableName('webapi_message_group'),
['id' => $id, 'timestamp' => $modificationTime, 'version' => 1]
);
}

Expand Down
8 changes: 4 additions & 4 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()->newTable(
$installer->getTable('webapi_resource_modification_log')
$installer->getTable('webapi_message_group')
)->addColumn(
'identifier',
'id',
Table::TYPE_TEXT,
255,
['primary' => true, 'nullable' => false],
'Resource Identifier'
'Message Group ID'
)->addColumn(
'timestamp',
Table::TYPE_TEXT,
255,
['unsigned' => true, 'nullable' => false],
'Timestamp'
'Message Timestamp'
)->addColumn(
'version',
Table::TYPE_INTEGER,
Expand Down
6 changes: 3 additions & 3 deletions Test/Unit/Model/DispatchPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use SnowIO\IdempotentAPI\Model\DispatchPlugin;
use SnowIO\IdempotentAPI\Model\ResourceModificationTimeRepository;
use SnowIO\IdempotentAPI\Model\WebApiMessageGroupRepository;
use SnowIO\Lock\Api\LockService;
use Magento\Framework\Webapi\Response;
use \Magento\Framework\App\RequestInterface;
Expand All @@ -20,7 +20,7 @@

class DispatchPluginTest extends PHPUnit_Framework_TestCase
{
/** @var ResourceModificationTimeRepository | PHPUnit_Framework_MockObject_MockObject */
/** @var WebApiMessageGroupRepository | PHPUnit_Framework_MockObject_MockObject */
private $mockResourceTimestampRespository;

/** @var LockService | PHPUnit_Framework_MockObject_MockObject */
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct($name = null, array $data = array(), $dataName = '')

public function setUp()
{
$this->mockResourceTimestampRespository = $this->getMockBuilder(ResourceModificationTimeRepository::class)
$this->mockResourceTimestampRespository = $this->getMockBuilder(WebApiMessageGroupRepository::class)
->disableOriginalConstructor()->setMethods([
'getLastModificationTime',
'updateModificationTime'
Expand Down

0 comments on commit 95f3dd2

Please sign in to comment.