CategoryRepository::autocomplete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Category;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\ORM\ORMException;
8
use Doctrine\ORM\OptimisticLockException;
9
use Doctrine\Common\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Persistence\ManagerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Ramsey\Uuid\UuidInterface;
11
use App\Repository\Exception\CategoryNotFoundException;
12
13
/**
14
 * @method Category|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method Category|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method Category[]    findAll()
17
 * @method Category[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class CategoryRepository extends ServiceEntityRepository
20
{
21
    /**
22
     * @param ManagerRegistry $registry
23
     */
24
    public function __construct(ManagerRegistry $registry)
25
    {
26
        parent::__construct($registry, Category::class);
27
    }
28
    
29
    /**
30
     * @param Category $category
31
     * @throws \Doctrine\ORM\ORMException
32
     * @throws \Doctrine\ORM\OptimisticLockException
33
     */
34
    public function save(Category $category): void
35
    {
36
        $em = $this->getEntityManager();
37
        $em->persist($category);
38
        $em->flush();
39
    }
40
    
41
    /**
42
     * @param Category $category
43
     * @throws ORMException
44
     * @throws OptimisticLockException
45
     */
46
    public function delete(Category $category): void
47
    {
48
        $em = $this->getEntityManager();
49
        $em->remove($category);
50
        $em->flush();
51
    }
52
    
53
    /**
54
     * @param UuidInterface $categoryId
55
     * @throws CategoryNotFoundException
56
     * @return Category
57
     */
58
    public function getCategory(UuidInterface $categoryId): Category
59
    {
60
        if ($category = $this->find($categoryId)) {
61
            return $category;
62
        }
63
        
64
        throw new CategoryNotFoundException('Category not found');
65
    }
66
    
67
    /**
68
     * @return array
69
     */
70
    public function listAllCategories(string $field = 'name', string $dir = 'ASC'): array
71
    {
72
        if (!Category::checkOrderField($field)) {
73
            return $this->findBy([], ['name' => 'ASC']);
74
        }
75
76
        return $this->findBy([], [$field => ($dir === 'DESC' ? 'DESC' : 'ASC')]);
77
    }
78
79
    /**
80
     * @param string|null $searchQuery
81
     * @return array
82
     */
83
    public function autocomplete(?string $searchQuery): array
84
    {
85
        $qb = $this->createQueryBuilder('c');
86
        return $qb->where($qb->expr()->like('c.name', ':searchQuery'))
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->where($qb->e...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
87
        ->orWhere($qb->expr()->like('c.description', ':searchQuery'))
88
        ->orderBy('c.name' ,'ASC')
89
        ->setParameters([
90
            'searchQuery' => "%{$searchQuery}%",
91
        ])
92
        ->getQuery()->getResult();
93
    }
94
}
95