<commit_before><?php

namespace AppBundle\API\Listing;

use AppBundle\API\ORMWebservice;
use AppBundle\User\FennecUser;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\ParameterBag;

class Overview extends ORMWebservice
{
    /**
     * @var EntityManager
     */
    private $manager;

    /**
     * @inheritdoc
     */
    public function execute(ParameterBag $query, FennecUser $user = null){
        $this->manager = $this->getManagerFromQuery($query);
        $result = array();
        $result['projects'] = $this->get_number_of_projects($user);
        $result['organisms'] = $this->get_number_of_organisms();
        $result['trait_entries'] = $this->get_number_of_trait_entries();
        $result['trait_types'] = $this->get_number_of_trait_types();
        return $result;
    }

    /**
     * @param $user FennecUser
     * @return int number_of_projects
     */
    private function get_number_of_projects($user){
        if ($user === null) {
            return 0;
        }
        $query_get_user_projects = <<<EOF
SELECT
    COUNT(*)
    FROM full_webuser_data WHERE provider = :provider AND oauth_id = :oauth_id
EOF;
        $stm_get_user_projects = $this->manager->getConnection()->prepare($query_get_user_projects);
        $stm_get_user_projects->bindValue('provider', $user->getProvider());
        $stm_get_user_projects->bindValue('oauth_id', $user->getId());
        $stm_get_user_projects->execute();
        $row = $stm_get_user_projects->fetch(\PDO::FETCH_ASSOC);
        return $row['count'];
    }

    private function get_number_of_organisms(){
        $query_get_number_of_organisms = <<<EOF
SELECT
    COUNT(*)
    FROM organism
EOF;
        $stm_get_number_of_organisms = $this->manager->getConnection()->prepare($query_get_number_of_organisms);
        $stm_get_number_of_organisms->execute();
        $row = $stm_get_number_of_organisms->fetch(\PDO::FETCH_ASSOC);
        return $row['count'];
    }

    private function get_number_of_trait_entries(){
        $query_get_number_of_trait_entries = <<<EOF
SELECT
    COUNT(*)
    FROM trait_categorical_entry
    WHERE deletion_date IS NULL
EOF;
        $stm_get_number_of_trait_entries = $this->manager->getConnection()->prepare($query_get_number_of_trait_entries);
        $stm_get_number_of_trait_entries->execute();
        $row = $stm_get_number_of_trait_entries->fetch(\PDO::FETCH_ASSOC);
        return $row['count'];
    }<commit_msg>Replace raw sql with orm version<commit_after><?php

namespace AppBundle\API\Listing;

use AppBundle\API\ORMWebservice;
use AppBundle\User\FennecUser;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\ParameterBag;

class Overview extends ORMWebservice
{
    /**
     * @var EntityManager
     */
    private $manager;

    /**
     * @inheritdoc
     */
    public function execute(ParameterBag $query, FennecUser $user = null){
        $this->manager = $this->getManagerFromQuery($query);
        $result = array();
        $result['projects'] = $this->get_number_of_projects($user);
        $result['organisms'] = $this->get_number_of_organisms();
        $result['trait_entries'] = $this->get_number_of_trait_entries();
        $result['trait_types'] = $this->get_number_of_trait_types();
        return $result;
    }

    /**
     * @param $user FennecUser
     * @return int number_of_projects
     */
    private function get_number_of_projects($user){
        if ($user === null) {
            return 0;
        }
        $query_get_user_projects = <<<EOF
SELECT
    COUNT(*)
    FROM full_webuser_data WHERE provider = :provider AND oauth_id = :oauth_id
EOF;
        $stm_get_user_projects = $this->manager->getConnection()->prepare($query_get_user_projects);
        $stm_get_user_projects->bindValue('provider', $user->getProvider());
        $stm_get_user_projects->bindValue('oauth_id', $user->getId());
        $stm_get_user_projects->execute();
        $row = $stm_get_user_projects->fetch(\PDO::FETCH_ASSOC);
        return $row['count'];
    }

    private function get_number_of_organisms(){
        $query = $this->manager->createQuery('SELECT COUNT(o.fennecId) FROM AppBundle\Entity\Organism o');
        return $query->getSingleScalarResult();
    }

    private function get_number_of_trait_entries(){
        $query_get_number_of_trait_entries = <<<EOF
SELECT
    COUNT(*)
    FROM trait_categorical_entry
    WHERE deletion_date IS NULL
EOF;
        $stm_get_number_of_trait_entries = $this->manager->getConnection()->prepare($query_get_number_of_trait_entries);
        $stm_get_number_of_trait_entries->execute();
        $row = $stm_get_number_of_trait_entries->fetch(\PDO::FETCH_ASSOC);
        return $row['count'];
    }
{'fraction_non_alphanumeric': 0.12966101694915255, 'fraction_numerical': 0.000423728813559322, 'mean_word_length': 3.556949806949807, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 51, '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': '2873957', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1503, 'n_words': 233}