Skip to content

Commit

Permalink
adds subscriber to use blameable and timestampable interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed Sep 12, 2018
1 parent 39ffdb5 commit 6c9ac94
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"require": {
"php": "^7.2",
"symfony/framework-bundle": "~2.3||~3||~4",
"symfony/security": "~2.3||~3||~4",
"chgst/common":"@dev",
"chgst/chgst":"@dev"
},
Expand Down
55 changes: 55 additions & 0 deletions spec/EventListener/EventAppendSubscriberSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace spec\Changeset\ChangesetBundle\EventListener;

use Changeset\ChangesetBundle\EventListener\EventAppendSubscriber;
use Changeset\Common\BlameableInterface;
use Changeset\Common\TimestampableInterface;
use Changeset\Event\EventInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class EventAppendSubscriberSpec extends ObjectBehavior
{
function let(TokenStorageInterface $storage, TokenInterface $token)
{
$storage->getToken()->willReturn($token);

$this->beConstructedWith($storage);
}

function it_is_initializable()
{
$this->shouldHaveType(EventAppendSubscriber::class);
$this->shouldHaveType(EventSubscriberInterface::class);
}

function it_implements_required_getSubscribedEvents_method()
{
$this->getSubscribedEvents()->shouldHaveCount(1);
}

function it_can_setBlame_on_event(GenericEvent $event, BlameableInterface $subject, TokenInterface $token)
{
$event->getSubject()->willReturn($subject);

$subject->setCreatedBy(Argument::any())->shouldBeCalled();

$token->getUsername()->willReturn('some username');

$this->setBlame($event);
}

function it_can_setTimestamp_on_the_event(GenericEvent $event, TimestampableInterface $subject)
{
$event->getSubject()->willReturn($subject);

$subject->setCreatedAt(Argument::any())->shouldBeCalled();

$this->setTimestamp($event);
}
}
51 changes: 51 additions & 0 deletions src/EventListener/EventAppendSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Changeset\ChangesetBundle\EventListener;

use Changeset\Common\BlameableInterface;
use Changeset\Common\TimestampableInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class EventAppendSubscriber implements EventSubscriberInterface
{
/** @var string */
protected $username;

/**
* EventAppendSubscriber constructor.
*
* @param TokenStorageInterface $storage
*/
public function __construct(TokenStorageInterface $storage)
{
if ($storage->getToken() && $storage->getToken()->getUsername())
{
$this->username = $storage->getToken()->getUsername();
}
}

public static function getSubscribedEvents()
{
return [
'changeset.command.handled' => [ [ 'setBlame', 0 ], [ 'setTimestamp', 0 ] ]
];
}

public function setBlame(GenericEvent $event)
{
if ($event->getSubject() instanceof BlameableInterface)
{
$event->getSubject()->setCreatedBy($this->username);
}
}

public function setTimestamp(GenericEvent $event)
{
if ($event->getSubject() instanceof TimestampableInterface)
{
$event->getSubject()->setCreatedAt(new \DateTime());
}
}
}
8 changes: 7 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
Changeset\Command\HandlerInterface:
public: true
class: Changeset\Command\Handler
arguments: [ '@event_dispatcher']
arguments: [ '@event_dispatcher', '@changeset.event_repository']

Changeset\Communication\CommandBusInterface:
public: true
Expand All @@ -16,3 +16,9 @@ services:
autowire: true
class: Changeset\Communication\InMemoryEventBus

Changeset\ChangesetBundle\EventListener\EventAppendSubscriber:
public: true
autowire: true
class: Changeset\ChangesetBundle\EventListener\EventAppendSubscriber
tags:
- { name: kernel.event_subscriber }

0 comments on commit 6c9ac94

Please sign in to comment.