Completed
Push — master ( 1c84dd...0f2f0f )
by Valentyn
03:06
created

MovieRepository::findAllByIdsWithSimilarMovies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.8666
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 12
    public function __construct(RegistryInterface $registry)
24
    {
25 12
        parent::__construct($registry, Movie::class);
26 12
    }
27
28 8
    private function getBaseQuery(): QueryBuilder
29
    {
30 8
        return $this->createQueryBuilder('m')
31 8
            ->leftJoin('m.translations', 'mt')
32 8
            ->addSelect('mt')
33 8
            ->leftJoin('m.genres', 'mg')
34 8
            ->addSelect('mg')
35 8
            ->leftJoin('mg.translations', 'mgt')
36 8
            ->addSelect('mgt');
37
    }
38
39
    public function getAllRecommendations(int $userId)
0 ignored issues
show
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
    }
42
43
    public function findAllByIdsWithFlags(array $ids, int $userId)
44
    {
45
        $result = $this->getBaseQuery()
46
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
47
            ->addSelect('uwm')
48
            ->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user_id')
49
            ->addSelect('urm')
50
            ->where('m.id IN (:ids)')
51
            ->setParameter('user_id', $userId)
52
            ->setParameter('ids', $ids)
53
            ->getQuery()
54
            ->getResult();
55
56
        // 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
57
        // todo => add sorting to sql
58
        $reversedIds = array_flip($ids);
59
        usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) {
60
            return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()];
61
        });
62
63
        return $result;
64
    }
65
66
    public function findAllByIdsWithoutFlags(array $ids)
67
    {
68
        $result = $this->findAllByIds($ids);
69
70
        // 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
71
        // todo => add sorting to sql
72
        $reversedIds = array_flip($ids);
73
        usort($result, function (Movie $movie1, Movie $movie2) use ($reversedIds) {
74
            return $reversedIds[$movie1->getId()] <=> $reversedIds[$movie2->getId()];
75
        });
76
77
        return $result;
78
    }
79
80
    public function findAllWithIsUserWatchedFlag(User $user)
81
    {
82
        $result = $this->getBaseQuery()
83
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id') // if this relation exists then user has already watched this movie
84
            ->addSelect('uwm')
85
            ->setParameter('user_id', $user->getId())
86
            ->orderBy('m.id', 'DESC')
87
            ->getQuery();
88
89
        return $result;
90
    }
91
92 4
    public function findAllWithIsGuestWatchedFlag(?GuestSession $guestSession)
93
    {
94 4
        $guestSessionId = $guestSession ? $guestSession->getId() : 0;
95
96 4
        $result = $this->getBaseQuery()
97 4
            ->leftJoin('m.guestWatchedMovie', 'gwm', 'WITH', 'gwm.guestSession = :guest_session_id') // if this relation exists then guest has already watched this movie
98 4
            ->addSelect('gwm')
99 4
            ->setParameter('guest_session_id', $guestSessionId)
100 4
            ->orderBy('m.id', 'DESC')
101 4
            ->getQuery();
102
103 4
        return $result;
104
    }
105
106
    /**
107
     * @param array $ids
108
     *
109
     * @return array|Movie[]
110
     */
111
    public function findAllByIds(array $ids)
112
    {
113
        $result = $this->getBaseQuery()
114
            ->where('m.id IN (:ids)')
115
            ->setParameter('ids', $ids)
116
            ->getQuery()
117
            ->getResult();
118
119
        return $result;
120
    }
121
122
    /**
123
     * @param array $ids
124
     *
125
     * @return array|Movie[]
126
     */
127
    public function findAllByIdsWithSimilarMovies(array $ids): array
128
    {
129
        $result = $this->createQueryBuilder('m')
130
            ->leftJoin('m.similarMovies', 'sm')
131
            ->addSelect('sm')
132
            ->where('m.id IN (:ids)')
133
            ->setParameter('ids', $ids)
134
            ->getQuery()
135
            ->getScalarResult();
136
137
        return $result;
138
    }
139
140
    /**
141
     * @param array $ids
142
     *
143
     * @return array|Movie[]
144
     */
145 1
    public function findAllByTmdbIds(array $ids)
146
    {
147 1
        $result = $this->getBaseQuery()
148 1
            ->where('m.tmdb.id IN (:ids)')
149 1
            ->setParameter('ids', $ids)
150 1
            ->getQuery()
151 1
            ->getResult();
152
153 1
        return $result;
154
    }
155
156
    /**
157
     * @param array $ids
158
     *
159
     * @return array of int
160
     */
161
    public function findAllIdsByTmdbIds(array $ids)
162
    {
163
        $result = $this->createQueryBuilder('m')
164
            ->select('m.id, m.tmdb.voteAverage')
165
            ->where('m.tmdb.id IN (:ids)')
166
            ->setParameter('ids', $ids)
167
            ->getQuery()
168
            ->getScalarResult();
169
170
        return $result;
171
    }
172
173 2
    public function getAllWatchedMoviesByUserId(int $userId): Query
174
    {
175 2
        $result = $this->getBaseQuery()
176 2
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
177 2
            ->addSelect('uwm')
178 2
            ->setParameter('user_id', $userId)
179 2
            ->andWhere('uwm.id != 0')
180 2
            ->orderBy('uwm.id', 'DESC')
181 2
            ->getQuery();
182
183 2
        return $result;
184
    }
185
186
    public function findAllQuery()
187
    {
188
        $result = $this->getBaseQuery()
189
            ->orderBy('m.id', 'DESC')
190
            ->getQuery();
191
192
        return $result;
193
    }
194
195 2
    public function findByTitleQuery(string $query)
196
    {
197 2
        $query = mb_strtolower($query);
198 2
        $result = $this->getBaseQuery()
199 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title')
200 2
            ->setParameter('title', "%{$query}%")
201 2
            ->getQuery();
202
203 2
        return $result;
204
    }
205
206 7
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
207
    {
208 7
        if ($id === null && $tmdb_id === null) {
209
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
210
        }
211
212 7
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
213
    }
214
}
215