Completed
Push — master ( 1a9043...984777 )
by Valentyn
08:31
created

MovieRepository::findByTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Repository;
5
6
use App\Movies\Entity\Movie;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * @method Movie|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method Movie|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method Movie[]    findAll()
14
 * @method Movie[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class MovieRepository extends ServiceEntityRepository
17
{
18 8
    public function __construct(RegistryInterface $registry)
19
    {
20 8
        parent::__construct($registry, Movie::class);
21 8
    }
22
23 2
    public function findByTitle(string $query)
24
    {
25 2
        $query = mb_strtolower($query);
26 2
        $result = $this->createQueryBuilder('m')
27 2
            ->leftJoin('m.translations', 't')
28 2
            ->andWhere('LOWER(m.originalTitle) LIKE :title OR LOWER(t.title) LIKE :title')
29 2
            ->setParameter('title', "%{$query}%")
30 2
            ->getQuery()->getResult();
31
32 2
        return $result;
33
    }
34
35
    /**
36
     * This method will return array of already existed tmdb ids in our database
37
     *
38
     * @param array $tmdb_ids
39
     * @return array
40
     */
41 1
    public function getExistedTmdbIds(array $tmdb_ids)
42
    {
43 1
        $result = $this->createQueryBuilder('m')
44 1
            ->select('m.tmdb.id')
45 1
            ->where('m.tmdb.id IN (:ids)')
46 1
            ->setParameter('ids', $tmdb_ids)
47 1
            ->getQuery()->getArrayResult();
48
49 1
        return $result;
50
    }
51
52 5
    public function findOneByIdOrTmdbId(?int $id = null, ?int $tmdb_id = null)
53
    {
54 5
        if ($id === null && $tmdb_id === null) {
55
            throw new \InvalidArgumentException('Movie ID or TMDB ID should be provided');
56
        }
57
58 5
        return $id ? $this->find($id) : $this->findOneBy(['tmdb.id' => $tmdb_id]);
59
    }
60
}
61