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
|
9 |
|
public function __construct(RegistryInterface $registry) |
24
|
|
|
{ |
25
|
9 |
|
parent::__construct($registry, Movie::class); |
26
|
9 |
|
} |
27
|
|
|
|
28
|
5 |
|
private function getBaseQuery(): QueryBuilder |
29
|
|
|
{ |
30
|
5 |
|
return $this->createQueryBuilder('m') |
31
|
5 |
|
->leftJoin('m.translations', 'mt') |
32
|
5 |
|
->addSelect('mt') |
33
|
5 |
|
->leftJoin('m.genres', 'mg') |
34
|
5 |
|
->addSelect('mg') |
35
|
5 |
|
->leftJoin('mg.translations', 'mgt') |
36
|
5 |
|
->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
|
|
|
public function getAllWatchedMoviesByUserId(int $userId): Query |
82
|
|
|
{ |
83
|
|
|
$result = $this->getBaseQuery() |
84
|
|
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') |
85
|
|
|
->addSelect('uwm') |
86
|
|
|
->setParameter('user_id', $userId) |
87
|
|
|
->andWhere('uwm.id != 0') |
88
|
|
|
->orderBy('uwm.id', 'DESC') |
89
|
|
|
->getQuery(); |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function findAllQuery() |
95
|
|
|
{ |
96
|
|
|
$result = $this->getBaseQuery() |
97
|
|
|
->orderBy('m.id', 'DESC') |
98
|
|
|
->getQuery(); |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function findByTitleQuery(string $query) |
104
|
|
|
{ |
105
|
2 |
|
$query = mb_strtolower($query); |
106
|
2 |
|
$result = $this->getBaseQuery() |
107
|
2 |
|
->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title') |
108
|
2 |
|
->setParameter('title', "%{$query}%") |
109
|
2 |
|
->getQuery(); |
110
|
|
|
|
111
|
2 |
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
5 |
|
public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null) |
115
|
|
|
{ |
116
|
5 |
|
if ($id === null && $tmdb_id === null) { |
117
|
|
|
throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided'); |
118
|
|
|
} |
119
|
|
|
|
120
|
5 |
|
return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|