Skip to content

Commit 9ee0d8c

Browse files
committed
Removed ObjectManager usage
1 parent 606c3e8 commit 9ee0d8c

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed
Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
@@ -9,6 +8,9 @@
98
use Magento\Framework\App\ObjectManager;
109
use Magento\Framework\Event\ObserverInterface;
1110

11+
/**
12+
* Class to flush cache by tags
13+
*/
1214
class FlushCacheByTags implements ObserverInterface
1315
{
1416
/**
@@ -40,15 +42,25 @@ class FlushCacheByTags implements ObserverInterface
4042
/**
4143
* @param \Magento\PageCache\Model\Config $config
4244
* @param \Magento\Framework\App\PageCache\Cache $cache
45+
* @param \Magento\Framework\App\Cache\Tag\Resolver $tagResolver
46+
* @param \Magento\PageCache\Model\Cache\Type $fullPageCache
4347
*/
44-
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache)
45-
{
48+
public function __construct(
49+
\Magento\PageCache\Model\Config $config,
50+
\Magento\Framework\App\PageCache\Cache $cache,
51+
\Magento\Framework\App\Cache\Tag\Resolver $tagResolver,
52+
\Magento\PageCache\Model\Cache\Type $fullPageCache
53+
) {
4654
$this->_config = $config;
4755
$this->_cache = $cache;
56+
$this->tagResolver = $tagResolver;
57+
$this->fullPageCache = $fullPageCache;
4858
}
4959

5060
/**
51-
* If Built-In caching is enabled it collects array of tags
61+
* Flushes cache
62+
*
63+
* If built-in caching is enabled it collects array of tags
5264
* of incoming object and asks to clean cache.
5365
*
5466
* @param \Magento\Framework\Event\Observer $observer
@@ -61,38 +73,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6173
if (!is_object($object)) {
6274
return;
6375
}
64-
$tags = $this->getTagResolver()->getTags($object);
76+
$tags = $this->tagResolver->getTags($object);
6577

6678
if (!empty($tags)) {
67-
$this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
79+
$this->fullPageCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
6880
}
6981
}
7082
}
71-
72-
/**
73-
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
74-
*
75-
*
76-
* @return \Magento\PageCache\Model\Cache\Type
77-
*/
78-
private function getCache()
79-
{
80-
if (!$this->fullPageCache) {
81-
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class);
82-
}
83-
return $this->fullPageCache;
84-
}
85-
86-
/**
87-
* @deprecated 100.1.2
88-
* @return \Magento\Framework\App\Cache\Tag\Resolver
89-
*/
90-
private function getTagResolver()
91-
{
92-
if ($this->tagResolver === null) {
93-
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
94-
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
95-
}
96-
return $this->tagResolver;
97-
}
9883
}

app/code/Magento/PageCache/Test/Unit/Observer/FlushCacheByTagsTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,17 @@ class FlushCacheByTagsTest extends \PHPUnit\Framework\TestCase
2929
*/
3030
protected function setUp()
3131
{
32-
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3332
$this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
3433
$this->_cacheMock = $this->createPartialMock(\Magento\Framework\App\PageCache\Cache::class, ['clean']);
34+
$this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
3535
$this->fullPageCacheMock = $this->createPartialMock(\Magento\PageCache\Model\Cache\Type::class, ['clean']);
3636

3737
$this->_model = new \Magento\PageCache\Observer\FlushCacheByTags(
3838
$this->_configMock,
39-
$this->_cacheMock
39+
$this->_cacheMock,
40+
$this->tagResolver,
41+
$this->fullPageCacheMock
4042
);
41-
42-
$this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
43-
44-
$helper->setBackwardCompatibleProperty($this->_model, 'tagResolver', $this->tagResolver);
45-
$reflection = new \ReflectionClass(\Magento\PageCache\Observer\FlushCacheByTags::class);
46-
$reflectionProperty = $reflection->getProperty('fullPageCache');
47-
$reflectionProperty->setAccessible(true);
48-
$reflectionProperty->setValue($this->_model, $this->fullPageCacheMock);
4943
}
5044

5145
/**

0 commit comments

Comments
 (0)