-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds subscriber to use blameable and timestampable interfaces
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
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
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); | ||
} | ||
} |
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,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()); | ||
} | ||
} | ||
} |
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