<commit_before><?php

namespace Zizoo\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Zizoo\Bundle\CmsBundle\Model\InstagramMediaRepositoryInterface;

/**
 * InstagramMediaRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class InstagramMediaRepository extends EntityRepository implements InstagramMediaRepositoryInterface
{
    public function save(InstagramMedia $media)
    {
        $em = $this->getEntityManager();
        $em->persist($media);
        $em->flush();
    }


    public function saveBatch(array $medias)
    {
        $em = $this->getEntityManager();
        $i = 0;
        foreach ($medias as $media) {
            $em->persist($media);
            $i += 1;
            if($i >= 20) {
                $i = 0;
                $em->flush();
            }
        }

        $em->flush();
    }


    public function getRecentMedia($pageNumber = null, $itemCount = null)
    {
        $qb = $this->createQueryBuilder('im');
        $this->addPaginationQueryBuilder($qb, $pageNumber, $itemCount);
        return $qb
            ->orderBy('im.createdAt', 'desc')
            ->getQuery()
            ->getResult();
    }


    public function getMediaCount()
    {
        return $this->createQueryBuilder('im')
            ->select('COUNT(im.id)')
            ->getQuery()
            ->getSingleScalarResult();
    }

    /**<commit_msg>UJ-1260 Instagram Integration -> caching query result<commit_after><?php

namespace Zizoo\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Zizoo\Bundle\CmsBundle\Model\InstagramMediaRepositoryInterface;

/**
 * InstagramMediaRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class InstagramMediaRepository extends EntityRepository implements InstagramMediaRepositoryInterface
{
    const CACHE_TTL = 86400;

    public function save(InstagramMedia $media)
    {
        $em = $this->getEntityManager();
        $em->persist($media);
        $em->flush();
    }


    public function saveBatch(array $medias)
    {
        $em = $this->getEntityManager();
        $i = 0;
        foreach ($medias as $media) {
            $em->persist($media);
            $i += 1;
            if($i >= 20) {
                $i = 0;
                $em->flush();
            }
        }

        $em->flush();
    }


    public function getRecentMedia($pageNumber = null, $itemCount = null)
    {
        $qb = $this->createQueryBuilder('im');
        $this->addPaginationQueryBuilder($qb, $pageNumber, $itemCount);
        return $qb
            ->orderBy('im.createdAt', 'desc')
            ->getQuery()
            ->useResultCache(true, self::CACHE_TTL)
            ->getResult();
    }


    public function getMediaCount()
    {
        return $this->createQueryBuilder('im')
            ->select('COUNT(im.id)')
            ->getQuery()
            ->useResultCache(true, self::CACHE_TTL)
            ->getSingleScalarResult();
    }

    /**
{'fraction_non_alphanumeric': 0.13125802310654686, 'fraction_numerical': 0.006097560975609756, 'mean_word_length': 2.200205338809035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '4739421', 'n_tokens_mistral': 920, 'n_tokens_neox': 883, 'n_words': 182}