Untitled

mail@pastecode.io avatar
unknown
php
a year ago
1.5 kB
1
Indexable
Never
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use App\Message\WriteToFileMessage;
use Symfony\Component\HttpFoundation\JsonResponse;

class RunController extends AbstractController
{

    public function run( MessageBusInterface $messageBus ): Response
    {
        $message = new WriteToFileMessage( 'umut', 5000 );
        $messageBus->dispatch( $message );

        return new JsonResponse( [ 'status' => true ], 200 );
    }
}
?>
<?php 

namespace App\Message;

class WriteToFileMessage
{
    private $text = [];
    private $count;

    public function __construct( String $text, int $count)
    {
        $this->text = $text;
        $this->count = $count;
    }

    public function getCount(): int
    {
        return $this->count;
    }

    public function getText(): String
    {
        return $this->text;
    }
}
?>
<?php 

namespace App\MessageHandler;

use App\Message\WriteToFileMessage;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class WriteToFileHandler
{

    public function __invoke( WriteToFileMessage $message )
    {
        for ($i=0; $i < $message->getCount() ; $i++) { 

            file_put_contents( 'cikti.txt' , $message->getText().PHP_EOL , FILE_APPEND);
        }
    }
}


?>