-
-
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.
✨ Add event system for processing IpStrategy
- Loading branch information
Showing
11 changed files
with
215 additions
and
55 deletions.
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
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\Event; | ||
|
||
use Flow\Ip; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
final class PopEvent extends Event | ||
{ | ||
/** | ||
* @param Ip<T> $ip | ||
*/ | ||
private Ip $ip; | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
*/ | ||
public function __construct(Ip $ip) | ||
{ | ||
$this->ip = $ip; | ||
} | ||
|
||
/** | ||
* @return Ip<T> | ||
*/ | ||
public function getIp(): Ip | ||
{ | ||
return $this->ip; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\Event; | ||
|
||
use Flow\Ip; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
final class PullEvent extends Event | ||
{ | ||
/** | ||
* @param null|Ip<T> $ip | ||
*/ | ||
private ?Ip $ip = null; | ||
|
||
/** | ||
* @return null|Ip<T> | ||
*/ | ||
public function getIp(): ?Ip | ||
{ | ||
return $this->ip; | ||
} | ||
|
||
/** | ||
* @return null|Ip<T> | ||
*/ | ||
public function setIp(?Ip $ip) | ||
{ | ||
$this->ip = $ip; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\Event; | ||
|
||
use Flow\Ip; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
final class PushEvent extends Event | ||
{ | ||
/** | ||
* @param Ip<T> $ip | ||
*/ | ||
private Ip $ip; | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
*/ | ||
public function __construct(Ip $ip) | ||
{ | ||
$this->ip = $ip; | ||
} | ||
|
||
/** | ||
* @return Ip<T> | ||
*/ | ||
public function getIp(): Ip | ||
{ | ||
return $this->ip; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,13 +4,17 @@ | |
|
||
namespace Flow\IpStrategy; | ||
|
||
use Flow\Event\PopEvent; | ||
use Flow\Event\PullEvent; | ||
use Flow\Event\PushEvent; | ||
use Flow\Ip; | ||
use Flow\IpStrategyEvent; | ||
use Flow\IpStrategyInterface; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @implements IpStrategyInterface<T> | ||
* @implements IpStrategyInterface | ||
*/ | ||
class LinearIpStrategy implements IpStrategyInterface | ||
Check failure on line 19 in src/IpStrategy/LinearIpStrategy.php GitHub Actions / Execute PHPStan analysis (8.2)
|
||
{ | ||
|
@@ -19,26 +23,27 @@ class LinearIpStrategy implements IpStrategyInterface | |
*/ | ||
private array $ips = []; | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
*/ | ||
public function push(Ip $ip): void | ||
public static function getSubscribedEvents(): array | ||
{ | ||
$this->ips[] = $ip; | ||
return [ | ||
IpStrategyEvent::PUSH => 'push', | ||
IpStrategyEvent::PULL => 'pull', | ||
]; | ||
} | ||
|
||
/** | ||
* @return null|Ip<T> | ||
* @param PushEvent<T> $event | ||
*/ | ||
public function pop(): ?Ip | ||
public function push(PushEvent $event): void | ||
{ | ||
return array_shift($this->ips); | ||
$this->ips[] = $event->getIp(); | ||
} | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
* @param PullEvent<T> $event | ||
*/ | ||
public function done(Ip $ip): void | ||
public function pull(PullEvent $event): void | ||
{ | ||
$event->setIp(array_shift($this->ips)); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,13 +4,17 @@ | |
|
||
namespace Flow\IpStrategy; | ||
|
||
use Flow\Event\PopEvent; | ||
use Flow\Event\PullEvent; | ||
use Flow\Event\PushEvent; | ||
use Flow\Ip; | ||
use Flow\IpStrategyEvent; | ||
use Flow\IpStrategyInterface; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @implements IpStrategyInterface<T> | ||
* @implements IpStrategyInterface | ||
*/ | ||
class MaxIpStrategy implements IpStrategyInterface | ||
Check failure on line 19 in src/IpStrategy/MaxIpStrategy.php GitHub Actions / Execute PHPStan analysis (8.2)
|
||
{ | ||
|
@@ -29,21 +33,30 @@ public function __construct(private int $max = 1, IpStrategyInterface $ipStrateg | |
$this->ipStrategy = $ipStrategy ?? new LinearIpStrategy(); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
IpStrategyEvent::PUSH => 'push', | ||
IpStrategyEvent::PULL => 'pull', | ||
IpStrategyEvent::POP => 'pop', | ||
]; | ||
} | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
* @param PushEvent<T> $even | ||
*/ | ||
public function push(Ip $ip): void | ||
public function push(PushEvent $event): void | ||
{ | ||
$this->ipStrategy->push($ip); | ||
$this->ipStrategy->push($event->getIp()); | ||
} | ||
|
||
/** | ||
* @return null|Ip<T> | ||
* @return PullEvent<T> | ||
*/ | ||
public function pop(): ?Ip | ||
public function pull(PullEvent $event): ?Ip | ||
{ | ||
if ($this->processing < $this->max) { | ||
$ip = $this->ipStrategy->pop(); | ||
$ip = $this->ipStrategy->pull(); | ||
if ($ip) { | ||
$this->processing++; | ||
} | ||
|
@@ -55,11 +68,11 @@ public function pop(): ?Ip | |
} | ||
|
||
/** | ||
* @param Ip<T> $ip | ||
* @param PopEvent<T> $event | ||
*/ | ||
public function done(Ip $ip): void | ||
public function done(PopEvent $event): void | ||
{ | ||
$this->ipStrategy->done($ip); | ||
$this->ipStrategy->done($event->getIp()); | ||
$this->processing--; | ||
} | ||
} |
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
Oops, something went wrong.