Completed
Push — master ( d44c39...321b0a )
by Valentyn
04:27
created

MovieRecommendationRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 98
ccs 56
cts 68
cp 0.8235
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAllByUser() 0 49 3
A findAllByMovieAndUser() 0 20 1
A findAllByMovie() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Repository;
6
7
use App\Movies\Entity\Movie;
8
use App\Movies\Entity\MovieRecommendation;
9
use App\Users\Entity\User;
10
use App\Users\Entity\UserInterestedMovie;
11
use App\Users\Entity\UserWatchedMovie;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\ORM\Query;
14
use Symfony\Bridge\Doctrine\RegistryInterface;
15
16
/**
17
 * @method Movie|null find($id, $lockMode = null, $lockVersion = null)
18
 * @method Movie|null findOneBy(array $criteria, array $orderBy = null)
19
 * @method Movie[]    findAll()
20
 * @method Movie[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
21
 */
22
class MovieRecommendationRepository extends ServiceEntityRepository
23
{
24 5
    public function __construct(RegistryInterface $registry)
25
    {
26 5
        parent::__construct($registry, MovieRecommendation::class);
27 5
    }
28
29
    // todo joins for movie (genres, translations etc.)
30 2
    public function findAllByUser(int $userId, int $minVote = 7, ?User $currentUser = null): array
31
    {
32 2
        $items = $this->getEntityManager()->createQueryBuilder()
33 2
            ->select('m')
34 2
            ->from(Movie::class, 'm')
35 2
            ->leftJoin('m.translations', 'mt', null, null, 'mt.locale')
36 2
            ->addSelect('mt')
37 2
            ->where('m.id IN (:ids)');
38
39 2
        $ids = $this->getEntityManager()->createQueryBuilder()
40 2
            ->select('IDENTITY(mr.recommendedMovie), COUNT(mr.recommendedMovie) HIDDEN rate')
41 2
            ->from(UserWatchedMovie::class, 'uwm')
42 2
            ->leftJoin(MovieRecommendation::class, 'mr', 'WITH', 'uwm.movie = mr.originalMovie')
43 2
            ->where('uwm.user = :user AND uwm.vote >= :vote')
44 2
            ->setParameter('user', $userId)
45 2
            ->setParameter('vote', $minVote)
46 2
            ->groupBy('mr.recommendedMovie')
47 2
            ->orderBy('rate DESC, MAX(mr.id)', 'DESC');
48
49 2
        if ($currentUser !== null) {
50 1
            if ($currentUser->getId() === $userId) {
51
                $items = $items
52 1
                    ->addSelect('uwmj')
53 1
                    ->leftJoin('m.userWatchedMovie', 'uwmj', 'WITH', 'uwmj.user = :user')
54 1
                    ->andWhere('uwmj.id IS NULL')
55 1
                    ->setParameter('user', $currentUser->getId());
56
57
                $ids
58 1
                    ->leftJoin(UserWatchedMovie::class, 'uwmj', 'WITH', 'uwmj.movie = mr.recommendedMovie AND uwmj.user = :user')
59 1
                    ->andWhere('uwmj.id IS NULL')
60 1
                    ->setParameter('user', $currentUser->getId());
61
            } else {
62
                $items = $items
63 1
                    ->addSelect('uwmj')
64 1
                    ->leftJoin('m.userWatchedMovie', 'uwmj', 'WITH', 'uwmj.user = :currentUser')
65 1
                    ->setParameter('currentUser', $currentUser->getId());
66
            }
67
        }
68
69 2
        $count = $this->getEntityManager()->createQueryBuilder()
70 2
            ->select('COUNT(DISTINCT mr.recommendedMovie)')
71 2
            ->from(UserWatchedMovie::class, 'uwm')
72 2
            ->leftJoin(MovieRecommendation::class, 'mr', 'WITH', 'uwm.movie = mr.originalMovie')
73 2
            ->where('uwm.user = :user AND uwm.vote >= :vote')
74 2
            ->setParameter('user', $userId)
75 2
            ->setParameter('vote', $minVote);
76
77 2
        return [$items->getQuery(), $ids->getQuery(), $count->getQuery()];
78
    }
79
80 3
    public function findAllByMovieAndUser(int $movieId, int $userId): Query
81
    {
82 3
        $query = $this->getEntityManager()->createQueryBuilder()
83 3
            ->select('m, COUNT(mr.recommendedMovie) HIDDEN rate')
84 3
            ->from(MovieRecommendation::class, 'mr')
85 3
            ->leftJoin(Movie::class, 'm', 'WITH', 'mr.recommendedMovie = m')
86 3
            ->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.originalMovie = mr.originalMovie AND urm.user = :user')
87 3
            ->addSelect('urm')
88 3
            ->leftJoin('m.userInterestedMovie', 'uim', 'WITH', 'uim.user = :user')
89 3
            ->addSelect('uim')
90 3
            ->where('mr.originalMovie = :movie')
91 3
            ->setParameter('user', $userId)
92 3
            ->setParameter('movie', $movieId)
93 3
            ->groupBy('mr.recommendedMovie, m.id, urm.id, uim.id')
94 3
            ->orderBy('rate', 'DESC')
95 3
            ->addOrderBy('MAX(mr.id)', 'DESC')
96 3
            ->getQuery();
97
98 3
        return $query;
99
    }
100
101
    public function findAllByMovie(int $movieId): Query
102
    {
103
        $query = $this->getEntityManager()->createQueryBuilder()
104
            ->select('m, COUNT(mr.recommendedMovie) HIDDEN rate')
105
            ->from(MovieRecommendation::class, 'mr')
106
            ->leftJoin(Movie::class, 'm', 'WITH', 'mr.recommendedMovie = m')
107
            //->leftJoin('m.userRecommendedMovie', 'urm', 'WITH', 'urm.user = :user AND urm.recommendedMovie = mr.recommendedMovie')
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
            //->addSelect('urm')
109
            ->where('mr.originalMovie = :movie')
110
            //->setParameter('user', $userId)
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
            ->setParameter('movie', $movieId)
112
            ->groupBy('mr.recommendedMovie, m.id')
113
            ->orderBy('rate', 'DESC')
114
            ->addOrderBy('MAX(mr.id)', 'DESC')
115
            ->getQuery();
116
117
        return $query;
118
    }
119
}
120