-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGE-791 Add alt registry for current category
- Loading branch information
Showing
3 changed files
with
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Observer; | ||
|
||
use Algolia\AlgoliaSearch\Registry\CurrentCategory; | ||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
class RegisterCurrentCategoryObserver implements ObserverInterface | ||
{ | ||
/** @var CurrentCategory */ | ||
private CurrentCategory $currentCategory; | ||
|
||
public function __construct(CurrentCategory $currentCategory) { | ||
$this->currentCategory = $currentCategory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var CategoryInterface */ | ||
$category = $observer->getEvent()->getData('category'); | ||
$this->currentCategory->set($category); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Registry; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Catalog\Api\Data\CategoryInterfaceFactory; | ||
|
||
class CurrentCategory | ||
{ | ||
private CategoryInterface $category; | ||
private CategoryRepositoryInterface $categoryRepository; | ||
private CategoryInterfaceFactory $categoryFactory; | ||
|
||
public function __construct( | ||
CategoryRepositoryInterface $categoryRepository, | ||
CategoryInterfaceFactory $categoryFactory | ||
) | ||
{ | ||
$this->categoryRepository = $categoryRepository; | ||
$this->categoryFactory = $categoryFactory; | ||
} | ||
|
||
public function set(CategoryInterface $category): void { | ||
$this->category = $category; | ||
} | ||
|
||
public function get(): CategoryInterface { | ||
return $this->category ?? $this->categoryFactory->create(); | ||
} | ||
} |
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