Skip to content

Commit a4d60f4

Browse files
committed
MAGETWO-49488: Refactor ResourceModel\Category children_count processing
1 parent b87e606 commit a4d60f4

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class Category extends AbstractResource
7272
* @var EntityManager
7373
*/
7474
protected $entityManager;
75+
/**
76+
* @var Category\AggregateCount
77+
*/
78+
private $aggregateCount;
7579

7680
/**
7781
* Category constructor.
@@ -82,6 +86,7 @@ class Category extends AbstractResource
8286
* @param Category\TreeFactory $categoryTreeFactory
8387
* @param Category\CollectionFactory $categoryCollectionFactory
8488
* @param EntityManager $entityManager
89+
* @param Category\AggregateCount $aggregateCount
8590
* @param array $data
8691
*/
8792
public function __construct(
@@ -92,6 +97,7 @@ public function __construct(
9297
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
9398
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
9499
EntityManager $entityManager,
100+
Category\AggregateCount $aggregateCount,
95101
$data = []
96102
) {
97103
parent::__construct(
@@ -104,8 +110,8 @@ public function __construct(
104110
$this->_categoryCollectionFactory = $categoryCollectionFactory;
105111
$this->_eventManager = $eventManager;
106112
$this->entityManager = $entityManager;
107-
108113
$this->connectionName = 'catalog';
114+
$this->aggregateCount = $aggregateCount;
109115
}
110116

111117
/**
@@ -173,6 +179,21 @@ protected function _getTree()
173179
return $this->_tree;
174180
}
175181

182+
/**
183+
* Process category data before delete
184+
* update children count for parent category
185+
* delete child categories
186+
*
187+
* @param \Magento\Framework\DataObject $object
188+
* @return $this
189+
*/
190+
protected function _beforeDelete(\Magento\Framework\DataObject $object)
191+
{
192+
parent::_beforeDelete($object);
193+
$this->aggregateCount->processDelete($object);
194+
$this->deleteChildren($object);
195+
}
196+
176197
/**
177198
* Delete children categories of specific category
178199
*

app/code/Magento/Catalog/Observer/CatalogCategoryRecalculateChildrenCount.php renamed to app/code/Magento/Catalog/Model/ResourceModel/Category/AggregateCount.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
<?php
22
/**
3-
* Copyright © 2015 Magento. All rights reserved.
3+
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Catalog\Observer;
6+
namespace Magento\Catalog\Model\ResourceModel\Category;
77

8-
use Magento\Framework\Event\ObserverInterface;
9-
use Magento\Framework\Event\Observer;
108
use Magento\Catalog\Model\Category;
119

12-
class CatalogCategoryRecalculateChildrenCount implements ObserverInterface
10+
/**
11+
* Class AggregateCount
12+
*/
13+
class AggregateCount
1314
{
1415
/**
15-
* Recalculate children count for category
16-
*
17-
* @param Observer $observer
16+
* @param Category $category
1817
* @return void
1918
*/
20-
public function execute(Observer $observer)
19+
public function processDelete(Category $category)
2120
{
22-
/** @var \Magento\Catalog\Model\Category $category */
23-
$category = $observer->getEvent()->getData('category');
24-
2521
/** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
2622
$resourceModel = $category->getResource();
2723
/**

app/code/Magento/Catalog/Observer/CatalogCategoryDeleteChildren.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/code/Magento/Catalog/etc/adminhtml/events.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@
99
<event name="cms_wysiwyg_images_static_urls_allowed">
1010
<observer name="catalog_wysiwyg" instance="Magento\Catalog\Observer\CatalogCheckIsUsingStaticUrlsAllowedObserver" />
1111
</event>
12-
<event name="catalog_category_delete_before">
13-
<observer name="catalog_category_recalculate_children_count" instance="Magento\Catalog\Observer\CatalogCategoryRecalculateChildrenCount" />
14-
<observer name="catalog_category_delete_children" instance="Magento\Catalog\Observer\CatalogCategoryDeleteChildren" />
15-
</event>
1612
</config>

0 commit comments

Comments
 (0)