<commit_before><?php

namespace Oro\Bundle\EmailBundle\Provider;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Oro\Bundle\EmailBundle\Event\EmailRecipientsLoadEvent;

class EmailRecipientsProvider
{
    /** @var EventDispatcherInterface */
    protected $dispatcher;

    /**
     * @param EventDispatcherInterface $dispatcher
     */
    public function __construct(EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    /**
     * @param object|null $relatedEntity
     * @param string|null $query
     * @param int $limit
     *
     * @return array
     */
    public function getEmailRecipients($relatedEntity = null, $query = null, $limit = 100)
    {
        if (!$this->dispatcher->hasListeners(EmailRecipientsLoadEvent::NAME)) {
            return [];
        }

        $event = new EmailRecipientsLoadEvent($relatedEntity, $query, $limit);
        $this->dispatcher->dispatch(EmailRecipientsLoadEvent::NAME, $event);

        return $event->getResults();
    }
}<commit_msg>CRM-3533: Add autocomplete for to, cc, bcc fields with possibility to type new email address<commit_after><?php

namespace Oro\Bundle\EmailBundle\Provider;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Oro\Bundle\EmailBundle\Event\EmailRecipientsLoadEvent;

class EmailRecipientsProvider
{
    /** @var EventDispatcherInterface */
    protected $dispatcher;

    /**
     * @param EventDispatcherInterface $dispatcher
     */
    public function __construct(EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    /**
     * @param object|null $relatedEntity
     * @param string|null $query
     * @param int $limit
     *
     * @return array
     */
    public function getEmailRecipients($relatedEntity = null, $query = null, $limit = 100)
    {
        if (!$this->dispatcher->hasListeners(EmailRecipientsLoadEvent::NAME)) {
            return [];
        }

        $event = new EmailRecipientsLoadEvent($relatedEntity, $query, $limit);
        $this->dispatcher->dispatch(EmailRecipientsLoadEvent::NAME, $event);

        return $this->valuesAsKeys($event->getResults());
    }

    /**
     * @param array $data
     *
     * @return array
     */
    public function valuesAsKeys(array $data)
    {
        foreach ($data as $key => $record) {
            if (isset($record['children'])) {
                $data[$key]['children'] = $this->valuesAsKeys($record['children']);
            } else {
                $data[$key]['id'] = $record['text'];
            }
        }

        return $data;
    }
}
{'fraction_non_alphanumeric': 0.12571428571428572, 'fraction_numerical': 0.0038095238095238095, 'mean_word_length': 2.8335766423357662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '1563921', 'n_tokens_mistral': 761, 'n_tokens_neox': 722, 'n_words': 143}