Skip to content
Merged
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parameters:
- '/Call to static method Webmozart\\Assert\\Assert::allIsInstanceOf\(\) with/'
- '/Function compact\(\) should not be used/'
- '/In method .+, caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception./'
- '/Class OpenClassrooms\\ServiceProxy\\Model\\Event is neither abstract nor final./'
exceptions:
check:
missingCheckedExceptionInThrows: true
Expand Down
5 changes: 5 additions & 0 deletions src/FrameworkBridge/Symfony/Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\Messenger\Transport\Serialization\MessageSerializer;
use OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\Subscriber\ServiceProxySubscriber;
use OpenClassrooms\ServiceProxy\Handler\Impl\Cache\SymfonyCacheHandler;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\Event\ServiceProxyEventFactory;
use OpenClassrooms\ServiceProxy\Invoker\Impl\AggregateMethodInvoker;
use OpenClassrooms\ServiceProxy\ProxyFactory;
use OpenClassrooms\ServiceProxy\ProxyFactoryConfiguration;
Expand Down Expand Up @@ -74,4 +75,8 @@
service('annotation_reader')->nullOnInvalid(),
])
->tag('kernel.event_subscriber');

$services->set(ServiceProxyEventFactory::class)
->autowire()
->autoconfigure();
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function dispatch(Event $event, ?string $queue = null): void
{
$this->eventDispatcher->dispatch(
$event,
$event->name . '.' . Transport::SYNC->value,
$event->name,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sidux since we set the SYNC in the getName() I removed it from here otherwise we have a post.use_case.sync.sync

);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Interceptor/Config/EventInterceptorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace OpenClassrooms\ServiceProxy\Interceptor\Config;

use OpenClassrooms\ServiceProxy\Model\Event;

final class EventInterceptorConfig
{
/**
* @param class-string<Event> $eventInstanceClassName
*/
public function __construct(
public readonly string $eventInstanceClassName = Event::class,
) {
}
}
24 changes: 24 additions & 0 deletions src/Interceptor/Contract/Event/EventFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace OpenClassrooms\ServiceProxy\Interceptor\Contract\Event;

use OpenClassrooms\ServiceProxy\Model\Event;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
use OpenClassrooms\ServiceProxy\Model\Request\Moment;

interface EventFactory
{
/**
* @template T of Event
* @param class-string<T> $eventClassName
* @return T
*/
public function createFromSenderInstance(
Instance $instance,
Moment $moment = Moment::SUFFIX,
?string $name = null,
string $eventClassName = Event::class
): Event;
}
36 changes: 36 additions & 0 deletions src/Interceptor/Impl/Event/ServiceProxyEventFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace OpenClassrooms\ServiceProxy\Interceptor\Impl\Event;

use OpenClassrooms\ServiceProxy\Interceptor\Contract\Event\EventFactory;
use OpenClassrooms\ServiceProxy\Model\Event;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
use OpenClassrooms\ServiceProxy\Model\Request\Moment;

final class ServiceProxyEventFactory implements EventFactory
{
/**
* @template T of Event
* @param class-string<T> $eventClassName
* @return T
*/
public function createFromSenderInstance(
Instance $instance,
Moment $moment = Moment::SUFFIX,
?string $name = null,
string $eventClassName = Event::class
): Event {
return new $eventClassName(
$instance->getReflection()->getName(),
$instance->getReflection()->getShortName(),
$instance->getMethod()->getName(),
$instance->getMethod()->getParameters(),
$name,
$instance->getMethod()->getReturnedValue(),
$instance->getMethod()->getException(),
$moment,
);
}
}
28 changes: 19 additions & 9 deletions src/Interceptor/Impl/EventInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use OpenClassrooms\ServiceProxy\Attribute\Event;
use OpenClassrooms\ServiceProxy\Handler\Contract\EventHandler;
use OpenClassrooms\ServiceProxy\Interceptor\Config\EventInterceptorConfig;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\AbstractInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\Event\EventFactory;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\PrefixInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\SuffixInterceptor;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
Expand All @@ -15,6 +17,14 @@

final class EventInterceptor extends AbstractInterceptor implements SuffixInterceptor, PrefixInterceptor
{
public function __construct(
private readonly EventFactory $eventFactory,
private readonly EventInterceptorConfig $config,
iterable $handlers = [],
) {
parent::__construct($handlers);
}

public function getPrefixPriority(): int
{
return 20;
Expand All @@ -28,15 +38,15 @@ public function getSuffixPriority(): int
public function prefix(Instance $instance): Response
{
$attributes = $instance->getMethod()
->getAttributesInstances(Event::class)
;
->getAttributesInstances(Event::class);

foreach ($attributes as $attribute) {
$handlers = $this->getHandlers(EventHandler::class, $attribute);
$event = \OpenClassrooms\ServiceProxy\Model\Event::createFromSenderInstance(
$event = $this->eventFactory->createFromSenderInstance(
$instance,
Moment::PREFIX,
$attribute->name,
$this->config->eventInstanceClassName,
);
foreach ($handlers as $handler) {
if ($attribute->isPre()) {
Expand All @@ -51,27 +61,28 @@ public function prefix(Instance $instance): Response
public function suffix(Instance $instance): Response
{
$attributes = $instance->getMethod()
->getAttributes(Event::class)
;
->getAttributes(Event::class);

foreach ($attributes as $attribute) {
$attribute = $attribute->newInstance();
$handlers = $this->getHandlers(EventHandler::class, $attribute);
foreach ($handlers as $handler) {
if ($attribute->isPost() && !$instance->getMethod()->threwException()) {
$event = \OpenClassrooms\ServiceProxy\Model\Event::createFromSenderInstance(
$event = $this->eventFactory->createFromSenderInstance(
$instance,
Moment::SUFFIX,
$attribute->name,
$this->config->eventInstanceClassName,
);
$handler->dispatch($event, $attribute->queue);
}

if ($attribute->isOnException() && $instance->getMethod()->threwException()) {
$event = \OpenClassrooms\ServiceProxy\Model\Event::createFromSenderInstance(
$event = $this->eventFactory->createFromSenderInstance(
$instance,
Moment::EXCEPTION,
$attribute->name,
$this->config->eventInstanceClassName,
);
$handler->dispatch($event, $attribute->queue);
}
Expand All @@ -89,7 +100,6 @@ public function supportsSuffix(Instance $instance): bool
public function supportsPrefix(Instance $instance): bool
{
return $instance->getMethod()
->hasAttribute(Event::class)
;
->hasAttribute(Event::class);
}
}
76 changes: 26 additions & 50 deletions src/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,48 @@
namespace OpenClassrooms\ServiceProxy\Model;

use OpenClassrooms\ServiceProxy\Attribute\Event\Transport;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
use OpenClassrooms\ServiceProxy\Model\Request\Moment;

final class Event
class Event
{
public string $name;

/**
* @param mixed[] $parameters
* @param class-string $class
* @param array<string, mixed> $parameters
*/
public function __construct(
public readonly string $name,
public readonly string $class,
public readonly string $classShortName,
public readonly string $method,
public readonly array $parameters,
?string $name = null,
public readonly mixed $response = null,
public readonly mixed $exception = null,
public readonly Moment $type = Moment::SUFFIX
) {
$this->name = self::getName(
className: $class,
moment: $type,
transport: Transport::SYNC,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should pass the transport from the constructor which is set in the attribute

method: $method,
name: $name
);
}

public static function createFromSenderInstance(
Instance $instance,
Moment $moment = Moment::SUFFIX,
?string $name = null
): self {
/** @var class-string $className */
$className = $instance->getReflection()->getName();
public function getUseCaseRequest(): mixed
{
return $this->parameters['useCaseRequest'] ?? ($this->parameters['request'] ?? null);
}

return new self(
self::getName(
className: $className,
moment: $moment,
method: $instance->getMethod()
->getName(),
name: $name,
),
$instance->getReflection()
->getName(),
$instance->getReflection()
->getShortName(),
$instance->getMethod()
->getName(),
$instance->getMethod()
->getParameters(),
$instance->getMethod()
->getReturnedValue(),
$instance->getMethod()
->getException(),
$moment,
);
public function getUseCaseResponse(): mixed
{
return $this->response;
}

public function getUseCaseException(): mixed
{
return $this->exception;
}

/**
Expand All @@ -70,7 +62,6 @@ public static function getName(
if ($name !== null) {
return $name;
}

$parts = explode('\\', $className);
$classShortName = array_pop($parts);

Expand All @@ -80,25 +71,10 @@ public static function getName(

$name = mb_strtolower((string) preg_replace('/(?<=\\w)(?=[A-Z])/', '_$1', $name));

if ($transport !== null) {
if ($transport !== null && $transport !== Transport::SYNC) {
return "{$moment->value}.{$name}.{$transport->value}";
}

return "{$moment->value}.{$name}";
}

public function getUseCaseRequest(): mixed
{
return $this->parameters['useCaseRequest'] ?? ($this->parameters['request'] ?? null);
}

public function getUseCaseResponse(): mixed
{
return $this->response;
}

public function getUseCaseException(): mixed
{
return $this->exception;
}
}
4 changes: 4 additions & 0 deletions tests/Interceptor/EventInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace OpenClassrooms\ServiceProxy\Tests\Interceptor;

use OpenClassrooms\ServiceProxy\Interceptor\Config\EventInterceptorConfig;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\Event\ServiceProxyEventFactory;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\EventInterceptor;
use OpenClassrooms\ServiceProxy\ProxyFactory;
use OpenClassrooms\ServiceProxy\Tests\Double\Mock\Event\EventHandlerMock;
Expand All @@ -28,6 +30,8 @@ protected function setUp(): void
$this->proxyFactory = $this->getProxyFactory(
[
new EventInterceptor(
new ServiceProxyEventFactory(),
new EventInterceptorConfig(),
[$this->handler],
),
]
Expand Down
7 changes: 6 additions & 1 deletion tests/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace OpenClassrooms\ServiceProxy\Tests;

use OpenClassrooms\ServiceProxy\Interceptor\Config\EventInterceptorConfig;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\PrefixInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\SuffixInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\CacheInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\Event\ServiceProxyEventFactory;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\EventInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\InvalidateCacheInterceptor;
use OpenClassrooms\ServiceProxy\Interceptor\Impl\LegacyCacheInterceptor;
Expand Down Expand Up @@ -37,7 +39,10 @@ protected function setUp(): void
$classes = get_declared_classes();

foreach ($classes as $class) {
if (is_subclass_of($class, PrefixInterceptor::class) || is_subclass_of($class, SuffixInterceptor::class)) {
if ($class === EventInterceptor::class) {
$interceptors[] = new $class(new ServiceProxyEventFactory(), new EventInterceptorConfig());
} elseif (is_subclass_of($class, PrefixInterceptor::class)
|| is_subclass_of($class, SuffixInterceptor::class)) {
$interceptors[] = new $class();
}
}
Expand Down
Loading