Completed
Push — master ( 3e3269...414f6c )
by Valentyn
05:10
created

MovieRepository::findAllWithIsUserWatchedFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 2
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
    /**
64
     * @param array $ids
65
     * @return array|Movie[]
66
     */
67
    public function findAllByIds(array $ids)
68
    {
69
        $result = $this->getBaseQuery()
70
            ->where('m.id IN (:ids)')
71
            ->setParameter('ids', $ids)
72
            ->getQuery()
73
            ->getResult();
74
75
        return $result;
76
    }
77
78
    public function findAllQuery()
79
    {
80
        $result = $this->getBaseQuery()
81
            ->orderBy('m.id', 'DESC')
82
            ->getQuery();
83
84
        return $result;
85
    }
86
87 2
    public function findByTitleQuery(string $query)
88
    {
89 2
        $query = mb_strtolower($query);
90 2
        $result = $this->getBaseQuery()
91 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(mt.title) LIKE :title')
92 2
            ->setParameter('title', "%{$query}%")
93 2
            ->getQuery();
94
95 2
        return $result;
96
    }
97
98 5
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
99
    {
100 5
        if ($id === null && $tmdb_id === null) {
101
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
102
        }
103
104 5
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
105
    }
106
}
107