Completed
Push — master ( 665a2d...66f2ad )
by Valentyn
03:38
created

MovieRepository::getAllWatchedMoviesByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 11
    public function __construct(RegistryInterface $registry)
24
    {
25 11
        parent::__construct($registry, Movie::class);
26 11
    }
27
28 7
    private function getBaseQuery(): QueryBuilder
29
    {
30 7
        return $this->createQueryBuilder('m')
31 7
            ->leftJoin('m.translations', 'mt')
32 7
            ->addSelect('mt')
33 7
            ->leftJoin('m.genres', 'mg')
34 7
            ->addSelect('mg')
35 7
            ->leftJoin('mg.translations', 'mgt')
36 7
            ->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 2
    public function getAllWatchedMoviesByUserId(int $userId): Query
82
    {
83 2
        $result = $this->getBaseQuery()
84 2
            ->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.user = :user_id')
85 2
            ->addSelect('uwm')
86 2
            ->setParameter('user_id', $userId)
87 2
            ->andWhere('uwm.id != 0')
88 2
            ->orderBy('uwm.id', 'DESC')
89 2
            ->getQuery();
90
91 2
        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 7
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
115
    {
116 7
        if ($id === null && $tmdb_id === null) {
117
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
118
        }
119
120 7
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
121
    }
122
}
123