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