Skip to content

Commit

Permalink
Increase randomFactor and add ENV variable for it
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Apr 25, 2024
1 parent 300a024 commit 6f07eca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/app/src/Application/Bootloader/EventsBootloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Application\Bootloader;

use App\Endpoint\Event\LikeListener;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;

final class EventsBootloader extends Bootloader
{
public function defineSingletons(): array
{
return [
LikeListener::class => static fn(EnvironmentInterface $env) => new LikeListener(
randomFactor: (int)$env->get('RANDOM_FACTOR', 20),
),
];
}
}
9 changes: 8 additions & 1 deletion app/app/src/Endpoint/Event/LikeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
namespace App\Endpoint\Event;

use App\Application\Event\Liked;
use Spiral\Core\Attribute\Singleton;
use Spiral\Events\Attribute\Listener;

#[Singleton]
final readonly class LikeListener
{
public function __construct(
private int $randomFactor = 10,
) {
}

#[Listener]
public function __invoke(Liked $event): void
{
Expand Down Expand Up @@ -125,7 +132,7 @@ public function __invoke(Liked $event): void
"When %s runs, CPU usage drops out of respect.",
];

$shouldPrint = \random_int(1, 10) === 1;
$shouldPrint = \random_int(1, $this->randomFactor) === 1;
if (!$shouldPrint) {
return;
}
Expand Down

0 comments on commit 6f07eca

Please sign in to comment.