zend-blog-3-backend

Форк
0
/
GeoLocationRepository.php 
70 строк · 1.8 Кб
1
<?php
2

3
namespace App\Repository;
4

5
use App\Entity\GeoLocation;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\Persistence\ManagerRegistry;
8

9
/**
10
 * GeoLocationRepository
11
 *
12
 * This class was generated by the Doctrine ORM. Add your own custom
13
 * repository methods below.
14
 *
15
 * @method GeoLocation|null findOneByIpAddress($ip)
16
 */
17
class GeoLocationRepository extends ServiceEntityRepository
18
{
19
    /**
20
     * @param ManagerRegistry $registry
21
     */
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, GeoLocation::class);
25
    }
26

27
    /**
28
     * @param string $ip
29
     *
30
     * @return GeoLocation|null
31
     */
32
    public function findOrCreateByIpAddress(string $ip): ?GeoLocation
33
    {
34
        $location = null;
35
        if (filter_var($ip, FILTER_VALIDATE_IP)) {
36
            $location = $this->findOneByIpAddress($ip);
37
            if (!$location) {
38
                $location = new GeoLocation();
39
                $location
40
                    ->setIpAddress($ip)
41
                ;
42

43
                $this->getEntityManager()->persist($location);
44
                $this->getEntityManager()->flush();
45
            }
46
        }
47

48
        return $location;
49
    }
50

51
    /**
52
     * @param string $from
53
     * @param string $to
54
     *
55
     * @return int
56
     */
57
    public function getLocationsCount(string $from, string $to): int
58
    {
59
        $qb = $this->createQueryBuilder('g');
60
        $qb
61
            ->select('COUNT(g.id) AS cnt')
62
            ->andWhere($qb->expr()->gt('g.timeCreated', ':from'))
63
            ->andWhere($qb->expr()->lte('g.timeCreated', ':to'))
64
            ->setParameter('from', $from)
65
            ->setParameter('to', $to)
66
        ;
67

68
        return $qb->getQuery()->getSingleScalarResult();
69
    }
70
}
71

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.