<commit_before><?php
/**
 * Webiny (http://www.webiny.com/)
 *
 * @link      http://www.webiny.com/ for the canonical source repository
 * @copyright Copyright (c) 2009-2015 Webiny LTD. (http://www.webiny.com)
 * @license   MIT
 */

namespace Webiny\AnalyticsDb\Query;

use Webiny\AnalyticsDb\AnalyticsDb;
use Webiny\AnalyticsDb\AnalyticsDbException;
use Webiny\Component\Mongo\Mongo;

/**
 * Class QueryBuilder
 * @package Webiny\AnalyticsDb
 */
abstract class AbstractQuery
{
    /**
     * @var Mongo
     */
    protected $mongo;

    /**
     * @var string Entity name.
     */
    protected $entity;

    /**
     * @var string|int Referral value.
     */
    protected $ref;

    /**
     * @var array Date range.
     */
    protected $dateRange;

    /**
     * @var array Mongo pipeline array.
     */
    protected $pipeline;

    /**
     * @var string Name of the mongo collection.
     */
    protected $collection;

    /**
     * @var string Name of the current id field.
     */
    protected $id;


    /**
     * This is the callback method that is triggered after the constructor call.
     *
     * @return void
     */
    abstract protected function populate();

    /**
     * Base constructor.
     *
     * @param Mongo      $mongo
     * @param string     $entity
     * @param string|int $ref
     * @param array      $dateRange
     */
    public function __construct(Mongo $mongo, $entity, $ref, array $dateRange)
    {
        $this->mongo = $mongo;
        $this->entity = $entity;
        $this->ref = $ref;
        $this->dateRange = $dateRange;

        $this->populate();
    }

    /**
     * Set the result limit and offset.
     *
     * @param int      $limit How many results to return.
     * @param int|null $skip  How many results to skip.
     *
     * @return $this
     */
    public function limit($limit, $skip = null)
    {
        $this->pipeline['$limit'] = $limit;
        if (!empty($skip)) {
            $this->pipeline['$skip'] = $skip;
        }

        return $this;
    }

    /**
     * Returns the query for mongo shell. Use to debug your queries.
     *
     * @return string
     */
    public function getMongoQuery()
    {
        return 'db.' . $this->getCollectionName() . '.aggregate(' . json_encode($this->getPipeline()) . ');';
    }

    /**
     * This method does your query lookup and returns the result in form of an array.
     * In case if there are no records to return, false is returned.
     *
     * @return bool
     */
    public function getResult()
    {
        $result = $this->mongo->aggregate($this->getCollectionName(), $this->getPipeline());
        $result = $result->toArray();

        if (is_array($result) && isset($result['ok']) && $result['ok'] == 1) {
            return $result['result'];
        }

        return false;
    }

    /**
     * Sorts the result by timestamp.<commit_msg>Update AbstractQuery.php<commit_after><?php
/**
 * Webiny (http://www.webiny.com/)
 *
 * @link      http://www.webiny.com/ for the canonical source repository
 * @copyright Copyright (c) 2009-2015 Webiny LTD. (http://www.webiny.com)
 * @license   MIT
 */

namespace Webiny\AnalyticsDb\Query;

use Webiny\AnalyticsDb\AnalyticsDb;
use Webiny\AnalyticsDb\AnalyticsDbException;
use Webiny\Component\Mongo\Mongo;

/**
 * Class QueryBuilder
 * @package Webiny\AnalyticsDb
 */
abstract class AbstractQuery
{
    /**
     * @var Mongo
     */
    protected $mongo;

    /**
     * @var string Entity name.
     */
    protected $entity;

    /**
     * @var string|int Referral value.
     */
    protected $ref;

    /**
     * @var array Date range.
     */
    protected $dateRange;

    /**
     * @var array Mongo pipeline array.
     */
    protected $pipeline;

    /**
     * @var string Name of the mongo collection.
     */
    protected $collection;

    /**
     * @var string Name of the current id field.
     */
    protected $id;


    /**
     * This is the callback method that is triggered after the constructor call.
     *
     * @return void
     */
    abstract protected function populate();

    /**
     * Base constructor.
     *
     * @param Mongo      $mongo
     * @param string     $entity
     * @param string|int $ref
     * @param array      $dateRange
     */
    public function __construct(Mongo $mongo, $entity, $ref, array $dateRange)
    {
        $this->mongo = $mongo;
        $this->entity = $entity;
        $this->ref = $ref;
        $this->dateRange = $dateRange;

        $this->populate();
    }

    /**
     * Set the result limit and offset.
     *
     * @param int      $limit How many results to return.
     * @param int|null $skip  How many results to skip.
     *
     * @return $this
     */
    public function limit($limit, $skip = null)
    {
        $this->pipeline['$limit'] = $limit;
        if (!empty($skip)) {
            $this->pipeline['$skip'] = $skip;
        }

        return $this;
    }

    /**
     * Returns the query for mongo shell. Use to debug your queries.
     *
     * @return string
     */
    public function getMongoQuery()
    {
        return 'db.' . $this->getCollectionName() . '.aggregate(' . json_encode($this->getPipeline()) . ');';
    }
    
     /**
     * This method does your query lookup and returns the result in form of an array.
     * In case if there are no records to return, false is returned.
     *
     * @return bool
     */
    public function getResult()
    {
        $result = $this->mongo->aggregate($this->getCollectionName(), $this->getPipeline());
        return $result->toArray();
    }

    /**
     * Sorts the result by timestamp.
{'fraction_non_alphanumeric': 0.13533167789996453, 'fraction_numerical': 0.0030152536360411493, 'mean_word_length': 1.9820200951877314, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '3946294', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1699, 'n_words': 471}