1<?php 2/** 3 * This file is part of the FreeDSx LDAP package. 4 * 5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace FreeDSx\Ldap\Protocol\Factory; 12 13use FreeDSx\Ldap\Operation as Operation; 14use FreeDSx\Ldap\Operation\Request as Request; 15use FreeDSx\Ldap\Operation\Response as Response; 16use FreeDSx\Ldap\Operation\ResultCode; 17use FreeDSx\Ldap\Protocol\ClientProtocolHandler; 18use FreeDSx\Ldap\Protocol\ClientProtocolHandler\RequestHandlerInterface; 19use FreeDSx\Ldap\Protocol\ClientProtocolHandler\ResponseHandlerInterface; 20 21/** 22 * Retrieves the correct handler for a specific client protocol request / response. 23 * 24 * @author Chad Sikorra <Chad.Sikorra@gmail.com> 25 */ 26class ClientProtocolHandlerFactory 27{ 28 /** 29 * @param Request\RequestInterface $request 30 * @return RequestHandlerInterface 31 */ 32 public function forRequest(Request\RequestInterface $request): RequestHandlerInterface 33 { 34 if ($request instanceof Request\SearchRequest) { 35 return new ClientProtocolHandler\ClientSearchHandler(); 36 } elseif ($request instanceof Request\UnbindRequest) { 37 return new ClientProtocolHandler\ClientUnbindHandler(); 38 } elseif ($request instanceof Request\SaslBindRequest) { 39 return new ClientProtocolHandler\ClientSaslBindHandler(); 40 } else { 41 return new ClientProtocolHandler\ClientBasicHandler(); 42 } 43 } 44 45 46 /** 47 * @param Request\RequestInterface $request 48 * @param Response\ResponseInterface $response 49 * @return ResponseHandlerInterface 50 */ 51 public function forResponse(Request\RequestInterface $request, Response\ResponseInterface $response): ResponseHandlerInterface 52 { 53 if ($response instanceof Response\SearchResultDone || $response instanceof Response\SearchResultEntry || $response instanceof Response\SearchResultReference) { 54 return new ClientProtocolHandler\ClientSearchHandler(); 55 } elseif ($response instanceof Operation\LdapResult && $response->getResultCode() === ResultCode::REFERRAL) { 56 return new ClientProtocolHandler\ClientReferralHandler(); 57 } elseif ($request instanceof Request\ExtendedRequest && $request->getName() === Request\ExtendedRequest::OID_START_TLS) { 58 return new ClientProtocolHandler\ClientStartTlsHandler(); 59 } elseif ($response instanceof Response\ExtendedResponse) { 60 return new ClientProtocolHandler\ClientExtendedOperationHandler(); 61 } else { 62 return new ClientProtocolHandler\ClientBasicHandler(); 63 } 64 } 65} 66