1<?php 2 3/** 4 * This file is part of the FreeDSx LDAP package. 5 * 6 * (c) Chad Sikorra <Chad.Sikorra@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace FreeDSx\Ldap\Server\RequestHandler; 13 14use FreeDSx\Ldap\LdapClient; 15use FreeDSx\Ldap\Search\Paging; 16use FreeDSx\Ldap\Server\Paging\PagingRequest; 17use FreeDSx\Ldap\Server\Paging\PagingResponse; 18use FreeDSx\Ldap\Server\RequestContext; 19 20/** 21 * Proxies paging requests to an an existing LDAP client. 22 * 23 * @author Chad Sikorra <Chad.Sikorra@gmail.com> 24 */ 25class ProxyPagingHandler implements PagingHandlerInterface 26{ 27 /** 28 * @var LdapClient 29 */ 30 private $client; 31 32 /** 33 * @var array<string, Paging> 34 */ 35 private $pagers = []; 36 37 /** 38 * @param LdapClient $client 39 */ 40 public function __construct(LdapClient $client) 41 { 42 $this->client = $client; 43 } 44 45 /** 46 * @inheritDoc 47 */ 48 public function page( 49 PagingRequest $pagingRequest, 50 RequestContext $context 51 ): PagingResponse { 52 $paging = $this->getPagerForClient($pagingRequest); 53 $entries = $paging->getEntries($pagingRequest->getSize()); 54 55 if (!$paging->hasEntries()) { 56 return PagingResponse::makeFinal($entries); 57 } else { 58 return PagingResponse::make( 59 $entries, 60 $paging->sizeEstimate() ?? 0 61 ); 62 } 63 } 64 65 /** 66 * @inheritDoc 67 */ 68 public function remove( 69 PagingRequest $pagingRequest, 70 RequestContext $context 71 ): void { 72 if (isset($this->pagers[$pagingRequest->getUniqueId()])) { 73 unset($this->pagers[$pagingRequest->getUniqueId()]); 74 } 75 } 76 77 private function getPagerForClient(PagingRequest $pagingRequest): Paging 78 { 79 if (!isset($this->pagers[$pagingRequest->getUniqueId()])) { 80 $this->pagers[$pagingRequest->getUniqueId()] = $this->client->paging( 81 $pagingRequest->getSearchRequest(), 82 $pagingRequest->getSize() 83 ); 84 $this->pagers[$pagingRequest->getUniqueId()]->isCritical( 85 $pagingRequest->isCritical() 86 ); 87 } 88 89 return $this->pagers[$pagingRequest->getUniqueId()]; 90 } 91} 92