Skip to content

Commit

Permalink
EZP-28747: HTTP Cache purge when UserService\{CreateUser,CreateUserGr…
Browse files Browse the repository at this point in the history
…oup,UpdateUser,UpdateUserGroup}Signal is emited. (#50)
  • Loading branch information
adamwojs authored and andrerom committed Feb 8, 2018
1 parent 258d0aa commit 85c82d6
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 115 deletions.
26 changes: 24 additions & 2 deletions src/Resources/config/slot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ services:
abstract: true
arguments: ["@ezplatform.http_cache.purge_client"]

ezplatform.http_cache.signalslot.abstract_publish:
abstract: true
arguments: ["@ezplatform.http_cache.purge_client", "@ezpublish.spi.persistence.cache.locationHandler"]

ezplatform.http_cache.signalslot.assign_section:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\AssignSectionSlot
parent: ezplatform.http_cache.signalslot.abstract_content
Expand Down Expand Up @@ -60,7 +64,7 @@ services:

ezplatform.http_cache.signalslot.publish_version:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\PublishVersionSlot
arguments: ["@ezplatform.http_cache.purge_client", "@ezpublish.spi.persistence.cache.locationHandler"]
parent: ezplatform.http_cache.signalslot.abstract_publish
tags:
- { name: ezpublish.api.slot, signal: ContentService\PublishVersionSignal }

Expand Down Expand Up @@ -88,12 +92,30 @@ services:
tags:
- { name: ezpublish.api.slot, signal: LocationService\UpdateLocationSignal }

ezplatform.http_cache.signalslot.create_user:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\CreateUserSlot
parent: ezplatform.http_cache.signalslot.abstract_publish
tags:
- { name: ezpublish.api.slot, signal: UserService\CreateUserSignal }

ezplatform.http_cache.signalslot.update_user:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\UpdateUserSlot
parent: ezplatform.http_cache.signalslot.abstract_content
parent: ezplatform.http_cache.signalslot.abstract_publish
tags:
- { name: ezpublish.api.slot, signal: UserService\UpdateUserSignal }

ezplatform.http_cache.signalslot.create_user_group:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\CreateUserGroupSlot
parent: ezplatform.http_cache.signalslot.abstract_publish
tags:
- { name: ezpublish.api.slot, signal: UserService\CreateUserGroupSignal }

ezplatform.http_cache.signalslot.update_user_group:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\UpdateUserGroupSlot
parent: ezplatform.http_cache.signalslot.abstract_publish
tags:
- { name: ezpublish.api.slot, signal: UserService\UpdateUserGroupSignal }

ezplatform.http_cache.signalslot.assign_user_to_user_group:
class: EzSystems\PlatformHttpCacheBundle\SignalSlot\AssignUserToUserGroupSlot
parent: ezplatform.http_cache.signalslot.abstract_content
Expand Down
2 changes: 1 addition & 1 deletion src/SignalSlot/AbstractContentSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function generateTags(Signal $signal)
$tags = [];

if (isset($signal->contentId)) {
// self in all forms (also withouth locations)
// self in all forms (also without locations)
$tags[] = 'content-' . $signal->contentId;
// reverse relations
$tags[] = 'relation-' . $signal->contentId;
Expand Down
64 changes: 64 additions & 0 deletions src/SignalSlot/AbstractPublishSlot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\SignalSlot;

use eZ\Publish\Core\SignalSlot\Signal;
use eZ\Publish\SPI\Persistence\Content\Location\Handler;
use EzSystems\PlatformHttpCacheBundle\PurgeClient\PurgeClientInterface;

abstract class AbstractPublishSlot extends AbstractContentSlot
{
/**
* @var \eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
private $locationHandler;

/**
* @param \EzSystems\PlatformHttpCacheBundle\PurgeClient\PurgeClientInterface $purgeClient
* @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $spiLocationHandler
*/
public function __construct(PurgeClientInterface $purgeClient, Handler $spiLocationHandler)
{
parent::__construct($purgeClient);
$this->locationHandler = $spiLocationHandler;
}

/**
* Extracts content id from signal.
*
* @param Signal $signal
* @return mixed
*/
abstract protected function getContentId(Signal $signal);

protected function generateTags(Signal $signal)
{
$contentId = $this->getContentId($signal);

$tags = [
// self in all forms (also without locations)
'content-' . $contentId,
// reverse relations
'relation-' . $contentId,
];

foreach ($this->locationHandler->loadLocationsByContent($contentId) as $location) {
// self
$tags[] = 'location-' . $location->id;
// children
$tags[] = 'parent-' . $location->id;
// parent
$tags[] = 'location-' . $location->parentId;
// siblings
$tags[] = 'parent-' . $location->parentId;
}

return $tags;
}
}
27 changes: 27 additions & 0 deletions src/SignalSlot/CreateUserGroupSlot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\SignalSlot;

use eZ\Publish\Core\SignalSlot\Signal;

/**
* A slot handling CreateUserGroupSlot.
*/
class CreateUserGroupSlot extends AbstractPublishSlot
{
protected function supports(Signal $signal)
{
return $signal instanceof Signal\UserService\CreateUserGroupSignal;
}

protected function getContentId(Signal $signal)
{
return $signal->userGroupId;
}
}
27 changes: 27 additions & 0 deletions src/SignalSlot/CreateUserSlot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\SignalSlot;

use eZ\Publish\Core\SignalSlot\Signal;

/**
* A slot handling CreateUserSlot.
*/
class CreateUserSlot extends AbstractPublishSlot
{
protected function supports(Signal $signal)
{
return $signal instanceof Signal\UserService\CreateUserSignal;
}

protected function getContentId(Signal $signal)
{
return $signal->userId;
}
}
48 changes: 5 additions & 43 deletions src/SignalSlot/PublishVersionSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,20 @@
*/
namespace EzSystems\PlatformHttpCacheBundle\SignalSlot;

use EzSystems\PlatformHttpCacheBundle\PurgeClient\PurgeClientInterface;
use eZ\Publish\Core\SignalSlot\Signal;
use eZ\Publish\SPI\Persistence\Content\Location\Handler;

/**
* A slot handling PublishVersionSignal.
*/
class PublishVersionSlot extends AbstractContentSlot
class PublishVersionSlot extends AbstractPublishSlot
{
/**
* @var \eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
private $locationHandler;

/**
* @param \EzSystems\PlatformHttpCacheBundle\PurgeClient\PurgeClientInterface $purgeClient
* @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $spiLocationHandler
*/
public function __construct(PurgeClientInterface $purgeClient, Handler $spiLocationHandler)
{
parent::__construct($purgeClient);
$this->locationHandler = $spiLocationHandler;
}

/**
* Default provides tags to clear content, relation, location, parent and sibling cache.
*
* Overload for tree operations where you also need to clear whole path.
*
* @param \eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal $signal
*
* @return array
*/
protected function generateTags(Signal $signal)
protected function supports(Signal $signal)
{
$tags = parent::generateTags($signal);
foreach ($this->locationHandler->loadLocationsByContent($signal->contentId) as $location) {
// self
$tags[] = 'location-' . $location->id;
// children
$tags[] = 'parent-' . $location->id;
// parent
$tags[] = 'location-' . $location->parentId;
// siblings
$tags[] = 'parent-' . $location->parentId;
}

return $tags;
return $signal instanceof Signal\ContentService\PublishVersionSignal;
}

protected function supports(Signal $signal)
protected function getContentId(Signal $signal)
{
return $signal instanceof Signal\ContentService\PublishVersionSignal;
return $signal->contentId;
}
}
27 changes: 27 additions & 0 deletions src/SignalSlot/UpdateUserGroupSlot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\SignalSlot;

use eZ\Publish\Core\SignalSlot\Signal;

/**
* A slot handling UpdateUserGroupSignal.
*/
class UpdateUserGroupSlot extends AbstractPublishSlot
{
protected function supports(Signal $signal)
{
return $signal instanceof Signal\UserService\UpdateUserGroupSignal;
}

protected function getContentId(Signal $signal)
{
return $signal->userGroupId;
}
}
13 changes: 5 additions & 8 deletions src/SignalSlot/UpdateUserSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
/**
* A slot handling UpdateUserSignal.
*/
class UpdateUserSlot extends AbstractContentSlot
class UpdateUserSlot extends AbstractPublishSlot
{
/**
* @param \eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserSignal $signal
*/
protected function generateTags(Signal $signal)
protected function supports(Signal $signal)
{
return ['content-' . $signal->userId];
return $signal instanceof Signal\UserService\UpdateUserSignal;
}

protected function supports(Signal $signal)
protected function getContentId(Signal $signal)
{
return $signal instanceof Signal\UserService\UpdateUserSignal;
return $signal->userId;
}
}
67 changes: 67 additions & 0 deletions tests/SignalSlot/AbstractPublishSlotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\Tests\SignalSlot;

use eZ\Publish\SPI\Persistence\Content\Location;
use eZ\Publish\SPI\Persistence\Content\Location\Handler;

abstract class AbstractPublishSlotTest extends AbstractContentSlotTest
{
protected $locationId = 45;
protected $parentLocationId = 32;

/** @var \eZ\Publish\SPI\Persistence\Content\Location\Handler|\PHPUnit_Framework_MockObject_MockObject */
protected $spiLocationHandlerMock;

protected function createSlot()
{
$class = $this->getSlotClass();
if ($this->spiLocationHandlerMock === null) {
$this->spiLocationHandlerMock = $this->createMock(Handler::class);
}

return new $class($this->purgeClientMock, $this->spiLocationHandlerMock);
}

/**
* @dataProvider getUnreceivedSignals
*/
public function testDoesNotReceiveOtherSignals($signal)
{
$this->purgeClientMock->expects($this->never())->method('purge');
$this->purgeClientMock->expects($this->never())->method('purgeAll');

$this->spiLocationHandlerMock->expects($this->never())->method('loadLocationsByContent');

$this->slot->receive($signal);
}

/**
* @dataProvider getReceivedSignals
*/
public function testReceivePurgesCacheForTags($signal)
{
$this->spiLocationHandlerMock
->expects($this->once())
->method('loadLocationsByContent')
->with($this->contentId)
->willReturn(
[
new Location([
'id' => $this->locationId,
'parentId' => $this->parentLocationId,
]),
]
);

$this->purgeClientMock->expects($this->once())->method('purge')->with($this->generateTags());
$this->purgeClientMock->expects($this->never())->method('purgeAll');
parent::receive($signal);
}
}
32 changes: 32 additions & 0 deletions tests/SignalSlot/CreateUserGroupSlotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\Tests\SignalSlot;

use eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserGroupSignal;
use EzSystems\PlatformHttpCacheBundle\SignalSlot\CreateUserGroupSlot;

class CreateUserGroupSlotTest extends AbstractPublishSlotTest
{
public function createSignal()
{
return new CreateUserGroupSignal([
'userGroupId' => $this->contentId,
]);
}

public function getReceivedSignalClasses()
{
return [CreateUserGroupSignal::class];
}

public function getSlotClass()
{
return CreateUserGroupSlot::class;
}
}
Loading

0 comments on commit 85c82d6

Please sign in to comment.