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

MovieActorRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 37.04%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 40
ccs 10
cts 27
cp 0.3704
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAllByMovie() 0 9 1
A findAllByActor() 0 22 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
    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