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\Factory;
13
14use FreeDSx\Ldap\Operation\LdapResult;
15use FreeDSx\Ldap\Operation\Request\AddRequest;
16use FreeDSx\Ldap\Operation\Request\BindRequest;
17use FreeDSx\Ldap\Operation\Request\CompareRequest;
18use FreeDSx\Ldap\Operation\Request\DeleteRequest;
19use FreeDSx\Ldap\Operation\Request\ExtendedRequest;
20use FreeDSx\Ldap\Operation\Request\ModifyDnRequest;
21use FreeDSx\Ldap\Operation\Request\ModifyRequest;
22use FreeDSx\Ldap\Operation\Request\SearchRequest;
23use FreeDSx\Ldap\Operation\Response\AddResponse;
24use FreeDSx\Ldap\Operation\Response\BindResponse;
25use FreeDSx\Ldap\Operation\Response\CompareResponse;
26use FreeDSx\Ldap\Operation\Response\DeleteResponse;
27use FreeDSx\Ldap\Operation\Response\ExtendedResponse;
28use FreeDSx\Ldap\Operation\Response\ModifyDnResponse;
29use FreeDSx\Ldap\Operation\Response\ModifyResponse;
30use FreeDSx\Ldap\Operation\Response\SearchResultDone;
31use FreeDSx\Ldap\Operation\ResultCode;
32use FreeDSx\Ldap\Protocol\LdapMessageRequest;
33use FreeDSx\Ldap\Protocol\LdapMessageResponse;
34
35/**
36 * For a specific request and result code/diagnostic, get the response object if possible.
37 *
38 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
39 */
40class ResponseFactory
41{
42    /**
43     * Retrieve the expected response type for the request that was given.
44     */
45    public function getStandardResponse(LdapMessageRequest $message, int $resultCode = ResultCode::SUCCESS, string $diagnostic = ''): LdapMessageResponse
46    {
47        $response = null;
48        $request = $message->getRequest();
49
50        if ($request instanceof BindRequest) {
51            $response = new BindResponse(new LdapResult($resultCode, '', $diagnostic));
52        } elseif ($request instanceof SearchRequest) {
53            $response = new SearchResultDone($resultCode, '', $diagnostic);
54        } elseif ($request instanceof AddRequest) {
55            $response = new AddResponse($resultCode, $request->getEntry()->getDn()->toString(), $diagnostic);
56        } elseif ($request instanceof CompareRequest) {
57            $response = new CompareResponse($resultCode, $request->getDn()->toString(), $diagnostic);
58        } elseif ($request instanceof DeleteRequest) {
59            $response = new DeleteResponse($resultCode, $request->getDn()->toString(), $diagnostic);
60        } elseif ($request instanceof ModifyDnRequest) {
61            $response = new ModifyDnResponse($resultCode, $request->getDn()->toString(), $diagnostic);
62        } elseif ($request instanceof ModifyRequest) {
63            $response = new ModifyResponse($resultCode, $request->getDn()->toString(), $diagnostic);
64        } elseif ($request instanceof ExtendedRequest) {
65            $response = new ExtendedResponse(new LdapResult($resultCode, '', $diagnostic));
66        } else {
67            return $this->getExtendedError('Invalid request.', ResultCode::OPERATIONS_ERROR);
68        }
69
70        return new LdapMessageResponse(
71            $message->getMessageId(),
72            $response
73        );
74    }
75
76    /**
77     * Retrieve an extended error, which has a message ID of zero.
78     */
79    public function getExtendedError(string $message, int $errorCode, ?string $responseName = null): LdapMessageResponse
80    {
81        return new LdapMessageResponse(
82            0,
83            new ExtendedResponse(new LdapResult($errorCode, '', $message), $responseName)
84        );
85    }
86}
87