<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Superdesk Web Publisher Core Bundle.
 *
 * Copyright 2019 Sourcefabric z.ú. and contributors.
 *
 * For the full copyright and license information, please see the
 * AUTHORS and LICENSE files distributed with this source code.
 *
 * @copyright 2019 Sourcefabric z.ú.
 * @license http://www.superdesk.org/license
 */

namespace SWP\Bundle\CoreBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface;
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ImageToWebpConversionListener
{
    protected $imageConversionProducer;

    protected $tenantContext;

    protected $isWebpConversionEnabled;

    protected $eventDispatcher;

    public function __construct(ProducerInterface $imageConversionProducer, TenantContextInterface $tenantContext, string $isWebpConversionEnabled, EventDispatcherInterface $eventDispatcher)
    {
        $this->imageConversionProducer = $imageConversionProducer;
        $this->tenantContext = $tenantContext;
        $this->isWebpConversionEnabled = $isWebpConversionEnabled;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function postPersist(LifecycleEventArgs $args): void
    {
        $rendition = $args->getObject();
        if (!$this->isWebpConversionEnabled || !$rendition instanceof ImageRenditionInterface) {
            return;
        }

        $tenantId = $this->tenantContext->getTenant()->getId();

        $this->eventDispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($rendition, $tenantId) {<commit_msg>fix image processing on production make sure that image conversion is handled also in production consumer environment where termionantion event is not fired<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Superdesk Web Publisher Core Bundle.
 *
 * Copyright 2019 Sourcefabric z.ú. and contributors.
 *
 * For the full copyright and license information, please see the
 * AUTHORS and LICENSE files distributed with this source code.
 *
 * @copyright 2019 Sourcefabric z.ú.
 * @license http://www.superdesk.org/license
 */

namespace SWP\Bundle\CoreBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface;
use SWP\Component\Bridge\Events as PackageEvents;
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class ImageToWebpConversionListener
{
    protected $imageConversionProducer;

    protected $tenantContext;

    protected $isWebpConversionEnabled;

    protected $eventDispatcher;

    public function __construct(ProducerInterface $imageConversionProducer, TenantContextInterface $tenantContext, string $isWebpConversionEnabled, EventDispatcherInterface $eventDispatcher)
    {
        $this->imageConversionProducer = $imageConversionProducer;
        $this->tenantContext = $tenantContext;
        $this->isWebpConversionEnabled = $isWebpConversionEnabled;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function postPersist(LifecycleEventArgs $args): void
    {
        $rendition = $args->getObject();
        if (!$this->isWebpConversionEnabled || !$rendition instanceof ImageRenditionInterface) {
            return;
        }

        $tenantId = $this->tenantContext->getTenant()->getId();

        $this->eventDispatcher->addListener(PackageEvents::PACKAGE_PROCESSED, function (GenericEvent $event) use ($rendition, $tenantId) {
{'fraction_non_alphanumeric': 0.08835443037974683, 'fraction_numerical': 0.004556962025316456, 'mean_word_length': 4.810294117647059, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6962919', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1065, 'n_words': 244}