Skip to content

Commit

Permalink
Fix PHP linting error
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed Nov 7, 2024
1 parent b84e204 commit 3fd5c4f
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 84 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*.swo
.DS_Store
composer.lock
/public/
/public/
.php-cs-fixer.cache
8 changes: 4 additions & 4 deletions src/Contract/ListenerLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Interface for classes that load event listeners into a ListenerProvider.
*
*
* This interface allows for modular and configurable loading of event listeners,
* making it easier to organize and maintain event listeners in different parts
* of the application.
Expand All @@ -15,11 +15,11 @@ interface ListenerLoaderInterface
{
/**
* Loads event listeners into the provided ListenerProvider.
*
*
* Implementations should use this method to register their event listeners
* with the provider, typically using the provider's addListener method.
*
*
* @param ListenerProvider $provider The provider to load listeners into
*/
public function loadListeners(ListenerProvider $provider): void;
}
}
13 changes: 7 additions & 6 deletions src/DataObjectEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Event listener for DataObject events that filters events based on operation type and object class.
*
*
* This listener can be configured to only handle specific operations (create, update, delete etc)
* and specific DataObject classes. When an event matches the configured criteria, the callback
* is executed with the event.
Expand All @@ -19,9 +19,9 @@ class DataObjectEventListener
/**
* Creates a new DataObject event listener.
*
* @param Closure(DataObjectEvent): void $callback Callback to execute when an event matches
* @param class-string<DataObject>[] $classes Array of DataObject class names to listen for
* @param Operation[]|null $operations Array of operations to listen for. If null, listens for all operations.
* @param Closure(DataObjectEvent): void $callback Callback to execute when an event matches
* @param class-string<DataObject>[] $classes Array of DataObject class names to listen for
* @param Operation[]|null $operations Array of operations to listen for. If null, listens for all operations.
*/
public function __construct(
private Closure $callback,
Expand All @@ -33,7 +33,7 @@ public function __construct(

/**
* Handles a DataObject event.
*
*
* Checks if the event matches the configured operations and classes,
* and executes the callback if it does.
*
Expand All @@ -57,10 +57,11 @@ public function __invoke(DataObjectEvent $event): void

/**
* Checks if the given class matches any of the configured target classes.
*
*
* A match occurs if the class is either the same as or a subclass of any target class.
*
* @param string $class The class name to check
*
* @return bool True if the class should be handled, false otherwise
*/
private function shouldHandleClass(string $class): bool
Expand Down
36 changes: 18 additions & 18 deletions src/Event/DataObjectEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

/**
* Event class representing operations performed on DataObjects.
*
*
* This event is dispatched whenever a significant operation occurs on a DataObject,
* such as creation, updates, deletion, or versioning operations. It captures key
* information about the operation including:
*
*
* - The ID of the affected DataObject
* - The class of the DataObject
* - The type of operation performed
* - The version number (for versioned objects)
* - The ID of the member who performed the operation
* - The timestamp when the operation occurred
*
*
* Example usage:
* ```php
* $event = DataObjectEvent::create(
Expand All @@ -42,11 +42,11 @@ class DataObjectEvent
private readonly int $timestamp;

/**
* @param int $objectID The ID of the affected DataObject
* @param string $objectClass The class name of the affected DataObject
* @param Operation $operation The type of operation performed
* @param int|null $version The version number (for versioned objects)
* @param int|null $memberID The ID of the member who performed the operation
* @param int $objectID The ID of the affected DataObject
* @param string $objectClass The class name of the affected DataObject
* @param Operation $operation The type of operation performed
* @param int|null $version The version number (for versioned objects)
* @param int|null $memberID The ID of the member who performed the operation
*/
public function __construct(
private readonly int $objectID,
Expand Down Expand Up @@ -108,9 +108,9 @@ public function getTimestamp(): int

/**
* Get the DataObject associated with this event
*
*
* @param bool $useVersion If true and the object is versioned, retrieves the specific version that was affected
* Note: This may return null if the object has been deleted since the event was created
* Note: This may return null if the object has been deleted since the event was created
*/
public function getObject(bool $useVersion = false): ?DataObject
{
Expand All @@ -119,12 +119,12 @@ public function getObject(bool $useVersion = false): ?DataObject
}

$object = DataObject::get_by_id($this->objectClass, $this->objectID);

// If we want the specific version and the object is versioned
if ($useVersion && $this->version && $object && $object->hasExtension(Versioned::class)) {
/** @var Versioned|DataObject $object */
return $object->Version == $this->version
? $object
return $object->Version == $this->version
? $object
: $object->Versions()->byID($this->version);
}

Expand All @@ -133,7 +133,7 @@ public function getObject(bool $useVersion = false): ?DataObject

/**
* Get the Member who performed the operation
*
*
* Note: This may return null if the member has been deleted since the event was created
* or if the operation was performed by a system process
*/
Expand Down Expand Up @@ -163,20 +163,20 @@ public function serialize(): string

/**
* Unserialize the event from a string
*
*
* @param string $data
*/
public function unserialize(string $data): void
{
$unserialized = unserialize($data);

// Use reflection to set readonly properties
$reflection = new \ReflectionClass($this);

foreach ($unserialized as $property => $value) {
$prop = $reflection->getProperty($property);
$prop->setAccessible(true);
$prop->setValue($this, $value);
}
}
}
}
6 changes: 3 additions & 3 deletions src/Event/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

/**
* Represents the type of operation performed on a DataObject.
*
*
* This enum is used to identify what kind of operation triggered a DataObjectEvent.
* Each operation maps to a specific action in the Silverstripe CMS:
*
*
* - CREATE: First time a DataObject is written to the database
* - UPDATE: Subsequent writes to an existing DataObject
* - DELETE: When a DataObject is deleted (both soft and hard deletes)
Expand All @@ -25,4 +25,4 @@ enum Operation: string
case UNPUBLISH = 'unpublish';
case ARCHIVE = 'archive';
case RESTORE = 'restore';
}
}
23 changes: 12 additions & 11 deletions src/Extension/EventDispatchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace ArchiPro\Silverstripe\EventDispatcher\Extension;

use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use ArchiPro\Silverstripe\EventDispatcher\Event\DataObjectEvent;
use ArchiPro\Silverstripe\EventDispatcher\Event\Operation;
use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Security;
use SilverStripe\Versioned\Versioned;

/**
* Extension that adds event dispatching capabilities to DataObjects.
*
*
* @property DataObject|Versioned $owner
*
* @method DataObject getOwner()
*/
class EventDispatchExtension extends Extension
Expand All @@ -34,7 +35,7 @@ public function onAfterWrite(): void
$owner->hasExtension(Versioned::class) ? $owner->Version : null,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -51,7 +52,7 @@ public function onBeforeDelete(): void
$owner->hasExtension(Versioned::class) ? $owner->Version : null,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -72,7 +73,7 @@ public function onAfterPublish(): void
$owner->Version,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -93,7 +94,7 @@ public function onAfterUnpublish(): void
$owner->Version,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -114,7 +115,7 @@ public function onAfterArchive(): void
$owner->Version,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -135,7 +136,7 @@ public function onAfterRestore(): void
$owner->Version,
Security::getCurrentUser()?->ID
);

$this->dispatchEvent($event);
}

Expand All @@ -146,4 +147,4 @@ protected function dispatchEvent(DataObjectEvent $event): DataObjectEvent
{
return Injector::inst()->get(EventService::class)->dispatch($event);
}
}
}
9 changes: 6 additions & 3 deletions src/Service/EventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\ListenerProvider;
use ArchiPro\Silverstripe\EventDispatcher\Contract\ListenerLoaderInterface;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;

/**
* Core service class for handling event dispatching in Silverstripe.
*
*
* This service wraps a PSR-14 compliant event dispatcher and provides
* a centralized way to dispatch events throughout the application.
*/
Expand All @@ -21,12 +21,14 @@ class EventService

/**
* @config
*
* @var array<string,array<callable>> Map of event class names to arrays of listener callbacks
*/
private static array $listeners = [];

/**
* @config
*
* @var array<ListenerLoaderInterface> Array of listener loaders
*/
private static array $loaders = [];
Expand Down Expand Up @@ -76,6 +78,7 @@ public function addListener(string $event, callable $listener): void

/**
* Adds a listener loader to the event service
*
* @throws \RuntimeException If the loader does not implement ListenerLoaderInterface
*/
public function addListenerLoader(ListenerLoaderInterface $loader): void
Expand Down Expand Up @@ -104,4 +107,4 @@ public function getListenerProvider(): ListenerProvider
{
return $this->listenerProvider;
}
}
}
4 changes: 2 additions & 2 deletions tests/php/Event/AbstractDataObjectEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace ArchiPro\Silverstripe\EventDispatcher\Tests\Event;

use SilverStripe\Dev\SapphireTest;
use ArchiPro\Silverstripe\EventDispatcher\Event\DataObjectWriteEvent;
use SilverStripe\Dev\SapphireTest;

class AbstractDataObjectEventTest extends SapphireTest
{
Expand Down Expand Up @@ -40,4 +40,4 @@ public function testJsonSerialization(): void
$this->assertArrayHasKey('changes', $data);
$this->assertArrayHasKey('timestamp', $data);
}
}
}
Loading

0 comments on commit 3fd5c4f

Please sign in to comment.