Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '8.1', '8.2', '8.3' ]
php-versions: [ '8.1', '8.2', '8.3', '8.4' ]

steps:
- name: Checkout
Expand Down
33 changes: 23 additions & 10 deletions Attribute/SubscribeDoctrineEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,35 @@
*
* @author Roni Saha <[email protected]>
*/

#[\Attribute(\Attribute::TARGET_CLASS)]
/* @final */ class SubscribeDoctrineEvents
/* @final */
class SubscribeDoctrineEvents
{
public $events = array();
public array $events = [];

public function __construct(array $values)
public function __construct(array|string $values)
{
if (isset($values['value'])) {
$values['events'] = $values['value'];
}
if (!isset($values['events'])) {
$validValues = [
'created',
'updated',
];
$valueValueStr = [
'created,updated',
'created, updated',
'updated,created',
'updated, created',
];
if (!empty($values) && is_string($values) && !in_array($values, $valueValueStr)) {
return;
}

$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));
if (!empty($values) && is_array($values)) {
foreach ($values as $value) {
if (!in_array($value, $validValues)) {
return;
}
}
}
$this->events = is_array($values) ? $values : array_map('trim', explode(',', $values));

$this->events = array_filter($this->events);
}
Expand Down
17 changes: 17 additions & 0 deletions Common/ClassUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Xiidea\EasyAuditBundle\Common;

use Doctrine\Persistence\Proxy;

class ClassUtils
{
public static function getClass($entity): string
{
if ($entity instanceof Proxy) {
return get_parent_class($entity);
}

return get_class($entity);
}
}
12 changes: 6 additions & 6 deletions Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

class Logger implements LoggerInterface
{
private $entityDeleteLogs = [];
private array $entityDeleteLogs = [];

public function __construct(private ManagerRegistry $doctrine)
{
}

#[\Override]
public function log(AuditLog $event = null)
public function log(?AuditLog $event = null): void
{
if (empty($event)) {
return;
Expand All @@ -43,29 +43,29 @@ public function log(AuditLog $event = null)
/**
* @return ObjectManager
*/
protected function getManager()
protected function getManager(): ObjectManager
{
return $this->getDoctrine()->getManager();
}

/**
* @return ManagerRegistry
*/
public function getDoctrine()
public function getDoctrine(): ManagerRegistry
{
return $this->doctrine;
}

/**
* @param AuditLog $event
*/
protected function saveLog(AuditLog $event)
protected function saveLog(AuditLog $event): void
{
$this->getManager()->persist($event);
$this->getManager()->flush($event);
}

public function savePendingLogs()
public function savePendingLogs(): void
{
foreach ($this->entityDeleteLogs as $log) {
$this->saveLog($log);
Expand Down
2 changes: 1 addition & 1 deletion Logger/MonologLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(private \Psr\Log\LoggerInterface $logger)
}

#[\Override]
public function log(AuditLog $event = null)
public function log(?AuditLog $event = null)
{
if (null === $event) {
return;
Expand Down
6 changes: 3 additions & 3 deletions Resolver/DoctrineObjectEventResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
namespace Xiidea\EasyAuditBundle\Resolver;

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Contracts\EventDispatcher\Event;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;
use Xiidea\EasyAuditBundle\Common\ClassUtils;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;

/** Custom Event Resolver Example Class */
class DoctrineObjectEventResolver implements EventResolverInterface
Expand Down Expand Up @@ -81,7 +81,7 @@ protected function getSingleIdentity()

/**
* @param DoctrineObjectEvent $event
* @param string $eventName
* @param string $eventName
*/
private function initialize(DoctrineObjectEvent $event, $eventName)
{
Expand Down
22 changes: 11 additions & 11 deletions Subscriber/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

namespace Xiidea\EasyAuditBundle\Subscriber;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Xiidea\EasyAuditBundle\Attribute\SubscribeDoctrineEvents;
use Xiidea\EasyAuditBundle\Common\ClassUtils;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;

class DoctrineSubscriber
{
private array $toBeDeleted = [];
private $dispatcher = null;
private $dispatcher = null;

public function __construct(private array $entities = [])
{
Expand Down Expand Up @@ -71,10 +71,10 @@ private function getToBeDeletedId($entity)
}

/**
* @param string $eventName
* @param LifecycleEventArgs $args
* @param string $eventName
* @param LifecycleEventArgs $args
*/
private function handleEvent($eventName, LifecycleEventArgs $args)
private function handleEvent($eventName, LifecycleEventArgs $args): void
{
if (true === $this->isConfiguredToTrack($args->getObject(), $eventName)) {
$this->dispatcher->dispatch(
Expand All @@ -86,7 +86,7 @@ private function handleEvent($eventName, LifecycleEventArgs $args)

/**
* @param $entity
* @param string $eventName
* @param string $eventName
*
* @return bool
*/
Expand Down Expand Up @@ -131,8 +131,8 @@ protected function isAttributedEvent($entity, $eventType)


/**
* @param string $eventType
* @param string $class
* @param string $eventType
* @param string $class
*
* @return bool
*/
Expand All @@ -142,7 +142,7 @@ private function shouldTrackEventType($eventType, $class)
}

/**
* @param string $class
* @param string $class
*
* @return bool
*/
Expand All @@ -152,7 +152,7 @@ private function shouldTrackAllEventType($class)
}

/**
* @param LifecycleEventArgs $args
* @param LifecycleEventArgs $args
* @param $className
*
* @return array
Expand All @@ -179,7 +179,7 @@ private function isScheduledForDelete($entity)
}

/**
* @param EventDispatcherInterface $dispatcher
* @param EventDispatcherInterface $dispatcher
*/
public function setDispatcher($dispatcher)
{
Expand Down
24 changes: 6 additions & 18 deletions Tests/Attribute/SubscribeDoctrineEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class SubscribeDoctrineEventsTest extends TestCase
{
public function testConstructWithoutData()
{
$annotation = new SubscribeDoctrineEvents(array());
$annotation = new SubscribeDoctrineEvents([]);

$this->assertTrue(is_array($annotation->events));
$this->assertEmpty($annotation->events);
}

public function testConstructWithInvalidData()
{
$data = array(
$data = [
'unknown' => 'foo',
'array' => array('bar' => 'bar'),
);
'array' => ['bar' => 'bar'],
];

$annotation = new SubscribeDoctrineEvents($data);

Expand All @@ -39,25 +39,13 @@ public function testConstructWithInvalidData()

public function testConstructWithValue()
{
$data = array('value' => 'updated,created');
$data = 'created,updated';

$annotation = new SubscribeDoctrineEvents($data);

$this->assertTrue(is_array($annotation->events));
$this->assertNotEmpty($annotation->events);

$this->assertEquals(explode(',', $data['value']), $annotation->events);
}

public function testConstructWithEvent()
{
$data = array('events' => 'updated,created');

$annotation = new SubscribeDoctrineEvents($data);

$this->assertTrue(is_array($annotation->events));
$this->assertNotEmpty($annotation->events);

$this->assertEquals(explode(',', $data['events']), $annotation->events);
$this->assertEquals(explode(',', $data), $annotation->events);
}
}
40 changes: 40 additions & 0 deletions Tests/Common/ClassUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Xiidea\EasyAuditBundle\Tests\Common;

use Doctrine\Persistence\Proxy;
use PHPUnit\Framework\TestCase;
use Xiidea\EasyAuditBundle\Common\ClassUtils;

class DummyClass
{
}

class DummyProxyClass extends DummyClass implements Proxy
{
public function __load(): void
{
}

public function __isInitialized(): bool
{
return true;
}
}

class ClassUtilTest extends TestCase
{
public function testGetClassWithNormalObject()
{
$object = new DummyClass();
$result = ClassUtils::getClass($object);
$this->assertSame(DummyClass::class, $result);
}

public function testGetClassWithProxyObject()
{
$proxy = new DummyProxyClass();
$result = ClassUtils::getClass($proxy);
$this->assertSame(DummyClass::class, $result);
}
}
8 changes: 4 additions & 4 deletions Tests/Fixtures/ODM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener
/**
* Collect information about a property change.
*
* @param object $sender the object on which the property changed
* @param object $sender the object on which the property changed
* @param string $propertyName the name of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
*/
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
public function propertyChanged(object $sender, string $propertyName, mixed $oldValue, mixed $newValue): void
{
}

Expand Down
Loading