<commit_before><?php

namespace Nuwber\Events\Amqp;

use Enqueue\AmqpLib\AmqpConnectionFactory;
use Enqueue\AmqpLib\AmqpContext;
use Enqueue\AmqpTools\DelayStrategy;
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
use Illuminate\Support\Collection;
use Nuwber\Events\Queue\Context;
use Interop\Queue\Topic;

class Connection
{
    /**
     * @var Collection
     */
    private $config;

    /**
     * @var DelayStrategy
     */
    private $delayStrategy;

    /**
     * @var AmqpConnectionFactory
     */
    private $connection;

    public function __construct(array $config)
    {
        $this->config = collect($config);
    }

    /**
     * @return AmqpConnectionFactory
     */
    public function connect(): AmqpConnectionFactory
    {
        if (!$this->connection) {
            $this->connection = $this->makeFactory();
        }

        return $this->connection;
    }

    /**
     * @return Context
     */
    public function createContext(): Context
    {
        $context = $this->connect()->createContext();

        return new Context($context, $this->makeTopic($context));
    }

    protected function makeTopic(AmqpContext $context): Topic
    {
        return (new TopicFactory($context))->make($this->config->get('exchange'));
    }

    /**
     * @return AmqpConnectionFactory
     */
    protected function makeFactory(): AmqpConnectionFactory
    {
        $factory = new AmqpConnectionFactory([
            'dsn' => $this->config->get('dsn'),
            'host' => $this->config->get('host', '127.0.0.1'),
            'port' => $this->config->get('port', 5672),
            'user' => $this->config->get('user', 'guest'),
            'pass' => $this->config->get('pass', '<PASSWORD>'),
            'vhost' => $this->config->get('vhost', '/'),
            'ssl_on' => $this->config->get('ssl.is_enabled', false),
            'ssl_verify' => $this->config->get('ssl.verify_peer', true),
            'ssl_cacert' => $this->config->get('ssl.cafile'),
            'ssl_cert' => $this->config->get('ssl.local_cert'),
            'ssl_key' => $this->config->get('ssl.local_key'),
            'ssl_passphrase' => $this->config->get('ssl.passphrase'),
        ]);

        $factory->setDelayStrategy($this->getDelayStrategy());

        return $factory;
    }

    /**
     * @param DelayStrategy $strategy<commit_msg>fix: properly load SSL configuration (#77)<commit_after><?php

namespace Nuwber\Events\Amqp;

use Enqueue\AmqpLib\AmqpConnectionFactory;
use Enqueue\AmqpLib\AmqpContext;
use Enqueue\AmqpTools\DelayStrategy;
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
use Illuminate\Support\Collection;
use Nuwber\Events\Queue\Context;
use Interop\Queue\Topic;

class Connection
{
    /**
     * @var Collection
     */
    private $config;

    /**
     * @var DelayStrategy
     */
    private $delayStrategy;

    /**
     * @var AmqpConnectionFactory
     */
    private $connection;

    public function __construct(array $config)
    {
        $this->config = collect($config);
    }

    /**
     * @return AmqpConnectionFactory
     */
    public function connect(): AmqpConnectionFactory
    {
        if (!$this->connection) {
            $this->connection = $this->makeFactory();
        }

        return $this->connection;
    }

    /**
     * @return Context
     */
    public function createContext(): Context
    {
        $context = $this->connect()->createContext();

        return new Context($context, $this->makeTopic($context));
    }

    protected function makeTopic(AmqpContext $context): Topic
    {
        return (new TopicFactory($context))->make($this->config->get('exchange'));
    }

    /**
     * @return AmqpConnectionFactory
     */
    protected function makeFactory(): AmqpConnectionFactory
    {
        $sslConfig = collect($this->config->get('ssl', []));
        $factory = new AmqpConnectionFactory([
            'dsn' => $this->config->get('dsn'),
            'host' => $this->config->get('host', '127.0.0.1'),
            'port' => $this->config->get('port', 5672),
            'user' => $this->config->get('user', 'guest'),
            'pass' => $this->config->get('pass', '<PASSWORD>'),
            'vhost' => $this->config->get('vhost', '/'),
            'ssl_on' => $sslConfig->get('is_enabled', false),
            'ssl_verify' => $sslConfig->get('verify_peer', true),
            'ssl_cacert' => $sslConfig->get('cafile'),
            'ssl_cert' => $sslConfig->get('local_cert'),
            'ssl_key' => $sslConfig->get('local_key'),
            'ssl_passphrase' => $sslConfig->get('passphrase'),
        ]);

        $factory->setDelayStrategy($this->getDelayStrategy());
        
        return $factory;
    }

    /**
     * @param DelayStrategy $strategy
{'fraction_non_alphanumeric': 0.16842327150084316, 'fraction_numerical': 0.00463743676222597, 'mean_word_length': 2.525260029717682, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6779620', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1403, 'n_words': 234}