<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of eelly package.
 *
 * (c) eelly.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Shadon\Events\Listener;

use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use PhpAmqpLib\Exception\AMQPTimeoutException;

/**
 * async annotation listener.
 *
 * @author hehui<<EMAIL>>
 */
class AsyncAnnotationListener extends AbstractListener
{
    /**
     * 注解名称.
     */
    private const ANNOTATIONS_NAME = 'Async';

    /**
     * @param Event      $event
     * @param Dispatcher $dispatcher
     *
     * @return bool
     */
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        if ('no-cache' == $this->request->getHeader('Cache-Control')) {
            return true;
        }
        // Parse the annotations in the method currently executed
        $annotations = $this->annotations->getMethod(
            $dispatcher->getControllerClass(),
            $dispatcher->getActiveMethod()
        );
        if ($annotations->has(self::ANNOTATIONS_NAME)) {
            $annotation = $annotations->get(self::ANNOTATIONS_NAME);
            $msgBody = [
                'class'   => $dispatcher->getControllerClass(),
                'method'  => $dispatcher->getActiveMethod(),
                'params'  => $dispatcher->getParams(),
                'time'    => microtime(true),
            ];
            try {
                /* @var \Shadon\Queue\Adapter\Producer $producer */
                $producer = $this->queueFactory->createProducer();
            } catch (AMQPTimeoutException | \ErrorException $e) {
                return true;
            }
            $producer->setExchangeOptions(['name' => $dispatcher->getModuleName(), 'type' => 'topic']);
            $routingKey = $annotation->getNamedParameter('route') ?? 'default_routing_key';
            $producer->publish(json_encode($msgBody), $routingKey);

            $connection = $producer->getConnection();
            if ($connection->isConnected()) {
                try {
                    $connection->close();
                } catch (\PhpAmqpLib\Exception\AMQPRuntimeException $e) {
                }
            }
            if ($event->isCancelable()) {
                $event->stop();
            }
            $dispatcher->setReturnedValue(true);

            return false;<commit_msg>Apply fixes from StyleCI<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of eelly package.
 *
 * (c) eelly.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Shadon\Events\Listener;

use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use PhpAmqpLib\Exception\AMQPTimeoutException;

/**
 * async annotation listener.
 *
 * @author hehui<<EMAIL>>
 */
class AsyncAnnotationListener extends AbstractListener
{
    /**
     * 注解名称.
     */
    private const ANNOTATIONS_NAME = 'Async';

    /**
     * @param Event      $event
     * @param Dispatcher $dispatcher
     *
     * @return bool
     */
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        if ('no-cache' == $this->request->getHeader('Cache-Control')) {
            return true;
        }
        // Parse the annotations in the method currently executed
        $annotations = $this->annotations->getMethod(
            $dispatcher->getControllerClass(),
            $dispatcher->getActiveMethod()
        );
        if ($annotations->has(self::ANNOTATIONS_NAME)) {
            $annotation = $annotations->get(self::ANNOTATIONS_NAME);
            $msgBody = [
                'class'   => $dispatcher->getControllerClass(),
                'method'  => $dispatcher->getActiveMethod(),
                'params'  => $dispatcher->getParams(),
                'time'    => microtime(true),
            ];

            try {
                /* @var \Shadon\Queue\Adapter\Producer $producer */
                $producer = $this->queueFactory->createProducer();
            } catch (AMQPTimeoutException | \ErrorException $e) {
                return true;
            }
            $producer->setExchangeOptions(['name' => $dispatcher->getModuleName(), 'type' => 'topic']);
            $routingKey = $annotation->getNamedParameter('route') ?? 'default_routing_key';
            $producer->publish(json_encode($msgBody), $routingKey);

            $connection = $producer->getConnection();
            if ($connection->isConnected()) {
                try {
                    $connection->close();
                } catch (\PhpAmqpLib\Exception\AMQPRuntimeException $e) {
                }
            }
            if ($event->isCancelable()) {
                $event->stop();
            }
            $dispatcher->setReturnedValue(true);

            return false;
{'fraction_non_alphanumeric': 0.1301010101010101, 'fraction_numerical': 0.00040404040404040404, 'mean_word_length': 2.054287476866132, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, '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': '3947002', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1315, 'n_words': 266}