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