PoolService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace IPv4Bundle\Services;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityRepository;
  5. use HostBundle\Entity\HostType;
  6. use IPv4Bundle\Entity\Pool;
  7. use KeaBundle\Entity\Lease4;
  8. class PoolService
  9. {
  10. /**
  11. * @var EntityRepository
  12. */
  13. private $poolRepository;
  14. /**
  15. * @param EntityManager $em
  16. */
  17. public function __construct(EntityManager $em)
  18. {
  19. $this->poolRepository = $em->getRepository(Pool::class);
  20. }
  21. /**
  22. * Retorna el primer pool donde se encuentra el lease
  23. *
  24. * @param Lease4 $lease
  25. *
  26. * @return Pool
  27. */
  28. public function getPoolFromLease(Lease4 $lease)
  29. {
  30. $address = ip2long($lease->getAddress());
  31. $pools = $this->poolRepository->findAll();
  32. foreach ($pools as $pool) {
  33. $firstIp = ip2long($pool->getFirstIp());
  34. $lastIp = ip2long($pool->getLastIp());
  35. if ($firstIp <= $address && $address <= $lastIp) {
  36. return $pool;
  37. }
  38. }
  39. return null;
  40. }
  41. /**
  42. * Retorna el primer pool donde se encuentra la $fixedIP
  43. *
  44. * @param string $fixedIP
  45. *
  46. * @return Pool
  47. */
  48. public function getPoolFromFixedIP($fixedIP)
  49. {
  50. $address = ip2long($fixedIP);
  51. $pools = $this->poolRepository->findAll();
  52. foreach ($pools as $pool) {
  53. $firstIp = ip2long($pool->getFirstIp());
  54. $lastIp = ip2long($pool->getLastIp());
  55. if ($firstIp <= $address && $address <= $lastIp) {
  56. return $pool;
  57. }
  58. }
  59. return null;
  60. }
  61. /**
  62. * @param HostType $hostType
  63. *
  64. * @return array
  65. */
  66. public function getStaticPoolIPRange(HostType $hostType = null)
  67. {
  68. $count = 1;
  69. $range = [];
  70. $pools = $this->poolRepository->findAllStaticByHostType($hostType);
  71. foreach ($pools as $pool) {
  72. $range[] = $pool->getFirstIp();
  73. $firstIp = ip2long($pool->getFirstIp());
  74. $lastIp = ip2long($pool->getLastIp());
  75. $currentIP = $firstIp + 1;
  76. while ($currentIP <= $lastIp) {
  77. $range[] = long2ip($currentIP);
  78. $currentIP++;
  79. }
  80. }
  81. sort($range);
  82. return $range;
  83. }
  84. }