Completed
Pull Request — master (#317)
by Guilherme
04:03
created

BlockedPhoneNumberRepository::findByPhone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 6
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