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\Protocol\ServerProtocolHandler;
13
14use FreeDSx\Asn1\Exception\EncoderException;
15use FreeDSx\Ldap\Exception\OperationException;
16use FreeDSx\Ldap\Operation\Request;
17use FreeDSx\Ldap\Operation\ResultCode;
18use FreeDSx\Ldap\Protocol\LdapMessageRequest;
19use FreeDSx\Ldap\Protocol\Queue\ServerQueue;
20use FreeDSx\Ldap\Server\RequestContext;
21use FreeDSx\Ldap\Server\RequestHandler\RequestHandlerInterface;
22use FreeDSx\Ldap\Server\Token\TokenInterface;
23
24/**
25 * Handles generic requests that can be sent to the user supplied dispatcher / handler.
26 *
27 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
28 */
29class ServerDispatchHandler extends BaseServerHandler implements ServerProtocolHandlerInterface
30{
31    /**
32     * @param LdapMessageRequest $message
33     * @param TokenInterface $token
34     * @param RequestHandlerInterface $dispatcher
35     * @param ServerQueue $queue
36     * @param array $options
37     * @throws OperationException
38     * @throws EncoderException
39     */
40    public function handleRequest(LdapMessageRequest $message, TokenInterface $token, RequestHandlerInterface $dispatcher, ServerQueue $queue, array $options): void
41    {
42        $context = new RequestContext($message->controls(), $token);
43        $request = $message->getRequest();
44
45        if ($request instanceof Request\AddRequest) {
46            $dispatcher->add($context, $request);
47        } elseif ($request instanceof Request\CompareRequest) {
48            $dispatcher->compare($context, $request);
49        } elseif ($request instanceof Request\DeleteRequest) {
50            $dispatcher->delete($context, $request);
51        } elseif ($request instanceof Request\ModifyDnRequest) {
52            $dispatcher->modifyDn($context, $request);
53        } elseif ($request instanceof Request\ModifyRequest) {
54            $dispatcher->modify($context, $request);
55        } elseif ($request instanceof Request\ExtendedRequest) {
56            $dispatcher->extended($context, $request);
57        } else {
58            throw new OperationException(
59                'The requested operation is not supported.',
60                ResultCode::NO_SUCH_OPERATION
61            );
62        }
63
64        $queue->sendMessage($this->responseFactory->getStandardResponse($message));
65    }
66}
67