This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from DivanteLtd/feature/support-for-agreements
Added support for agreements
- Loading branch information
Showing
15 changed files
with
428 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/module-vsbridge-indexer-agreement/Index/Mapping/Agreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Index\Mapping; | ||
|
||
use Divante\VsbridgeIndexerCore\Api\Mapping\FieldInterface; | ||
use Divante\VsbridgeIndexerCore\Api\MappingInterface; | ||
|
||
class Agreement implements MappingInterface | ||
{ | ||
/** | ||
* @return array|mixed|null | ||
*/ | ||
public function getMappingProperties() | ||
{ | ||
$properties = [ | ||
'id' => ['type' => FieldInterface::TYPE_LONG], | ||
'content' => ['type' => FieldInterface::TYPE_TEXT], | ||
'active' => ['type' => FieldInterface::TYPE_BOOLEAN], | ||
'title' => ['type' => FieldInterface::TYPE_TEXT], | ||
'content_height' => ['type' => FieldInterface::TYPE_TEXT], | ||
'checkbox_text' => ['type' => FieldInterface::TYPE_TEXT], | ||
'is_html' => ['type' => FieldInterface::TYPE_BOOLEAN], | ||
'mode' => ['type' => FieldInterface::TYPE_INTEGER], | ||
]; | ||
|
||
return ['properties' => $properties]; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/module-vsbridge-indexer-agreement/Model/Indexer/Action/Agreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Model\Indexer\Action; | ||
|
||
|
||
use Divante\VsbridgeIndexerAgreement\Model\ResourceModel\Agreement as AgreementResource; | ||
|
||
class Agreement | ||
{ | ||
/** | ||
* @var AgreementResource | ||
*/ | ||
private $resourceModel; | ||
|
||
/** | ||
* Agreement constructor. | ||
* | ||
* @param AgreementResource $agreementResource | ||
*/ | ||
public function __construct(AgreementResource $agreementResource) | ||
{ | ||
$this->resourceModel = $agreementResource; | ||
} | ||
|
||
/** | ||
* @param int $storeId | ||
* @param array $agreementIds | ||
* | ||
* @return \Traversable | ||
*/ | ||
public function rebuild($storeId = 1, array $agreementIds = []) | ||
{ | ||
$lastAgreementId = 0; | ||
|
||
do { | ||
$agreements = $this->resourceModel->loadAgreements($storeId, $agreementIds, $lastAgreementId); | ||
|
||
foreach ($agreements as $agreement) { | ||
$lastAgreementId = (int) $agreement['agreement_id']; | ||
$agreement['id'] = $lastAgreementId; | ||
$agreement['title'] = $agreement['name']; | ||
$agreement['content'] = (string) $agreement['content']; | ||
$agreement['active'] = (bool) $agreement['is_active']; | ||
$agreement['is_html'] = (bool) $agreement['is_html']; | ||
$agreement['mode'] = (int) $agreement['mode']; | ||
|
||
unset($agreement['name'], $agreement['is_active'], $agreement['agreement_id']); | ||
|
||
yield $lastAgreementId => $agreement; | ||
} | ||
} while (!empty($agreements)); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/module-vsbridge-indexer-agreement/Model/Indexer/Agreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Model\Indexer; | ||
|
||
use Magento\Framework\Indexer\ActionInterface; | ||
use Magento\Framework\Mview\ActionInterface as MviewActionInterfaceAlias; | ||
use Divante\VsbridgeIndexerCore\Indexer\StoreManager; | ||
use Divante\VsbridgeIndexerCore\Indexer\GenericIndexerHandler; | ||
use Divante\VsbridgeIndexerAgreement\Model\Indexer\Action\Agreement as AgreementAction; | ||
|
||
class Agreement implements ActionInterface, MviewActionInterfaceAlias | ||
{ | ||
/** | ||
* @var GenericIndexerHandler | ||
*/ | ||
private $indexHandler; | ||
|
||
/** | ||
* @var AgreementAction | ||
*/ | ||
private $agreementAction; | ||
|
||
/** | ||
* @var StoreManager | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* Agreement constructor. | ||
* | ||
* @param GenericIndexerHandler $indexerHandler | ||
* @param StoreManager $storeManager | ||
* @param AgreementAction $action | ||
*/ | ||
public function __construct( | ||
GenericIndexerHandler $indexerHandler, | ||
StoreManager $storeManager, | ||
AgreementAction $action | ||
) { | ||
$this->indexHandler = $indexerHandler; | ||
$this->agreementAction = $action; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute($ids) | ||
{ | ||
$stores = $this->storeManager->getStores(); | ||
|
||
foreach ($stores as $store) { | ||
$this->indexHandler->saveIndex($this->agreementAction->rebuild($store->getId(), $ids), $store); | ||
$this->indexHandler->cleanUpByTransactionKey($store, $ids); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function executeFull() | ||
{ | ||
$stores = $this->storeManager->getStores(); | ||
|
||
foreach ($stores as $store) { | ||
$this->indexHandler->saveIndex($this->agreementAction->rebuild($store->getId()), $store); | ||
$this->indexHandler->cleanUpByTransactionKey($store); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function executeList(array $ids) | ||
{ | ||
$this->execute($ids); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function executeRow($id) | ||
{ | ||
$this->execute([$id]); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/module-vsbridge-indexer-agreement/Model/Indexer/AgreementProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Model\Indexer; | ||
|
||
use Magento\Framework\Indexer\AbstractProcessor; | ||
|
||
class AgreementProcessor extends AbstractProcessor | ||
{ | ||
/** | ||
* Indexer ID | ||
*/ | ||
const INDEXER_ID = 'vsbridge_agreement_indexer'; | ||
} |
83 changes: 83 additions & 0 deletions
83
src/module-vsbridge-indexer-agreement/Model/ResourceModel/Agreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Model\ResourceModel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\Store\Model\Store; | ||
|
||
class Agreement | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @var MetadataPool | ||
*/ | ||
private $metaDataPool; | ||
|
||
/** | ||
* Agreement constructor. | ||
* | ||
* @param MetadataPool $metadataPool | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
MetadataPool $metadataPool, | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resource = $resourceConnection; | ||
$this->metaDataPool = $metadataPool; | ||
} | ||
|
||
/** | ||
* @param int $storeId | ||
* @param array $agreementIds | ||
* @param int $fromId | ||
* @param int $limit | ||
* | ||
* @return array | ||
*/ | ||
public function loadAgreements($storeId = 1, array $agreementIds = [], $fromId = 0, $limit = 1000) | ||
{ | ||
$select = $this->getConnection()->select()->from(['agreement' => 'checkout_agreement']); | ||
$select->join( | ||
['store_table' => $this->resource->getTableName('checkout_agreement_store')], | ||
"agreement.agreement_id = store_table.agreement_id", | ||
[] | ||
)->group("agreement.agreement_id"); | ||
|
||
$select->where( | ||
'store_table.store_id IN (?)', | ||
[ | ||
Store::DEFAULT_STORE_ID, | ||
$storeId, | ||
] | ||
); | ||
|
||
if (!empty($agreementIds)) { | ||
$select->where('agreement.agreement_id IN (?)', $agreementIds); | ||
} | ||
|
||
$select->where('is_active = ?', 1); | ||
$select->where('agreement.agreement_id > ?', $fromId) | ||
->limit($limit) | ||
->order('agreement.agreement_id'); | ||
|
||
return $this->getConnection()->fetchAll($select); | ||
} | ||
|
||
/** | ||
* @return \Magento\Framework\DB\Adapter\AdapterInterface | ||
*/ | ||
private function getConnection() | ||
{ | ||
return $this->resource->getConnection(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/module-vsbridge-indexer-agreement/Plugin/Indexer/Agreement/Save/UpdateAgreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Copyright Divante Sp. z o.o. | ||
* See LICENSE_DIVANTE.txt for license details. | ||
*/ | ||
|
||
namespace Divante\VsbridgeIndexerAgreement\Plugin\Indexer\Agreement\Save; | ||
|
||
use Divante\VsbridgeIndexerAgreement\Model\Indexer\AgreementProcessor; | ||
use Magento\CheckoutAgreements\Model\Agreement; | ||
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement as ResourceModelAgreement; | ||
|
||
class UpdateAgreement | ||
{ | ||
|
||
/** | ||
* @var AgreementProcessor | ||
*/ | ||
private $agreementProcessor; | ||
|
||
/** | ||
* @var ResourceModelAgreement | ||
*/ | ||
private $resourceModelAgreement; | ||
|
||
/** | ||
* Save constructor. | ||
* | ||
* @param AgreementProcessor $agreementProcessor | ||
* @param ResourceModelAgreement $resourceModelAgreement | ||
*/ | ||
public function __construct(AgreementProcessor $agreementProcessor, ResourceModelAgreement $resourceModelAgreement) | ||
{ | ||
$this->agreementProcessor = $agreementProcessor; | ||
$this->resourceModelAgreement = $resourceModelAgreement; | ||
} | ||
|
||
/** | ||
* @param Agreement $agreement | ||
* @param Agreement $result | ||
* | ||
* @return Agreement | ||
*/ | ||
public function afterAfterSave(Agreement $agreement, Agreement $result): Agreement | ||
{ | ||
$this->resourceModelAgreement->addCommitCallback(function () use ($agreement) { | ||
$this->agreementProcessor->reindexRow($agreement->getId()); | ||
}); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param Agreement $agreement | ||
* @param Agreement $result | ||
* | ||
* @return Agreement | ||
*/ | ||
public function afterAfterDeleteCommit(Agreement $agreement, Agreement $result): Agreement | ||
{ | ||
$this->agreementProcessor->reindexRow($agreement->getId()); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "divante/module-vsbridge-indexer-agreement", | ||
"type": "magento2-module", | ||
"authors":[ | ||
{ | ||
"name":"Kamil", | ||
"email":"kpaluch@divante.pl" | ||
} | ||
], | ||
"require": { | ||
"elasticsearch/elasticsearch": "~5.0" | ||
}, | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Divante\\VsbridgeIndexerCore\\": "" | ||
} | ||
} | ||
} |
Oops, something went wrong.