Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also update the cache when updating a setting entity directly
- Loading branch information
Showing
10 changed files
with
160 additions
and
57 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
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
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,43 @@ | ||
<?php | ||
|
||
namespace Craue\ConfigBundle\EventListener; | ||
|
||
use Craue\ConfigBundle\CacheAdapter\CacheAdapterInterface; | ||
use Craue\ConfigBundle\CacheAdapter\NullAdapter; | ||
use Craue\ConfigBundle\Entity\SettingInterface; | ||
use Doctrine\ORM\Event\LifecycleEventArgs; | ||
|
||
/** | ||
* @author Christian Raue <christian.raue@gmail.com> | ||
* @copyright 2011-2023 Christian Raue | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
class SettingUpdateListener { | ||
|
||
/** | ||
* @var CacheAdapterInterface | ||
*/ | ||
private $cache; | ||
|
||
public function __construct(?CacheAdapterInterface $cache = null) { | ||
$this->cache = $cache ?? new NullAdapter(); | ||
} | ||
|
||
// TODO add `PostUpdateEventArgs` type-hint as soon as doctrine/orm >= 2.13 is required | ||
public function postUpdate($eventArgs) : void { | ||
assert($eventArgs instanceof LifecycleEventArgs); | ||
|
||
$entity = $eventArgs->getObject(); | ||
|
||
if (!$entity instanceof SettingInterface) { | ||
return; | ||
} | ||
|
||
$this->updateCache($entity); | ||
} | ||
|
||
private function updateCache(SettingInterface $setting) : void { | ||
$this->cache->set($setting->getName(), $setting->getValue()); | ||
} | ||
|
||
} |
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Author: Christian Raue <christian.raue@gmail.com> | ||
Copyright: 2011-2023 Christian Raue | ||
License: http://opensource.org/licenses/mit-license.php MIT License | ||
--> | ||
<container | ||
xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Craue\ConfigBundle\EventListener\SettingUpdateListener" autowire="true"> | ||
<tag name="doctrine.event_listener" event="postUpdate" /> | ||
</service> | ||
</services> | ||
</container> |
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
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
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,68 @@ | ||
<?php | ||
|
||
namespace Craue\ConfigBundle\Tests\EventListener; | ||
|
||
use Craue\ConfigBundle\CacheAdapter\CacheAdapterInterface; | ||
use Craue\ConfigBundle\Entity\SettingInterface; | ||
use Craue\ConfigBundle\EventListener\SettingUpdateListener; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Event\LifecycleEventArgs; | ||
use Doctrine\ORM\Event\PostUpdateEventArgs; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @author Christian Raue <christian.raue@gmail.com> | ||
* @copyright 2011-2023 Christian Raue | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
class SettingUpdateListenerTest extends TestCase { | ||
|
||
public function testPostUpdate() : void { | ||
$cache = $this->createMock(CacheAdapterInterface::class); | ||
$listener = new SettingUpdateListener($cache); | ||
|
||
$setting = $this->getMockBuilder(SettingInterface::class)->onlyMethods(['getName', 'getValue'])->getMockForAbstractClass(); | ||
$name = 'name'; | ||
$newValue = 'new-value'; | ||
|
||
$setting->expects($this->once()) | ||
->method('getName') | ||
->willReturn($name) | ||
; | ||
|
||
$setting->expects($this->once()) | ||
->method('getValue') | ||
->willReturn($newValue) | ||
; | ||
|
||
$cache->expects($this->once()) | ||
->method('set') | ||
->with($name, $newValue) | ||
; | ||
|
||
$listener->postUpdate($this->getPostUpdateEventArgs($setting)); | ||
} | ||
|
||
public function testPostUpdate_entityIsNotSetting() : void { | ||
$cache = $this->createMock(CacheAdapterInterface::class); | ||
$listener = new SettingUpdateListener($cache); | ||
|
||
$cache->expects($this->never()) | ||
->method('set') | ||
; | ||
|
||
$listener->postUpdate($this->getPostUpdateEventArgs(new \stdClass())); | ||
} | ||
|
||
// TODO remove as soon as doctrine/orm >= 2.13 is required | ||
private function getPostUpdateEventArgs(object $object) : LifecycleEventArgs { | ||
$em = $this->createMock(EntityManagerInterface::class); | ||
|
||
if (!class_exists(PostUpdateEventArgs::class)) { | ||
return new LifecycleEventArgs($object, $em); | ||
} | ||
|
||
return new PostUpdateEventArgs($object, $em); | ||
} | ||
|
||
} |
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
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
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