Completed
Push — master ( 3a2ccf...d47add )
by Valentyn
02:40
created

MovieRepository::findOneByIdOrTmdbId()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 2
crap 4.25
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
            ->orderBy('m.id', 'DESC')
52 3
            ->getQuery();
53
54 3
        return $result;
55
    }
56
57
    // todo optimization (attach relations)
58 2
    public function findByTitleQuery(string $query)
59
    {
60 2
        $query = mb_strtolower($query);
61 2
        $result = $this->createQueryBuilder('m')
62 2
            ->leftJoin('m.translations', 't')
63 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(t.title) LIKE :title')
64 2
            ->setParameter('title', "%{$query}%")
65 2
            ->getQuery();
66
67 2
        return $result;
68
    }
69
70
    /**
71
     * This method will return array of already existed tmdb ids in our database
72
     *
73
     * @param array $tmdb_ids
74
     * @return array
75
     */
76 1
    public function getExistedTmdbIds(array $tmdb_ids)
77
    {
78 1
        $result = $this->createQueryBuilder('m')
79 1
            ->select('m.tmdb.id')
80 1
            ->where('m.tmdb.id IN (:ids)')
81 1
            ->setParameter('ids', $tmdb_ids)
82 1
            ->getQuery()->getArrayResult();
83
84 1
        return $result;
85
    }
86
87 5
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
88
    {
89 5
        if ($id === null && $tmdb_id === null) {
90
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
91
        }
92
93 5
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
94
    }
95
}
96