Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Added support for agreements
Browse files Browse the repository at this point in the history
  • Loading branch information
camilloop committed Jun 4, 2020
1 parent 8451a85 commit 1939614
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added
- Add support for agreements ([#286](https://github.com/DivanteLtd/magento2-vsbridge-indexer/pull/286))

## [1.17.0] (2020.05.20)

### Added
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"autoload": {
"files": [
"src/module-vsbridge-indexer-agreement/registration.php",
"src/module-vsbridge-indexer-core/registration.php",
"src/module-vsbridge-indexer-catalog/registration.php",
"src/module-vsbridge-indexer-cms/registration.php",
Expand All @@ -42,6 +43,7 @@
"src/module-vsbridge-downloadable/registration.php"
],
"psr-4": {
"Divante\\VsbridgeIndexerAgreement\\": "src/module-vsbridge-indexer-agreement",
"Divante\\VsbridgeIndexerCore\\": "src/module-vsbridge-indexer-core",
"Divante\\VsbridgeIndexerCatalog\\": "src/module-vsbridge-indexer-catalog",
"Divante\\VsbridgeIndexerCms\\": "src/module-vsbridge-indexer-cms",
Expand Down
32 changes: 32 additions & 0 deletions src/module-vsbridge-indexer-agreement/Index/Mapping/Agreement.php
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],
'name' => ['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];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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['content'] = (string) $agreement['content'];
$agreement['active'] = (bool)$agreement['is_active'];
$agreement['is_html'] = (bool)$agreement['is_html'];
$agreement['mode'] = (int)$agreement['mode'];

unset($agreement['is_active'], $agreement['agreement_id']);

yield $lastAgreementId => $agreement;
}
} while (!empty($agreements));
}
}
90 changes: 90 additions & 0 deletions src/module-vsbridge-indexer-agreement/Model/Indexer/Agreement.php
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]);
}
}
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';
}
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();
}
}
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;
}
}
21 changes: 21 additions & 0 deletions src/module-vsbridge-indexer-agreement/composer.json
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\\": ""
}
}
}
Loading

0 comments on commit 1939614

Please sign in to comment.