Completed
Push — master ( 1302da...3a2ccf )
by Valentyn
03:07
created

MovieRepository::findByTitleQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Repository;
5
6
use App\Movies\Entity\Movie;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * TODO add relations to all queries
12
 *
13
 * @method Movie|null find($id, $lockMode = null, $lockVersion = null)
14
 * @method Movie|null findOneBy(array $criteria, array $orderBy = null)
15
 * @method Movie[]    findAll()
16
 * @method Movie[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17
 */
18
class MovieRepository extends ServiceEntityRepository
19
{
20 9
    public function __construct(RegistryInterface $registry)
21
    {
22 9
        parent::__construct($registry, Movie::class);
23 9
    }
24
25
    public function findAllWithIsWatchedFlag(int $userId)
26
    {
27
        $result = $this->createQueryBuilder('m')
28
            ->leftJoin('m.translations', 'mt')
29
            ->addSelect('mt')
30
            ->leftJoin('m.genres', 'mg')
31
            ->addSelect('mg')
32
            ->leftJoin('mg.translations', 'mgt')
33
            ->addSelect('mgt')
34
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
35
            ->addSelect('uwm')
36
            ->setParameter('user_id', $userId)
37
            ->getQuery();
38
39
        return $result;
40
    }
41
42 3
    public function findAllQuery()
43
    {
44 3
        $result = $this->createQueryBuilder('m')
45 3
            ->leftJoin('m.translations', 'mt')
46 3
            ->addSelect('mt')
47 3
            ->leftJoin('m.genres', 'mg')
48 3
            ->addSelect('mg')
49 3
            ->leftJoin('mg.translations', 'mgt')
50 3
            ->addSelect('mgt')
51 3
            ->getQuery();
52
53 3
        return $result;
54
    }
55
56
    // todo optimization (attach relations)
57 2
    public function findByTitleQuery(string $query)
58
    {
59 2
        $query = mb_strtolower($query);
60 2
        $result = $this->createQueryBuilder('m')
61 2
            ->leftJoin('m.translations', 't')
62 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(t.title) LIKE :title')
63 2
            ->setParameter('title', "%{$query}%")
64 2
            ->getQuery();
65
66 2
        return $result;
67
    }
68
69
    /**
70
     * This method will return array of already existed tmdb ids in our database
71
     *
72
     * @param array $tmdb_ids
73
     * @return array
74
     */
75 1
    public function getExistedTmdbIds(array $tmdb_ids)
76
    {
77 1
        $result = $this->createQueryBuilder('m')
78 1
            ->select('m.tmdb.id')
79 1
            ->where('m.tmdb.id IN (:ids)')
80 1
            ->setParameter('ids', $tmdb_ids)
81 1
            ->getQuery()->getArrayResult();
82
83 1
        return $result;
84
    }
85
86 5
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
87
    {
88 5
        if ($id === null && $tmdb_id === null) {
89
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
90
        }
91
92 5
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
93
    }
94
}
95