Completed
Push — master ( 3d9909...7a8301 )
by Valentyn
03:23
created

MovieRepository::getAllRecommendations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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