<commit_before><?php
namespace Knit\Store\DoctrineDBAL;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;

use MD\Foundation\Debug\Timer;

use Knit\Criteria\CriteriaExpression;
use Knit\Exceptions\StoreConnectionFailedException;
use Knit\Exceptions\StoreQueryErrorException;
use Knit\Store\DoctrineDBAL\CriteriaParser;
use Knit\Store\StoreInterface;
use Knit\Knit;

/**
 * Persistent store based on Doctrine DBAL that supports MySQL, PostgreSQL and more.
 *
 * @package    Knit
 * @subpackage Store\DoctrineDBAL
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME>
 * @license    https://github.com/michaldudek/Knit/blob/master/LICENSE.md MIT License
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Store implements StoreInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * Database connection.
     *
     * @var Connection
     */
    protected $connection;

    /**
     * DoctrineDBAL specific criteria parser.
     *
     * @var CriteriaParser
     */
    protected $criteriaParser;

    /**
     * Constructor.
     *
     * Creates database connection using Doctrine's `Connection` class based on passed `$config`.
     * See {@link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html} for
     * details on how to configure a connection.
     *
     * @param array                $config         Connection configuration. Passed directory to `Connection`.
     * @param CriteriaParser       $criteriaParser Knit criteria parser.
     * @param LoggerInterface|null $logger         [optional] Logger.
     */
    public function __construct(array $config, CriteriaParser $criteriaParser, LoggerInterface $logger = null)
    {
        try {
            $this->connection = DriverManager::getConnection($config, new Configuration());
            $this->connection->connect();
        } catch (\Exception $e) {
            throw new StoreConnectionFailedException('DoctrineDBAL: '. $e->getMessage(), $e->getCode(), $e);
        }

        $this->criteriaParser = $criteriaParser;
        $this->logger = $logger ? $logger : new NullLogger();
    }

    /**
     * Finds items within a collection.
     *
     * @param string $collection Name of the collection in which to look for.
     * @param CriteriaExpression $criteria [optional] Lookup criteria.
     * @param array $params [optional] Other parameters, like `orderBy` or `orderDir`.
     *
     * @return array
     */
    public function find($collection, CriteriaExpression $criteria = null, array $params = [])<commit_msg>Allow to pass an existing doctrine connection to Doctrine DBAL store.<commit_after><?php
namespace Knit\Store\DoctrineDBAL;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;

use MD\Foundation\Debug\Timer;

use Knit\Criteria\CriteriaExpression;
use Knit\Exceptions\StoreConnectionFailedException;
use Knit\Exceptions\StoreQueryErrorException;
use Knit\Store\DoctrineDBAL\CriteriaParser;
use Knit\Store\StoreInterface;
use Knit\Knit;

/**
 * Persistent store based on Doctrine DBAL that supports MySQL, PostgreSQL and more.
 *
 * @package    Knit
 * @subpackage Store\DoctrineDBAL
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME>
 * @license    https://github.com/michaldudek/Knit/blob/master/LICENSE.md MIT License
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Store implements StoreInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * Database connection.
     *
     * @var Connection
     */
    protected $connection;

    /**
     * DoctrineDBAL specific criteria parser.
     *
     * @var CriteriaParser
     */
    protected $criteriaParser;

    /**
     * Constructor.
     *
     * Creates database connection using Doctrine's `Connection` class based on passed `$config`.
     * See {@link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html} for
     * details on how to configure a connection.
     *
     * @param array                $config         Connection configuration. Passed directory to `Connection`.
     * @param CriteriaParser       $criteriaParser Knit criteria parser.
     * @param LoggerInterface|null $logger         [optional] Logger.
     * @param Connection|null      $connection     [optional] Existing Doctrine DBAL connection if you already have one.
     */
    public function __construct(
        array $config,
        CriteriaParser $criteriaParser,
        LoggerInterface $logger = null,
        Connection $connection = null
    ) {
        try {
            $this->connection = $connection ? $connection : DriverManager::getConnection($config, new Configuration());
            $this->connection->connect();
        } catch (\Exception $e) {
            throw new StoreConnectionFailedException('DoctrineDBAL: '. $e->getMessage(), $e->getCode(), $e);
        }

        $this->criteriaParser = $criteriaParser;
        $this->logger = $logger ? $logger : new NullLogger();
    }

    /**
     * Finds items within a collection.
     *
     * @param string $collection Name of the collection in which to look for.
     * @param CriteriaExpression $criteria [optional] Lookup criteria.
     * @param array $params [optional] Other parameters, like `orderBy` or `orderDir`.
     *
     * @return array
     */
    public function find($collection, CriteriaExpression $criteria = null, array $params = [])
{'fraction_non_alphanumeric': 0.10594989561586639, 'fraction_numerical': 0.0013917884481558804, 'mean_word_length': 3.1034975017844397, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '2341876', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1592, 'n_words': 421}