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\ClientProtocolHandler;
13
14use FreeDSx\Asn1\Exception\EncoderException;
15use FreeDSx\Ldap\Control\Control;
16use FreeDSx\Ldap\Entry\Entry;
17use FreeDSx\Ldap\Exception\BindException;
18use FreeDSx\Ldap\Exception\ConnectionException;
19use FreeDSx\Ldap\Exception\OperationException;
20use FreeDSx\Ldap\Exception\ProtocolException;
21use FreeDSx\Ldap\Exception\ReferralException;
22use FreeDSx\Ldap\Exception\UnsolicitedNotificationException;
23use FreeDSx\Ldap\Operation\Request\RequestInterface;
24use FreeDSx\Ldap\Protocol\ClientProtocolHandler;
25use FreeDSx\Ldap\Protocol\LdapMessageRequest;
26use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
27use FreeDSx\Sasl\Exception\SaslException;
28
29/**
30 * Contains client protocol specific details that get passed to the request handlers.
31 *
32 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
33 */
34class ClientProtocolContext
35{
36    /**
37     * @var ClientProtocolHandler
38     */
39    protected $protocolHandler;
40
41    /**
42     * @var ClientQueue
43     */
44    protected $clientQueue;
45
46    /**
47     * @var array
48     */
49    protected $options;
50
51    /**
52     * @var RequestInterface
53     */
54    protected $request;
55
56    /**
57     * @var Control[]
58     */
59    protected $controls;
60
61    /**
62     * @var LdapMessageRequest
63     */
64    protected $sentMessage;
65
66    public function __construct(
67        RequestInterface $request,
68        array $controls,
69        ClientProtocolHandler $protocolHandler,
70        ClientQueue $queue,
71        array $options
72    ) {
73        $this->request = $request;
74        $this->controls = $controls;
75        $this->protocolHandler = $protocolHandler;
76        $this->clientQueue = $queue;
77        $this->options = $options;
78    }
79
80    public function getRequest(): RequestInterface
81    {
82        return $this->request;
83    }
84
85    /**
86     * @return Control[]
87     * @psalm-return array<array-key, Control>
88     */
89    public function getControls(): array
90    {
91        return $this->controls;
92    }
93
94    public function getProtocolHandler(): ClientProtocolHandler
95    {
96        return $this->protocolHandler;
97    }
98
99    public function getQueue(): ClientQueue
100    {
101        return $this->clientQueue;
102    }
103
104    public function getOptions(): array
105    {
106        return $this->options;
107    }
108
109    public function messageToSend(): LdapMessageRequest
110    {
111        if ($this->sentMessage !== null) {
112            return $this->sentMessage;
113        }
114        $this->sentMessage = new LdapMessageRequest(
115            $this->clientQueue->generateId(),
116            $this->request,
117            ...$this->controls
118        );
119
120        return $this->sentMessage;
121    }
122
123    /**
124     * @param bool $reload force reload the RootDSE
125     * @return Entry
126     * @throws ConnectionException
127     * @throws OperationException
128     * @throws UnsolicitedNotificationException
129     * @throws \FreeDSx\Socket\Exception\ConnectionException
130     * @throws EncoderException
131     * @throws BindException
132     * @throws ProtocolException
133     * @throws ReferralException
134     * @throws SaslException
135     */
136    public function getRootDse(bool $reload = false): Entry
137    {
138        return $this->protocolHandler->fetchRootDse($reload);
139    }
140}
141