Passed
Push — master ( a6dc76...6d79cb )
by Guilherme
01:12 queued 11s
created

getSearchByPartialPhoneQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 10
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\PhoneVerificationBundle\Entity;
12
13
use Doctrine\ORM\EntityRepository;
14
use libphonenumber\PhoneNumber;
15
use LoginCidadao\PhoneVerificationBundle\Model\BlockedPhoneNumberInterface;
16
17
/**
18
 * Class BlockedPhoneNumberRepository
19
 * @package LoginCidadao\PhoneVerificationBundle\Entity
20
 *
21
 * @codeCoverageIgnore
22
 */
23
class BlockedPhoneNumberRepository extends EntityRepository
24
{
25
    /**
26
     * @param PhoneNumber $phoneNumber
27
     * @return BlockedPhoneNumberInterface
28
     */
29
    public function findByPhone(PhoneNumber $phoneNumber): ?BlockedPhoneNumberInterface
30
    {
31
        /** @var BlockedPhoneNumberInterface $blockedPhone */
32
        $blockedPhone = $this->findOneBy(['phoneNumber' => $phoneNumber]);
33
34
        return $blockedPhone;
35
    }
36
37
    /**
38
     * @param string $search
39
     * @return BlockedPhoneNumberInterface[]
40
     */
41
    public function searchBlocksByPartialPhone(string $search): array
42
    {
43
44
        return $this->getSearchByPartialPhoneQuery($search)
45
            ->getQuery()
46
            ->getResult();
47
    }
48
49
    public function getSearchByPartialPhoneQuery(string $search)
50
    {
51
        if ($search[0] === '+') {
52
            $search = "{$search}%";
53
        } else {
54
            $search = "%{$search}%";
55
        }
56
57
        return $this->createQueryBuilder('b')
58
            ->where('b.phoneNumber LIKE :search')
59
            ->setParameter('search', $search);
60
    }
61
}
62