Completed
Push — master ( 358b85...ccefed )
by Valentyn
04:05
created

MovieActorRepository::findAllByMovie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.9666
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\Movies\Entity\MovieActor;
8
use App\Users\Entity\User;
9
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10
use Doctrine\ORM\Query;
11
use Symfony\Bridge\Doctrine\RegistryInterface;
12
13
/**
14
 * @method MovieActor|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method MovieActor|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method MovieActor[]    findAll()
17
 * @method MovieActor[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class MovieActorRepository extends ServiceEntityRepository
20
{
21 3
    public function __construct(RegistryInterface $registry)
22
    {
23 3
        parent::__construct($registry, MovieActor::class);
24 3
    }
25
26 3
    public function findAllByMovie(int $movieId): Query
27
    {
28 3
        return $this->createQueryBuilder('ma')
29 3
            ->leftJoin('ma.actor', 'a')
30 3
            ->addSelect('a')
31 3
            ->where('ma.movie = :movieId')
32 3
            ->setParameter('movieId', $movieId)
33 3
            ->getQuery();
34
    }
35
36
    public function findAllByActor(int $actorId, ?User $user = null): Query
37
    {
38
        $qb = $this->createQueryBuilder('ma')
39
            ->leftJoin('ma.movie', 'm')
40
            ->addSelect('m')
41
            ->leftJoin('m.translations', 'mt')
42
            ->addSelect('mt')
43
            ->leftJoin('m.genres', 'mg')
44
            ->addSelect('mg')
45
            ->leftJoin('mg.translations', 'mgt')
46
            ->addSelect('mgt');
47
48
        if ($user !== null) {
49
            $qb->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.movie = ma.movie AND uwm.user = :user_id')
50
                ->addSelect('uwm')
51
                ->setParameter('user_id', $user->getId());
52
        }
53
54
        return $qb->where('ma.actor = :actorId')
55
            ->setParameter('actorId', $actorId)
56
            ->getQuery();
57
    }
58
}
59