PoolService.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace IPv4Bundle\Services;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityRepository;
  5. use IPv4Bundle\Entity\Pool;
  6. use KeaBundle\Entity\Lease4;
  7. class PoolService
  8. {
  9. /**
  10. * @var EntityRepository
  11. */
  12. private $poolRepository;
  13. /**
  14. * @param EntityManager $em
  15. */
  16. public function __construct(EntityManager $em)
  17. {
  18. $this->poolRepository = $em->getRepository(Pool::class);
  19. }
  20. /**
  21. * Retorna el primer pool donde se encuentra el lease
  22. *
  23. * @param Lease4 $lease
  24. *
  25. * @return Pool
  26. */
  27. public function getPoolFromLease(Lease4 $lease)
  28. {
  29. $address = ip2long($lease->getAddress());
  30. $pools = $this->poolRepository->findAll();
  31. foreach ($pools as $pool) {
  32. $firstIp = ip2long($pool->getFirstIp());
  33. $lastIp = ip2long($pool->getLastIp());
  34. if ($firstIp <= $address && $address <= $lastIp) {
  35. return $pool;
  36. }
  37. }
  38. return null;
  39. }
  40. }