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\Queue;
13
14use FreeDSx\Asn1\Exception\EncoderException;
15use FreeDSx\Asn1\Exception\PartialPduException;
16use FreeDSx\Ldap\Exception\ProtocolException;
17use FreeDSx\Ldap\Exception\RuntimeException;
18use FreeDSx\Ldap\Exception\UnsolicitedNotificationException;
19use FreeDSx\Ldap\Protocol\LdapMessage;
20use FreeDSx\Ldap\Protocol\LdapMessageRequest;
21use FreeDSx\Ldap\Protocol\LdapMessageResponse;
22use FreeDSx\Ldap\Protocol\LdapQueue;
23use FreeDSx\Socket\Exception\ConnectionException;
24use FreeDSx\Socket\Queue\Message;
25
26/**
27 * The LDAP Queue class for sending and receiving messages for servers.
28 *
29 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
30 */
31class ServerQueue extends LdapQueue
32{
33    /**
34     * @param int|null $id
35     * @return LdapMessageRequest
36     * @throws ProtocolException
37     * @throws UnsolicitedNotificationException
38     * @throws ConnectionException
39     */
40    public function getMessage(?int $id = null): LdapMessageRequest
41    {
42        $message = $this->getAndValidateMessage($id);
43
44        if (!$message instanceof LdapMessageRequest) {
45            throw new ProtocolException(sprintf(
46                'Expected an instance of LdapMessageResponse but got: %s',
47                get_class($message)
48            ));
49        }
50
51        return $message;
52    }
53
54    /**
55     * @param LdapMessageResponse ...$response
56     * @return $this
57     * @throws EncoderException
58     */
59    public function sendMessage(LdapMessageResponse ...$response): self
60    {
61        $this->sendLdapMessage(...$response);
62
63        return $this;
64    }
65
66    /**
67     * @param Message $message
68     * @param int|null $id
69     * @return LdapMessage
70     * @throws ProtocolException
71     * @throws EncoderException
72     * @throws PartialPduException
73     * @throws RuntimeException
74     */
75    protected function constructMessage(Message $message, ?int $id = null)
76    {
77        return LdapMessageRequest::fromAsn1($message->getMessage());
78    }
79}
80