-
Notifications
You must be signed in to change notification settings - Fork 2
feat(event): refacto of eventInterceptor and EventFactory #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or 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,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; | ||
| } |
This file contains hidden or 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,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, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ); | ||
Wilkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -70,7 +62,6 @@ public static function getName( | |
| if ($name !== null) { | ||
| return $name; | ||
| } | ||
|
|
||
| $parts = explode('\\', $className); | ||
| $classShortName = array_pop($parts); | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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