Completed
Push — master ( 26caf8...b62262 )
by Valentyn
03:08
created

MovieRepository::findAllByTmdbIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Repository;
6
7
use App\Guests\Entity\GuestSession;
8
use App\Movies\Entity\Movie;
9
use App\Users\Entity\User;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\QueryBuilder;
13
use Symfony\Bridge\Doctrine\RegistryInterface;
14
15
/**
16
 * @method Movie|null find($id, $lockMode = null, $lockVersion = null)
17
 * @method Movie|null findOneBy(array $criteria, array $orderBy = null)
18
 * @method Movie[]    findAll()
19
 * @method Movie[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
20
 */
21
class MovieRepository extends ServiceEntityRepository
22
{
23 11
    public function __construct(RegistryInterface $registry)
24
    {
25 11
        parent::__construct($registry, Movie::class);
26 11
    }
27
28 7
    private function getBaseQuery(): QueryBuilder
29
    {
30 7
        return $this->createQueryBuilder('m')
31 7
            ->leftJoin('m.translations', 'mt')
32 7
            ->addSelect('mt')
33 7
            ->leftJoin('m.genres', 'mg')
34 7
            ->addSelect('mg')
35 7
            ->leftJoin('mg.translations', 'mgt')
36 7
            ->addSelect('mgt');
37
    }
38
39
    public function findAllWithIsUserWatchedFlag(User $user)
40
    {
41
        $result = $this->getBaseQuery()
42
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
43
            ->addSelect('uwm')
44
            ->setParameter('user_id', $user->getId())
45
            ->orderBy('m.id', 'DESC')
46
            ->getQuery();
47
48
        return $result;
49
    }
50
51 3
    public function findAllWithIsGuestWatchedFlag(?GuestSession $guestSession)
52
    {
53 3
        $guestSessionId = $guestSession ? $guestSession->getId() : 0;
54
55 3
        $result = $this->getBaseQuery()
56 3
            ->leftJoin('m.guestWatchedMovie', 'gwm', 'WITH', 'gwm.guestSession = :guest_session_id') // if this relation exists then guest has already watched this movie
57 3
            ->addSelect('gwm')
58 3
            ->setParameter('guest_session_id', $guestSessionId)
59 3
            ->orderBy('m.id', 'DESC')
60 3
            ->getQuery();
61
62 3
        return $result;
63
    }
64
65
    /**
66
     * @param array $ids
67
     *
68
     * @return array|Movie[]
69
     */
70
    public function findAllByIds(array $ids)
71
    {
72
        $result = $this->getBaseQuery()
73
            ->where('m.id IN (:ids)')
74
            ->setParameter('ids', $ids)
75
            ->getQuery()
76
            ->getResult();
77
78
        return $result;
79
    }
80
81
    /**
82
     * @param array $ids
83
     *
84
     * @return array|Movie[]
85
     */
86
    public function findAllByTmdbIds(array $ids)
87
    {
88
        $result = $this->getBaseQuery()
89
            ->where('m.tmdb.id IN (:ids)')
90
            ->setParameter('ids', $ids)
91
            ->getQuery()
92
            ->getResult();
93
94
        return $result;
95
    }
96
97 2
    public function getAllWatchedMoviesByUserId(int $userId): Query
98
    {
99 2
        $result = $this->getBaseQuery()
100 2
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
101 2
            ->addSelect('uwm')
102 2
            ->setParameter('user_id', $userId)
103 2
            ->andWhere('uwm.id != 0')
104 2
            ->orderBy('uwm.id', 'DESC')
105 2
            ->getQuery();
106
107 2
        return $result;
108
    }
109
110
    public function findAllQuery()
111
    {
112
        $result = $this->getBaseQuery()
113
            ->orderBy('m.id', 'DESC')
114
            ->getQuery();
115
116
        return $result;
117
    }
118
119 2
    public function findByTitleQuery(string $query)
120
    {
121 2
        $query = mb_strtolower($query);
122 2
        $result = $this->getBaseQuery()
123 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title')
124 2
            ->setParameter('title', "%{$query}%")
125 2
            ->getQuery();
126
127 2
        return $result;
128
    }
129
130 7
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
131
    {
132 7
        if ($id === null && $tmdb_id === null) {
133
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
134
        }
135
136 7
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
137
    }
138
}
139