Completed
Push — master ( d587ee...4ea92a )
by Valentyn
03:27
created

MovieActorRepository::findAllByActor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 2
    public function findAllByActor(int $actorId, ?User $user = null): Query
37
    {
38 2
        $qb = $this->createQueryBuilder('ma')
39 2
            ->leftJoin('ma.movie', 'm')
40 2
            ->addSelect('m')
41 2
            ->leftJoin('m.translations', 'mt')
42 2
            ->addSelect('mt')
43 2
            ->leftJoin('m.genres', 'mg')
44 2
            ->addSelect('mg')
45 2
            ->leftJoin('mg.translations', 'mgt')
46 2
            ->addSelect('mgt');
47
48 2
        if ($user !== null) {
49 1
            $qb->leftJoin('m.userWatchedMovie', 'uwm', 'WITH', 'uwm.movie = ma.movie AND uwm.user = :user_id')
50 1
                ->addSelect('uwm')
51 1
                ->setParameter('user_id', $user->getId());
52
        }
53
54 2
        return $qb->where('ma.actor = :actorId')
55 2
            ->setParameter('actorId', $actorId)
56 2
            ->getQuery();
57
    }
58
}
59