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\ClientProtocolHandler;
12
13use FreeDSx\Ldap\Exception\ConnectionException;
14use FreeDSx\Ldap\Operation\Response\ExtendedResponse;
15use FreeDSx\Ldap\Operation\ResultCode;
16use FreeDSx\Ldap\Protocol\LdapMessageRequest;
17use FreeDSx\Ldap\Protocol\LdapMessageResponse;
18use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
19
20/**
21 * Logic for handling a StartTLS operation.
22 *
23 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
24 */
25class ClientStartTlsHandler implements ResponseHandlerInterface
26{
27    /**
28     * {@inheritDoc}
29     */
30    public function handleResponse(LdapMessageRequest $messageTo, LdapMessageResponse $messageFrom, ClientQueue $queue, array $options): ?LdapMessageResponse
31    {
32        /** @var ExtendedResponse $response */
33        $response = $messageFrom->getResponse();
34
35        if ($response->getResultCode() !== ResultCode::SUCCESS) {
36            throw new ConnectionException(sprintf(
37                'Unable to start TLS: %s',
38                $response->getDiagnosticMessage()
39            ));
40        }
41        $queue->encrypt();
42
43        return $messageFrom;
44    }
45}
46