Skip to content

Commit

Permalink
Fix #17 - Implements IndentityInterface - Clean cache after save reta…
Browse files Browse the repository at this point in the history
…iler
  • Loading branch information
Maxime LECLERCQ committed Sep 6, 2018
1 parent 23a6650 commit b89ba3d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 11 deletions.
42 changes: 32 additions & 10 deletions Block/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
*/
namespace Smile\StoreLocator\Block;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Smile\Map\Api\MapInterface;
use Smile\Map\Model\AddressFormatter;
use Smile\Retailer\Api\Data\RetailerInterface;
use Smile\StoreLocator\Helper\Schedule;

/**
* Shop search block.
Expand All @@ -24,8 +25,10 @@
* @package Smile\StoreLocator
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class Search extends \Magento\Framework\View\Element\Template
class Search extends \Magento\Framework\View\Element\Template implements IdentityInterface
{
const CACHE_TAG = 'smile_store_locator_makers';

/**
* @var MapInterface
*/
Expand All @@ -42,7 +45,7 @@ class Search extends \Magento\Framework\View\Element\Template
private $storeLocatorHelper;

/**
* @var \Smile\Map\Model\AddressFormatter
* @var AddressFormatter
*/
private $addressFormatter;

Expand All @@ -61,26 +64,33 @@ class Search extends \Magento\Framework\View\Element\Template
*/
private $cacheInterface;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* Constructor.
*
* @param \Magento\Framework\View\Element\Template\Context $context Block context.
* @param \Smile\Map\Api\MapProviderInterface $mapProvider Map provider.
* @param \Smile\Retailer\Model\ResourceModel\Retailer\CollectionFactory $retailerCollectionFactory Retailer collection factory.
* @param \Smile\StoreLocator\Helper\Data $storeLocatorHelper Store locator helper.
* @param \Smile\Map\Model\AddressFormatter $addressFormatter Address formatter tool.
* @param AddressFormatter $addressFormatter Address formatter tool.
* @param \Smile\StoreLocator\Helper\Schedule $scheduleHelper Schedule Helper
* @param \Smile\StoreLocator\Model\Retailer\ScheduleManagement $scheduleManagement Schedule Management
* @param SerializerInterface $serializer JSON Serializer
* @param array $data Additional data.
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Smile\Map\Api\MapProviderInterface $mapProvider,
\Smile\Retailer\Model\ResourceModel\Retailer\CollectionFactory $retailerCollectionFactory,
\Smile\StoreLocator\Helper\Data $storeLocatorHelper,
\Smile\Map\Model\AddressFormatter $addressFormatter,
AddressFormatter $addressFormatter,
\Smile\StoreLocator\Helper\Schedule $scheduleHelper,
\Smile\StoreLocator\Model\Retailer\ScheduleManagement $scheduleManagement,
SerializerInterface $serializer,
$data = []
) {
parent::__construct($context, $data);
Expand All @@ -91,6 +101,7 @@ public function __construct(
$this->scheduleHelper = $scheduleHelper;
$this->scheduleManager = $scheduleManagement;
$this->cacheInterface = $context->getCache();
$this->serializer = $serializer;
$this->addData(
[
'cache_lifetime' => false,
Expand Down Expand Up @@ -124,7 +135,7 @@ public function getJsLayout()
$jsLayout['components']['store-locator-search']['children']['geocoder']['fulltextSearch'] = $this->escapeJsQuote($query);
}

return json_encode($jsLayout);
return $this->serializer->serialize($jsLayout);
}

/**
Expand Down Expand Up @@ -174,15 +185,25 @@ public function getMarkers()
}
\Magento\Framework\Profiler::stop('SmileStoreLocator:STORES');

$markers = json_encode($markers);
$markers = $this->serializer->serialize($markers);
$this->cacheInterface->save(
$markers,
$cacheKey,
$collection->getNewEmptyItem()->getCacheTags()
$this->getIdentities()
);
}

return json_decode($markers);
return $this->serializer->unserialize($markers);
}

/**
* Return unique ID(s) for each object in system
*
* @return array|string[]
*/
public function getIdentities()
{
return [self::CACHE_TAG];
}

/**
Expand All @@ -206,6 +227,7 @@ protected function _prepareLayout()
* Collection of displayed retailers.
*
* @return \Smile\Retailer\Model\ResourceModel\Retailer\Collection
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getRetailerCollection()
{
Expand All @@ -222,7 +244,7 @@ private function getRetailerCollection()
*
* @param \Smile\Retailer\Api\Data\RetailerInterface $retailer The store
*
* @return string
* @return array
*/
private function getSetStorePostData($retailer)
{
Expand Down
19 changes: 18 additions & 1 deletion Block/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
*/
namespace Smile\StoreLocator\Block;

use Magento\Framework\DataObject\IdentityInterface;

/**
* Retailer View Block
*
* @category Smile
* @package Smile\StoreLocator
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class View extends AbstractView
class View extends AbstractView implements IdentityInterface
{
/**
* @var \Smile\StoreLocator\Helper\Data
Expand All @@ -44,6 +46,21 @@ public function __construct(
$this->storeLocatorHelper = $storeLocatorHelper;
}

/**
* Return unique ID(s) for each object in system
*
* @return string[]
*/
public function getIdentities()
{
$identities = [];
if ($this->getRetailer()) {
$identities = $this->getRetailer()->getIdentities();
}

return $identities;
}

/**
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* {@inheritdoc}
Expand Down
60 changes: 60 additions & 0 deletions Observer/CleanStoreLocatorCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\StoreLocator
* @author Maxime Leclercq <maxime.leclercq@smile.fr>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\StoreLocator\Observer;

use Magento\PageCache\Model\Cache\Type as Cache;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Smile\Seller\Api\Data\SellerInterface;
use Smile\StoreLocator\Block\Search;

/**
* Clean store locator cache observer.
*
* @category Smile
* @package Smile\StoreLocator
*/
class CleanStoreLocatorCache implements ObserverInterface
{
/**
* @var Cache
*/
private $cache;

/**
* CleanStoreLocatorCache constructor.
*
* @param Cache $cache Cache.
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}

/**
* Clean store locator marker cache when save a seller.
*
* @param Observer $observer
*
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
public function execute(Observer $observer)
{
/** @var SellerInterface $seller */
$seller = $observer->getEvent()->getSeller();
if ($seller->hasDataChanges()) {
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [Search::CACHE_TAG]);
}
}
}
23 changes: 23 additions & 0 deletions etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!--
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\StoreLocator
* @author Maxime Leclercq <maxime.leclercq@smile.fr>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="smile_seller_save_after">
<observer name="smile_store_locator_clear_cache" instance="Smile\StoreLocator\Observer\CleanStoreLocatorCache" shared="false" />
</event>
<event name="smile_seller_delete_after">
<observer name="smile_store_locator_clear_cache" instance="Smile\StoreLocator\Observer\CleanStoreLocatorCache" shared="false" />
</event>
</config>

0 comments on commit b89ba3d

Please sign in to comment.