Completed
Push — master ( 358b85...ccefed )
by Valentyn
04:05
created

MovieRepository::getBaseQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 27
    public function __construct(RegistryInterface $registry)
24
    {
25 27
        parent::__construct($registry, Movie::class);
26 27
    }
27
28 23
    private function getBaseQuery(): QueryBuilder
29
    {
30 23
        return $this->createQueryBuilder('m')
31 23
            ->leftJoin('m.translations', 'mt', null, null, 'mt.locale')
32 23
            ->addSelect('mt')
33 23
            ->leftJoin('m.genres', 'mg')
34 23
            ->addSelect('mg')
35 23
            ->leftJoin('mg.translations', 'mgt', null, null, 'mgt.locale')
36 23
            ->addSelect('mgt');
37
    }
38
39
    /**
40
     * @param int       $id
41
     * @param User|null $user
42
     *
43
     * @throws \Doctrine\ORM\NoResultException
44
     * @throws \Doctrine\ORM\NonUniqueResultException
45
     *
46
     * @return Movie|null
47
     */
48 2
    public function findOneForMoviePage(int $id, ?User $user = null): ?Movie
49
    {
50 2
        if ($user === null) {
51 2
            return $this->getBaseQuery()
52 2
                ->where('m.id = :id')
53 2
                ->setParameter('id', $id)
54 2
                ->getQuery()
55 2
                ->getSingleResult();
56
        }
57
58
        $result = $this->getBaseQuery()
59
            ->where('m.id = :id')
60
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
61
            ->addSelect('uwm')
62
            ->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :id')
63
            ->addSelect('urm')
64
            ->leftJoin('m.userInterestedMovie', 'uim', 'WITH', 'uim.user = :user_id AND uim.movie = :id')
65
            ->addSelect('uim')
66
            ->setParameter('user_id', $user->getId())
67
            ->setParameter('id', $id)
68
            ->getQuery()
69
            ->getSingleResult();
70
71
        return $result;
72
    }
73
74
    public function findAllByIdsWithFlags(array $ids, int $userId, int $originalMovieId)
75
    {
76
        $result = $this->getBaseQuery()
77
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
78
            ->addSelect('uwm')
79
            ->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :original_movie_id')
80
            ->addSelect('urm')
81
            ->where('m.id IN (:ids)')
82
            ->setParameter('user_id', $userId)
83
            ->setParameter('original_movie_id', $originalMovieId)
84
            ->setParameter('ids', $ids)
85
            ->getQuery()
86
            ->getResult();
87
88
        // Sorting here because ORDER BY FIELD(m.id, ...$ids) not working in postgres, we need to use joins on sorted table and so on, but I dont want to
89
        // todo => add sorting to sql
90
        $reversedIds = array_flip($ids);
91
        usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) {
92
            return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()];
93
        });
94
95
        return $result;
96
    }
97
98
    public function findAllByIdsWithoutFlags(array $ids)
99
    {
100
        $result = $this->findAllByIds($ids);
101
102
        // Sorting here because ORDER BY FIELD(m.id, ...$ids) not working in postgres, we need to use joins on sorted table and so on, but I dont want to
103
        // todo => add sorting to sql
104
        $reversedIds = array_flip($ids);
105
        usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) {
106
            return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()];
107
        });
108
109
        return $result;
110
    }
111
112
    public function findAllWithIsUserWatchedFlag(User $user)
113
    {
114
        $result = $this->getBaseQuery()
115
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
116
            ->addSelect('uwm')
117
            ->setParameter('user_id', $user->getId())
118
            ->orderBy('m.id', 'DESC')
119
            ->getQuery();
120
121
        return $result;
122
    }
123
124 17
    public function findAllWithIsGuestWatchedFlag(?GuestSession $guestSession)
125
    {
126 17
        $guestSessionId = $guestSession ? $guestSession->getId() : 0;
127
128 17
        $result = $this->getBaseQuery()
129 17
            ->leftJoin('m.guestWatchedMovie', 'gwm', 'WITH', 'gwm.guestSession = :guest_session_id') // if this relation exists then guest has already watched this movie
130 17
            ->addSelect('gwm')
131 17
            ->setParameter('guest_session_id', $guestSessionId)
132 17
            ->orderBy('m.id', 'DESC')
133 17
            ->getQuery();
134
135 17
        return $result;
136
    }
137
138
    /**
139
     * @param array $ids
140
     *
141
     * @return array|Movie[]
142
     */
143
    public function findAllByIds(array $ids)
144
    {
145
        $result = $this->getBaseQuery()
146
            ->where('m.id IN (:ids)')
147
            ->setParameter('ids', $ids)
148
            ->getQuery()
149
            ->getResult();
150
151
        return $result;
152
    }
153
154
    /**
155
     * @param array $ids
156
     *
157
     * @return array|Movie[]
158
     */
159
    public function findAllByIdsWithSimilarMovies(array $ids): array
160
    {
161
        $result = $this->createQueryBuilder('m')
162
            ->leftJoin('m.similarMovies', 'sm')
163
            ->addSelect('sm')
164
            ->where('m.id IN (:ids)')
165
            ->setParameter('ids', $ids)
166
            ->getQuery()
167
            ->getScalarResult();
168
169
        return $result;
170
    }
171
172
    /**
173
     * @param array $ids
174
     *
175
     * @return array|Movie[]
176
     */
177 1
    public function findAllByTmdbIds(array $ids)
178
    {
179 1
        $result = $this->getBaseQuery()
180 1
            ->where('m.tmdb.id IN (:ids)')
181 1
            ->setParameter('ids', $ids)
182 1
            ->getQuery()
183 1
            ->getResult();
184
185 1
        return $result;
186
    }
187
188
    /**
189
     * @param array $ids
190
     *
191
     * @return array of int
192
     */
193
    public function findAllIdsByTmdbIds(array $ids)
194
    {
195
        $result = $this->createQueryBuilder('m')
196
            ->select('m.id, m.tmdb.voteAverage, m.releaseDate')
197
            ->where('m.tmdb.id IN (:ids)')
198
            ->setParameter('ids', $ids)
199
            ->getQuery()
200
            ->getScalarResult();
201
202
        return $result;
203
    }
204
205 2
    public function getAllWatchedMoviesByUserId(int $ownerId, ?User $currentUser = null): Query
206
    {
207 2
        $result = $this->getBaseQuery()
208 2
            ->leftJoin('m.ownerWatchedMovie', 'owm', 'WITH', 'owm.user = :owner_id')
209 2
            ->addSelect('owm')
210 2
            ->setParameter('owner_id', $ownerId)
211 2
            ->andWhere('owm.id != 0')
212 2
            ->orderBy('owm.id', 'DESC');
213
214 2
        if ($currentUser !== null) {
215 2
            $result->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
216 2
                ->addSelect('uwm')
217 2
                ->setParameter('user_id', $currentUser->getId());
218
        }
219
220 2
        return $result->getQuery();
221
    }
222
223 3
    public function getAllInterestedMoviesByUserId(int $profileOwnerId, ?User $currentUser = null): Query
224
    {
225 3
        $result = $this->getBaseQuery()
226 3
            ->leftJoin('m.userInterestedMovie', 'uim', 'WITH', 'uim.user = :owner_id')
227 3
            ->addSelect('uim')
228 3
            ->setParameter('owner_id', $profileOwnerId)
229 3
            ->andWhere('uim.id != 0')
230 3
            ->orderBy('uim.id', 'DESC');
231
232 3
        if ($currentUser !== null) {
233 3
            $result->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :current_user_id')
234 3
                ->addSelect('uwm')
235 3
                ->setParameter('current_user_id', $currentUser->getId());
236
        }
237
238 3
        return $result->getQuery();
239
    }
240
241 2
    public function findAllByActor(int $actorId, ?User $currentUser = null): Query
242
    {
243 2
        $result = $this->getBaseQuery()
244 2
            ->leftJoin('m.actors', 'ma', 'WITH', 'ma.actor = :actor AND ma.movie = m')
245 2
            ->setParameter('actor', $actorId)
246 2
            ->andWhere('ma.id != 0')
247 2
            ->orderBy('m.releaseDate', 'DESC');
248
249 2
        if ($currentUser !== null) {
250 1
            $result->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
251 1
                ->addSelect('uwm')
252 1
                ->setParameter('user_id', $currentUser->getId());
253
        }
254
255 2
        return $result->getQuery();
256
    }
257
258
    public function findAllQuery()
259
    {
260
        $result = $this->getBaseQuery()
261
            ->orderBy('m.id', 'DESC')
262
            ->getQuery();
263
264
        return $result;
265
    }
266
267 2
    public function findByTitleQuery(string $query)
268
    {
269 2
        $query = mb_strtolower($query);
270 2
        $result = $this->getBaseQuery()
271 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title')
272 2
            ->setParameter('title', "%{$query}%")
273 2
            ->getQuery();
274
275 2
        return $result;
276
    }
277
278
    public function findByTitleWithUserRecommendedMovieQuery(string $query, int $userId, int $originalMovieId)
279
    {
280
        $query = mb_strtolower($query);
281
        $result = $this->getBaseQuery()
282
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title')
283
            ->setParameter('title', "%{$query}%")
284
            ->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id AND urm.originalMovie = :movie_id')
285
            ->addSelect('urm')
286
            ->setParameter('user_id', $userId)
287
            ->setParameter('movie_id', $originalMovieId)
288
            ->getQuery();
289
290
        return $result;
291
    }
292
293 9
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
294
    {
295 9
        if ($id === null && $tmdb_id === null) {
296
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
297
        }
298
299 9
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
300
    }
301
}
302