zend-blog-3-backend
70 строк · 1.8 Кб
1<?php
2
3namespace App\Repository;
4
5use App\Entity\GeoLocation;
6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7use 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*/
17class GeoLocationRepository extends ServiceEntityRepository
18{
19/**
20* @param ManagerRegistry $registry
21*/
22public function __construct(ManagerRegistry $registry)
23{
24parent::__construct($registry, GeoLocation::class);
25}
26
27/**
28* @param string $ip
29*
30* @return GeoLocation|null
31*/
32public function findOrCreateByIpAddress(string $ip): ?GeoLocation
33{
34$location = null;
35if (filter_var($ip, FILTER_VALIDATE_IP)) {
36$location = $this->findOneByIpAddress($ip);
37if (!$location) {
38$location = new GeoLocation();
39$location
40->setIpAddress($ip)
41;
42
43$this->getEntityManager()->persist($location);
44$this->getEntityManager()->flush();
45}
46}
47
48return $location;
49}
50
51/**
52* @param string $from
53* @param string $to
54*
55* @return int
56*/
57public 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
68return $qb->getQuery()->getSingleScalarResult();
69}
70}
71