|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Movies\Repository; |
|
5
|
|
|
|
|
6
|
|
|
use App\Guests\Entity\GuestSession; |
|
7
|
|
|
use App\Movies\Entity\Movie; |
|
8
|
|
|
use App\Users\Entity\User; |
|
9
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
11
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method Movie|null find($id, $lockMode = null, $lockVersion = null) |
|
15
|
|
|
* @method Movie|null findOneBy(array $criteria, array $orderBy = null) |
|
16
|
|
|
* @method Movie[] findAll() |
|
17
|
|
|
* @method Movie[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
18
|
|
|
*/ |
|
19
|
|
|
class MovieRepository extends ServiceEntityRepository |
|
20
|
|
|
{ |
|
21
|
9 |
|
public function __construct(RegistryInterface $registry) |
|
22
|
|
|
{ |
|
23
|
9 |
|
parent::__construct($registry, Movie::class); |
|
24
|
9 |
|
} |
|
25
|
|
|
|
|
26
|
5 |
|
private function getBaseQuery(): QueryBuilder |
|
27
|
|
|
{ |
|
28
|
5 |
|
return $this->createQueryBuilder('m') |
|
29
|
5 |
|
->leftJoin('m.translations', 'mt') |
|
30
|
5 |
|
->addSelect('mt') |
|
31
|
5 |
|
->leftJoin('m.genres', 'mg') |
|
32
|
5 |
|
->addSelect('mg') |
|
33
|
5 |
|
->leftJoin('mg.translations', 'mgt') |
|
34
|
5 |
|
->addSelect('mgt'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function findAllWithIsUserWatchedFlag(User $user) |
|
38
|
|
|
{ |
|
39
|
|
|
$result = $this->getBaseQuery() |
|
40
|
|
|
->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie |
|
41
|
|
|
->addSelect('uwm') |
|
42
|
|
|
->setParameter('user_id', $user->getId()) |
|
43
|
|
|
->orderBy('m.id', 'DESC') |
|
44
|
|
|
->getQuery(); |
|
45
|
|
|
|
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
public function findAllWithIsGuestWatchedFlag(?GuestSession $guestSession) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$guestSessionId = $guestSession ? $guestSession->getId() : 0; |
|
52
|
|
|
|
|
53
|
3 |
|
$result = $this->getBaseQuery() |
|
54
|
3 |
|
->leftJoin('m.guestWatchedMovie', 'gwm', 'WITH', 'gwm.guestSession = :guest_session_id') // if this relation exists then guest has already watched this movie |
|
55
|
3 |
|
->addSelect('gwm') |
|
56
|
3 |
|
->setParameter('guest_session_id', $guestSessionId) |
|
57
|
3 |
|
->orderBy('m.id', 'DESC') |
|
58
|
3 |
|
->getQuery(); |
|
59
|
|
|
|
|
60
|
3 |
|
return $result; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function findAllQuery() |
|
64
|
|
|
{ |
|
65
|
|
|
$result = $this->getBaseQuery() |
|
66
|
|
|
->orderBy('m.id', 'DESC') |
|
67
|
|
|
->getQuery(); |
|
68
|
|
|
|
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
public function findByTitleQuery(string $query) |
|
73
|
|
|
{ |
|
74
|
2 |
|
$query = mb_strtolower($query); |
|
75
|
2 |
|
$result = $this->getBaseQuery() |
|
76
|
2 |
|
->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title') |
|
77
|
2 |
|
->setParameter('title', "%{$query}%") |
|
78
|
2 |
|
->getQuery(); |
|
79
|
|
|
|
|
80
|
2 |
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This method will return array of already existed tmdb ids in our database |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $tmdb_ids |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getExistedTmdbIds(array $tmdb_ids) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$result = $this->createQueryBuilder('m') |
|
92
|
1 |
|
->select('m.tmdb.id') |
|
93
|
1 |
|
->where('m.tmdb.id IN (:ids)') |
|
94
|
1 |
|
->setParameter('ids', $tmdb_ids) |
|
95
|
1 |
|
->getQuery()->getArrayResult(); |
|
96
|
|
|
|
|
97
|
1 |
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null) |
|
101
|
|
|
{ |
|
102
|
5 |
|
if ($id === null && $tmdb_id === null) { |
|
103
|
|
|
throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|