<commit_before><?php

namespace Gos\Bundle\WebSocketBundle\Event;

use Gos\Bundle\WebSocketBundle\Pusher\ServerPushHandlerRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\Registry\PeriodicRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use React\Socket\Server;

class StartServerListener
{
    /**
     * @var PeriodicRegistry
     */
    protected $periodicRegistry;

    /**
     * @var ServerPushHandlerRegistry
     */
    protected $serverPushHandlerRegistry;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param PeriodicRegistry          $periodicRegistry
     * @param ServerPushHandlerRegistry $serverPushHandlerRegistry
     * @param LoggerInterface|null      $logger
     */
    public function __construct(
        PeriodicRegistry $periodicRegistry,
        ServerPushHandlerRegistry $serverPushHandlerRegistry,
        LoggerInterface $logger = null
    ) {
        $this->periodicRegistry = $periodicRegistry;
        $this->serverPushHandlerRegistry = $serverPushHandlerRegistry;
        $this->logger = null === $logger ? new NullLogger() : $logger;
    }

    /**
     * @param Server        $server
     * @param LoopInterface $loop
     */
    protected function closure(Server $server, LoopInterface $loop)
    {
        $this->logger->notice('Stopping server ...');

        foreach ($this->serverPushHandlerRegistry->getPushers() as $handler) {
            $handler->close();
            $this->logger->info(sprintf('Stop %s push handler', $handler->getName()));
        }

        $server->emit('end');
        $server->close();

        foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
            if ($periodic instanceof TimerInterface) {
                $loop->cancelTimer($periodic);
            }
        }

        $loop->stop();

        $this->logger->notice('Server stopped!');
    }

    /**
     * @param ServerEvent $event
     */
    public function bindPnctlEvent(ServerEvent $event)
    {
        $loop = $event->getEventLoop();
        $server = $event->getServer();

        if (defined('SIGINT')) {
            $loop->addSignal(SIGINT, function () use ($server, $loop) {
                $this->closure($server, $loop);
            });
        }
    }
}
<commit_msg>Inline the Closure<commit_after><?php

namespace Gos\Bundle\WebSocketBundle\Event;

use Gos\Bundle\WebSocketBundle\Pusher\ServerPushHandlerRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\Registry\PeriodicRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\EventLoop\TimerInterface;

class StartServerListener
{
    /**
     * @var PeriodicRegistry
     */
    protected $periodicRegistry;

    /**
     * @var ServerPushHandlerRegistry
     */
    protected $serverPushHandlerRegistry;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param PeriodicRegistry          $periodicRegistry
     * @param ServerPushHandlerRegistry $serverPushHandlerRegistry
     * @param LoggerInterface|null      $logger
     */
    public function __construct(
        PeriodicRegistry $periodicRegistry,
        ServerPushHandlerRegistry $serverPushHandlerRegistry,
        LoggerInterface $logger = null
    ) {
        $this->periodicRegistry = $periodicRegistry;
        $this->serverPushHandlerRegistry = $serverPushHandlerRegistry;
        $this->logger = null === $logger ? new NullLogger() : $logger;
    }

    /**
     * @param ServerEvent $event
     */
    public function bindPnctlEvent(ServerEvent $event)
    {
        $loop = $event->getEventLoop();
        $server = $event->getServer();

        if (defined('SIGINT')) {
            $loop->addSignal(SIGINT, function () use ($server, $loop) {
                $this->logger->notice('Stopping server ...');

                foreach ($this->serverPushHandlerRegistry->getPushers() as $handler) {
                    $handler->close();
                    $this->logger->info(sprintf('Stop %s push handler', $handler->getName()));
                }

                $server->emit('end');
                $server->close();

                foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
                    if ($periodic instanceof TimerInterface) {
                        $loop->cancelTimer($periodic);
                    }
                }

                $loop->stop();

                $this->logger->notice('Server stopped!');
            });
        }
    }
}

{'fraction_non_alphanumeric': 0.12527520915896082, 'fraction_numerical': 0.0, 'mean_word_length': 2.3282051282051284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5297884', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1173, 'n_words': 220}