IndexItemRepository::search()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 1
crap 20
1
<?php
2
3
namespace SumoCoders\FrameworkSearchBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * IndexItemRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class IndexItemRepository extends EntityRepository
14
{
15
    public function search($term)
16
    {
17
        $query = $this->createQueryBuilder('r')
18
            ->where('r.value LIKE :term')
19
            ->setParameter('term', '%' . $term . '%')
20
            ->getQuery();
21
22
        $results = $query->getResult();
23
24
        if (empty($results)) {
25
            return null;
26
        }
27
28
        // group per class
29
        $items = array();
30
        foreach ($results as $indexItem) {
31
            /** @var $indexItem IndexItem */
32
            $class = $indexItem->getObjectType();
33
            $id = $indexItem->getOtherId();
34
35
            if (!isset($items[$class][$id])) {
36
                $items[$class][$id] = 0;
37
            }
38
39
            $items[$class][$id]++;
40
        }
41
42
        return $items;
43
    }
44
}
45