<commit_before><?php

namespace Ekyna\Bundle\CommerceBundle\DependencyInjection;

use Ekyna\Bundle\CommerceBundle\Service\Document\DocumentHelper;
use Ekyna\Bundle\CommerceBundle\Service\Document\DocumentPageBuilder;
use Ekyna\Bundle\CommerceBundle\Service\Document\PdfGenerator;
use Ekyna\Bundle\CommerceBundle\Service\Document\RendererFactory;
use Ekyna\Bundle\CommerceBundle\Service\Widget\WidgetHelper;
use Ekyna\Bundle\CommerceBundle\Service\Widget\WidgetRenderer;
use Ekyna\Bundle\CommerceBundle\Service\Stock\AvailabilityHelper;
use Ekyna\Bundle\ResourceBundle\DependencyInjection\AbstractExtension;
use Ekyna\Component\Commerce\Bridge\Doctrine\DependencyInjection\DoctrineBundleMapping;
use Ekyna\Component\Commerce\Bridge\Mailchimp;
use Ekyna\Component\Commerce\Bridge\SendInBlue;
use Ekyna\Component\Commerce\Cart;
use Ekyna\Component\Commerce\Customer;
use Ekyna\Component\Commerce\Features;
use Ekyna\Component\Commerce\Order;
use Ekyna\Component\Commerce\Pricing\Api;
use Ekyna\Component\Commerce\Quote;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;

/**
 * Class EkynaCommerceExtension
 * @package Ekyna\Bundle\CommerceBundle\DependencyInjection
 * @author  <NAME> <<EMAIL>>
 */
class EkynaCommerceExtension extends AbstractExtension
{
    /**
     * @inheritDoc
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $config = $this->configure($configs, 'ekyna_commerce', new Configuration(), $container);

        $this->configureAccounting($config['accounting'], $container);
        $this->configureCache($config['cache'], $container);
        $this->configureDefaults($config['default'], $container);
        $this->configurePdf($config['pdf'], $container);
        $this->configureDocument($config['document'], $container);
        $this->configureFeatures($config['feature'], $container);
        $this->configurePricing($config['pricing'], $container);
        $this->configureStock($config['stock'], $container);
        $this->configureWidget($config['widget'], $container);
        $this->configureSaleFactory($container);

        if (in_array($container->getParameter('kernel.environment'), ['dev', 'test'], true)) {
            $this->loader->load('services_dev_test.xml');
        }
    }

    /**
     * Configures the defaults.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureDefaults(array $config, ContainerBuilder $container)
    {
        $container->setParameter('ekyna_commerce.default.company_logo', $config['company_logo']);
        $container->setParameter('ekyna_commerce.default.country', $config['country']);
        $container->setParameter('ekyna_commerce.default.currency', $config['currency']);
        $container->setParameter('ekyna_commerce.default.vat_display_mode', $config['vat_display_mode']);
        $container->setParameter('ekyna_commerce.default.fraud', $config['fraud']);
        $container->setParameter('ekyna_commerce.default.expiration.cart', $config['expiration']['cart']);
        $container->setParameter('ekyna_commerce.default.expiration.quote', $config['expiration']['quote']);
        $container->setParameter('ekyna_commerce.default.notify', $config['notify']);
    }

    /**
     * Configures the cache.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureCache(array $config, ContainerBuilder $container)
    {
        $container->setParameter('ekyna_commerce.cache.countries', $config['countries']);
    }

    /**
     * Configures accounting.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureAccounting(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition('ekyna_commerce.accounting.exporter')
            ->replaceArgument(8, $config);
    }

    /**
     * Configures PDF (generator).
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configurePdf(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition(PdfGenerator::class)
            ->setArguments([$config['entry_point'], $config['token']]);
    }

    /**
     * Configures document.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureDocument(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition(DocumentHelper::class)
            ->replaceArgument(6, array_replace([
                'logo_path' => '%ekyna_commerce.default.company_logo%',
            ], $config));

        $container
            ->getDefinition(DocumentPageBuilder::class)
            ->replaceArgument(2, $config);

        $container
            ->getDefinition(RendererFactory::class)
            ->replaceArgument(2, [
                'shipment_remaining_date' => $config['shipment_remaining_date'],
                'debug'                   => '%kernel.debug%',
            ]);
    }

    /**
     * Configures the features.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureFeatures(array $config, ContainerBuilder $container)
    {
        // Set features parameter
        $container->setParameter('ekyna_commerce.features', $config);
        // Set service config
        $container->getDefinition(Features::class)->replaceArgument(0, $config);

        if ($config[Features::NEWSLETTER]['enabled']) {
            $this->loader->load('services/newsletter.xml');

            $this->configureMailchimp($config[Features::NEWSLETTER]['mailchimp'], $container);
            $this->configureSendInBlue($config[Features::NEWSLETTER]['sendinblue'], $container);
        }
    }

    /**
     * Configures mailchimp newsletter gateway.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureMailchimp(array $config, ContainerBuilder $container): void
    {
        if (empty($config['api_key'])) {
            return;
        }

        if (!class_exists('DrewM\\MailChimp\\MailChimp')) {
            throw new LogicException(
                "To use MailChimp newsletter gateway, you must install drewm/mailchimp-api first.\n" .
                "Please run: composer require drewm/mailchimp-api"
            );
        }

        $this->loader->load('services/newsletter/mailchimp.xml');

        $container
            ->getDefinition(Mailchimp\Api::class)
            ->replaceArgument(1, $config['api_key']);
    }

    /**
     * Configures sendInBlue newsletter gateway.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureSendInBlue(array $config, ContainerBuilder $container): void
    {
        if (empty($config['api_key'])) {
            return;
        }

        if (!class_exists('SendinBlue\\Client\\Configuration')) {
            throw new LogicException(
                "To use SendInBlue newsletter gateway, you must install sendinblue/api-v3-sdk first.\n" .
                "Please run: composer require sendinblue/api-v3-sdk \"6.x.x\""
            );
        }

        $this->loader->load('services/newsletter/sendinblue.xml');

        $container
            ->getDefinition(SendInBlue\Api::class)<commit_msg>[Commerce] Fixed version of newsletter gateway in exceptions messages.<commit_after><?php

namespace Ekyna\Bundle\CommerceBundle\DependencyInjection;

use Ekyna\Bundle\CommerceBundle\Service\Document\DocumentHelper;
use Ekyna\Bundle\CommerceBundle\Service\Document\DocumentPageBuilder;
use Ekyna\Bundle\CommerceBundle\Service\Document\PdfGenerator;
use Ekyna\Bundle\CommerceBundle\Service\Document\RendererFactory;
use Ekyna\Bundle\CommerceBundle\Service\Widget\WidgetHelper;
use Ekyna\Bundle\CommerceBundle\Service\Widget\WidgetRenderer;
use Ekyna\Bundle\CommerceBundle\Service\Stock\AvailabilityHelper;
use Ekyna\Bundle\ResourceBundle\DependencyInjection\AbstractExtension;
use Ekyna\Component\Commerce\Bridge\Doctrine\DependencyInjection\DoctrineBundleMapping;
use Ekyna\Component\Commerce\Bridge\Mailchimp;
use Ekyna\Component\Commerce\Bridge\SendInBlue;
use Ekyna\Component\Commerce\Cart;
use Ekyna\Component\Commerce\Customer;
use Ekyna\Component\Commerce\Features;
use Ekyna\Component\Commerce\Order;
use Ekyna\Component\Commerce\Pricing\Api;
use Ekyna\Component\Commerce\Quote;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;

/**
 * Class EkynaCommerceExtension
 * @package Ekyna\Bundle\CommerceBundle\DependencyInjection
 * @author  <NAME> <<EMAIL>>
 */
class EkynaCommerceExtension extends AbstractExtension
{
    /**
     * @inheritDoc
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $config = $this->configure($configs, 'ekyna_commerce', new Configuration(), $container);

        $this->configureAccounting($config['accounting'], $container);
        $this->configureCache($config['cache'], $container);
        $this->configureDefaults($config['default'], $container);
        $this->configurePdf($config['pdf'], $container);
        $this->configureDocument($config['document'], $container);
        $this->configureFeatures($config['feature'], $container);
        $this->configurePricing($config['pricing'], $container);
        $this->configureStock($config['stock'], $container);
        $this->configureWidget($config['widget'], $container);
        $this->configureSaleFactory($container);

        if (in_array($container->getParameter('kernel.environment'), ['dev', 'test'], true)) {
            $this->loader->load('services_dev_test.xml');
        }
    }

    /**
     * Configures the defaults.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureDefaults(array $config, ContainerBuilder $container)
    {
        $container->setParameter('ekyna_commerce.default.company_logo', $config['company_logo']);
        $container->setParameter('ekyna_commerce.default.country', $config['country']);
        $container->setParameter('ekyna_commerce.default.currency', $config['currency']);
        $container->setParameter('ekyna_commerce.default.vat_display_mode', $config['vat_display_mode']);
        $container->setParameter('ekyna_commerce.default.fraud', $config['fraud']);
        $container->setParameter('ekyna_commerce.default.expiration.cart', $config['expiration']['cart']);
        $container->setParameter('ekyna_commerce.default.expiration.quote', $config['expiration']['quote']);
        $container->setParameter('ekyna_commerce.default.notify', $config['notify']);
    }

    /**
     * Configures the cache.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureCache(array $config, ContainerBuilder $container)
    {
        $container->setParameter('ekyna_commerce.cache.countries', $config['countries']);
    }

    /**
     * Configures accounting.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureAccounting(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition('ekyna_commerce.accounting.exporter')
            ->replaceArgument(8, $config);
    }

    /**
     * Configures PDF (generator).
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configurePdf(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition(PdfGenerator::class)
            ->setArguments([$config['entry_point'], $config['token']]);
    }

    /**
     * Configures document.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureDocument(array $config, ContainerBuilder $container)
    {
        $container
            ->getDefinition(DocumentHelper::class)
            ->replaceArgument(6, array_replace([
                'logo_path' => '%ekyna_commerce.default.company_logo%',
            ], $config));

        $container
            ->getDefinition(DocumentPageBuilder::class)
            ->replaceArgument(2, $config);

        $container
            ->getDefinition(RendererFactory::class)
            ->replaceArgument(2, [
                'shipment_remaining_date' => $config['shipment_remaining_date'],
                'debug'                   => '%kernel.debug%',
            ]);
    }

    /**
     * Configures the features.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureFeatures(array $config, ContainerBuilder $container)
    {
        // Set features parameter
        $container->setParameter('ekyna_commerce.features', $config);
        // Set service config
        $container->getDefinition(Features::class)->replaceArgument(0, $config);

        if ($config[Features::NEWSLETTER]['enabled']) {
            $this->loader->load('services/newsletter.xml');

            $this->configureMailchimp($config[Features::NEWSLETTER]['mailchimp'], $container);
            $this->configureSendInBlue($config[Features::NEWSLETTER]['sendinblue'], $container);
        }
    }

    /**
     * Configures mailchimp newsletter gateway.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureMailchimp(array $config, ContainerBuilder $container): void
    {
        if (empty($config['api_key'])) {
            return;
        }

        if (!class_exists('DrewM\\MailChimp\\MailChimp')) {
            throw new LogicException(
                "To use MailChimp newsletter gateway, you must install drewm/mailchimp-api first.\n" .
                "Please run: composer require drewm/mailchimp-api:^2.5"
            );
        }

        $this->loader->load('services/newsletter/mailchimp.xml');

        $container
            ->getDefinition(Mailchimp\Api::class)
            ->replaceArgument(1, $config['api_key']);
    }

    /**
     * Configures sendInBlue newsletter gateway.
     *
     * @param array            $config
     * @param ContainerBuilder $container
     */
    private function configureSendInBlue(array $config, ContainerBuilder $container): void
    {
        if (empty($config['api_key'])) {
            return;
        }

        if (!class_exists('SendinBlue\\Client\\Configuration')) {
            throw new LogicException(
                "To use SendInBlue newsletter gateway, you must install sendinblue/api-v3-sdk first.\n" .
                "Please run: composer require sendinblue/api-v3-sdk:^6.4"
            );
        }

        $this->loader->load('services/newsletter/sendinblue.xml');

        $container
            ->getDefinition(SendInBlue\Api::class)
{'fraction_non_alphanumeric': 0.13059480775550444, 'fraction_numerical': 0.001380216891225764, 'mean_word_length': 2.9718089271730617, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3824446', 'n_tokens_mistral': 4078, 'n_tokens_neox': 3980, 'n_words': 662}