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
|
8 |
|
public function __construct(RegistryInterface $registry) |
21
|
|
|
{ |
22
|
8 |
|
parent::__construct($registry, Movie::class); |
23
|
8 |
|
} |
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()->getResult(); |
38
|
|
|
|
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function findByTitle(string $query) |
43
|
|
|
{ |
44
|
2 |
|
$query = mb_strtolower($query); |
45
|
2 |
|
$result = $this->createQueryBuilder('m') |
46
|
2 |
|
->leftJoin('m.translations', 't') |
47
|
2 |
|
->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(t.title) LIKE :title') |
48
|
2 |
|
->setParameter('title', "%{$query}%") |
49
|
2 |
|
->getQuery()->getResult(); |
50
|
|
|
|
51
|
2 |
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This method will return array of already existed tmdb ids in our database |
56
|
|
|
* |
57
|
|
|
* @param array $tmdb_ids |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
public function getExistedTmdbIds(array $tmdb_ids) |
61
|
|
|
{ |
62
|
1 |
|
$result = $this->createQueryBuilder('m') |
63
|
1 |
|
->select('m.tmdb.id') |
64
|
1 |
|
->where('m.tmdb.id IN (:ids)') |
65
|
1 |
|
->setParameter('ids', $tmdb_ids) |
66
|
1 |
|
->getQuery()->getArrayResult(); |
67
|
|
|
|
68
|
1 |
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null) |
72
|
|
|
{ |
73
|
5 |
|
if ($id === null && $tmdb_id === null) { |
74
|
|
|
throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided'); |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|