<commit_before><?php

declare(strict_types=1);

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

namespace SWP\Bundle\CoreBundle\EventSubscriber;

use GuzzleHttp\Client;
use SWP\Bundle\ContentBundle\ArticleEvents;
use SWP\Bundle\CoreBundle\Model\ArticlePreview;
use SWP\Bundle\CoreBundle\Repository\WebhookRepositoryInterface;
use SWP\Bundle\CoreBundle\Webhook\WebhookEvents;
use SWP\Bundle\WebhookBundle\Model\WebhookInterface;
use SWP\Component\Common\Serializer\SerializerInterface;
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;

final class PreviewWebhookEventSubscriber extends AbstractWebhookEventSubscriber
{
    /**
     * @var SerializerInterface
     */
    private $serializer;

    public function __construct(
        SerializerInterface $serializer,
        WebhookRepositoryInterface $webhooksRepository,
        TenantContextInterface $tenantContext,
        TenantRepositoryInterface $tenantRepository
    ) {
        $this->serializer = $serializer;

        parent::__construct($webhooksRepository, $tenantContext, $tenantRepository);
    }

    public static function getSubscribedEvents()
    {
        return [
            ArticleEvents::PREVIEW => 'processEvent',
        ];
    }

    public function processEvent(GenericEvent $event, string $dispatcherEventName, EventDispatcherInterface $dispatcher): void
    {
        $subject = $event->getSubject();
        Assert::isInstanceOf($subject, ArticlePreview::class);
        $article = $subject->getArticle();

        $webhooks = $this->getWebhooks($article, WebhookEvents::PREVIEW_EVENT, $dispatcher);
        $headers = [];

        if (!isset($webhooks[0])) {
            return;
        }

        /** @var WebhookInterface $webhook */
        $webhook = $webhooks[0];

        $metadata = [
                'event' => WebhookEvents::PREVIEW_EVENT,
                'tenant' => $webhook->getTenantCode(),
            ];

        foreach ($metadata as $header => $value) {
            $headers['X-WEBHOOK-'.\strtoupper($header)] = $value;
        }

        $client = new Client();
        $requestOptions = [
                'headers' => $headers,
                'body' => $this->serializer->serialize($article, 'json'),
            ];

        /** @var \GuzzleHttp\Psr7\Response $response */
        $response = $client->post($webhook->getUrl(), $requestOptions);
        $content = $response->getBody()->getContents();

        $content = json_decode($content, true);

        if (!isset($content['url'])) {
            return;
        }

        if ($this->isUrlValid($content['url'])) {
            $subject->setPreviewUrl($content['url']);
        }
    }

    private function isUrlValid(string $url): bool
    {
        return false !== filter_var($url, FILTER_VALIDATE_URL);
    }
}<commit_msg>Update PreviewWebhookEventSubscriber.php<commit_after><?php

declare(strict_types=1);

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

namespace SWP\Bundle\CoreBundle\EventSubscriber;

use GuzzleHttp\Client;
use SWP\Bundle\ContentBundle\ArticleEvents;
use SWP\Bundle\CoreBundle\Model\ArticlePreview;
use SWP\Bundle\CoreBundle\Repository\WebhookRepositoryInterface;
use SWP\Bundle\CoreBundle\Webhook\WebhookEvents;
use SWP\Bundle\WebhookBundle\Model\WebhookInterface;
use SWP\Component\Common\Serializer\SerializerInterface;
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;

final class PreviewWebhookEventSubscriber extends AbstractWebhookEventSubscriber
{
    /**
     * @var SerializerInterface
     */
    private $serializer;

    public function __construct(
        SerializerInterface $serializer,
        WebhookRepositoryInterface $webhooksRepository,
        TenantContextInterface $tenantContext,
        TenantRepositoryInterface $tenantRepository
    ) {
        $this->serializer = $serializer;

        parent::__construct($webhooksRepository, $tenantContext, $tenantRepository);
    }

    public static function getSubscribedEvents()
    {
        return [
            ArticleEvents::PREVIEW => 'processEvent',
        ];
    }

    public function processEvent(GenericEvent $event, string $dispatcherEventName, EventDispatcherInterface $dispatcher): void
    {
        $subject = $event->getSubject();
        if (!$subject instanceof ArticlePreview::class) {
            return;
        }
 
        $article = $subject->getArticle();
        $webhooks = $this->getWebhooks($article, WebhookEvents::PREVIEW_EVENT, $dispatcher);
        $headers = [];

        if (!isset($webhooks[0])) {
            return;
        }

        /** @var WebhookInterface $webhook */
        $webhook = $webhooks[0];

        $metadata = [
            'event' => WebhookEvents::PREVIEW_EVENT,
            'tenant' => $webhook->getTenantCode(),
        ];

        foreach ($metadata as $header => $value) {
            $headers['X-WEBHOOK-'.\strtoupper($header)] = $value;
        }

        $client = new Client();
        $requestOptions = [
            'headers' => $headers,
            'body' => $this->serializer->serialize($article, 'json'),
        ];

        /** @var \GuzzleHttp\Psr7\Response $response */
        $response = $client->post($webhook->getUrl(), $requestOptions);
        $content = $response->getBody()->getContents();

        $content = json_decode($content, true);

        if (!isset($content['url'])) {
            return;
        }

        if ($this->isUrlValid($content['url'])) {
            $subject->setPreviewUrl($content['url']);
        }
    }

    private function isUrlValid(string $url): bool
    {
        return false !== filter_var($url, FILTER_VALIDATE_URL);
    }
}
{'fraction_non_alphanumeric': 0.11953089760938204, 'fraction_numerical': 0.0036084799278304014, 'mean_word_length': 3.073484384568279, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '5219772', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1807, 'n_words': 358}