Test Failed
Push — issue#666 ( f5ce0d...ce3976 )
by Guilherme
28:08
created

CityRepository::findByString()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 4
nop 3
dl 0
loc 35
ccs 0
cts 25
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Entity;
12
13
use Doctrine\ORM\EntityRepository;
14
15
class CityRepository extends EntityRepository
16
{
17
18
    public function findByString($string, $countryId = null, $stateId = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
19
    {
20
        $qb = $this->getEntityManager()->createQueryBuilder();
21
22
        $qb
23
            ->select('c')
24
            ->from('LoginCidadaoCoreBundle:City', 'c')
25
            ->join(
26
                'LoginCidadaoCoreBundle:State',
27
                's',
28
                'WITH',
29
                'c.state = s'
30
            )
31
            ->join(
32
                'LoginCidadaoCoreBundle:Country',
33
                'co',
34
                'WITH',
35
                's.country = co'
36
            )
37
            ->where('c.name LIKE :string OR LOWER(c.name) LIKE :string')
38
            ->addOrderBy('s.preference', 'DESC')
39
            ->addOrderBy('c.name', 'ASC')
40
            ->setParameter('string', "$string%");
41
42
        if ($stateId > 0) {
43
            $qb->andWhere('s.id = :stateId')
44
                ->setParameter('stateId', $stateId);
45
        }
46
        if ($countryId > 0) {
47
            $qb->andWhere('co.id = :countryId')
48
                ->setParameter('countryId', $countryId);
49
        }
50
51
        return $qb->getQuery()->getResult();
52
    }
53
54
    public function findByPreferedState()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        $em = $this->getEntityManager();
57
        $states = $em->getRepository('LoginCidadaoCoreBundle:State')
58
            ->createQueryBuilder('s')
59
            ->orderBy('s.preference', 'DESC')
60
            ->setMaxResults(1)
61
            ->getQuery()->getResult();
62
        $state = reset($states);
63
        $cities = $state->getCities();
64
65
        return $cities;
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    public function countCities()
72
    {
73
        return $this->createQueryBuilder('c')
74
            ->select('COUNT(c)')
75
            ->getQuery()->getSingleScalarResult();
76
    }
77
}
78