<commit_before><?php

namespace Kunstmaan\SearchBundle\Provider;

use Elastica\Client;
use Elastica\Document;
use Elastica\Index;

class ElasticaProvider implements SearchProviderInterface
{
    /** @var Client The Elastica client */
    private $client;

    /** @var array An array of Elastica search nodes (each item in the array needs a host and port) */
    private $nodes = array();

    /**
     * @return Client
     */
    public function getClient()
    {
        if (!$this->client instanceof Client) {
            $this->client = new Client(
                array('connections' => $this->nodes,
                )
            );

            //NEXT_MAJOR: remove checks and update ruflin/elastica dependency constraints
            if (!class_exists(\Elastica\Mapping::class)) {
                @trigger_error('Using a version of ruflin/elastica below v7.0 is deprecated since KunstmaanSearchBundle 5.6 and support for older versions will be removed in KunstmaanSearchBundle 6.0. Upgrade to ruflin/elastica and elasticsearch v7 instead.', E_USER_DEPRECATED);
            }
        }

        return $this->client;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'Elastica';
    }

    /**
     * @param string $indexName
     *
     * @return \Elastica\Index
     */
    public function createIndex($indexName)
    {
        return new Index($this->getClient(), $indexName);
    }

    /**
     * @param string $indexName
     *
     * @return \Elastica\Index
     */
    public function getIndex($indexName)
    {
        return $this->getClient()->getIndex($indexName);
    }

    /**
     * @param string $uid
     * @param array  $document
     * @param string $indexName
     * @param string $indexType
     *
     * @return \Elastica\Document
     */
    public function createDocument($uid, $document, $indexName = '', $indexType = '')
    {
        if (method_exists(Document::class, 'setType')) {
            return new Document($uid, $document, $indexType, $indexName);
        }

        return new Document($uid, $document, $indexName);
    }

    /**
     * @param string $indexName
     * @param string $indexType
     * @param array  $document
     * @param string $uid
     *
     * @return \Elastica\Response<commit_msg>[SearchBundle] Ignore phpstan error line until deprecations are cleaned<commit_after><?php

namespace Kunstmaan\SearchBundle\Provider;

use Elastica\Client;
use Elastica\Document;
use Elastica\Index;

class ElasticaProvider implements SearchProviderInterface
{
    /** @var Client The Elastica client */
    private $client;

    /** @var array An array of Elastica search nodes (each item in the array needs a host and port) */
    private $nodes = array();

    /**
     * @return Client
     */
    public function getClient()
    {
        if (!$this->client instanceof Client) {
            $this->client = new Client(
                array('connections' => $this->nodes,
                )
            );

            //NEXT_MAJOR: remove checks and update ruflin/elastica dependency constraints
            if (!class_exists(\Elastica\Mapping::class)) {
                @trigger_error('Using a version of ruflin/elastica below v7.0 is deprecated since KunstmaanSearchBundle 5.6 and support for older versions will be removed in KunstmaanSearchBundle 6.0. Upgrade to ruflin/elastica and elasticsearch v7 instead.', E_USER_DEPRECATED);
            }
        }

        return $this->client;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'Elastica';
    }

    /**
     * @param string $indexName
     *
     * @return \Elastica\Index
     */
    public function createIndex($indexName)
    {
        return new Index($this->getClient(), $indexName);
    }

    /**
     * @param string $indexName
     *
     * @return \Elastica\Index
     */
    public function getIndex($indexName)
    {
        return $this->getClient()->getIndex($indexName);
    }

    /**
     * @param string $uid
     * @param array  $document
     * @param string $indexName
     * @param string $indexType
     *
     * @return \Elastica\Document
     */
    public function createDocument($uid, $document, $indexName = '', $indexType = '')
    {
        if (method_exists(Document::class, 'setType')) {
            // @phpstan-ignore-next-line
            return new Document($uid, $document, $indexType, $indexName);
        }

        return new Document($uid, $document, $indexName);
    }

    /**
     * @param string $indexName
     * @param string $indexType
     * @param array  $document
     * @param string $uid
     *
     * @return \Elastica\Response
{'fraction_non_alphanumeric': 0.11426135152419527, 'fraction_numerical': 0.002984438286079727, 'mean_word_length': 2.2225274725274726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5625542', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1327, 'n_words': 358}