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\Server;
13
14use FreeDSx\Ldap\Exception\ProtocolException;
15use FreeDSx\Ldap\Server\Paging\PagingRequests;
16
17/**
18 * Used to retain history regarding certain client request details.
19 *
20 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
21 */
22final class RequestHistory
23{
24    /**
25     * @var int[]
26     */
27    private $ids = [];
28
29    /**
30     * @var PagingRequests
31     */
32    private $pagingRequests;
33
34    public function __construct(PagingRequests $pagingRequests = null)
35    {
36        $this->pagingRequests = $pagingRequests ?? new PagingRequests();
37    }
38
39    /**
40     * Add a specific message ID that the client has used.
41     *
42     * @param int $id
43     * @throws ProtocolException
44     */
45    public function addId(int $id): void
46    {
47        if ($id === 0 || in_array($id, $this->ids, true)) {
48            throw new ProtocolException(sprintf(
49                'The message ID %s is not valid.',
50                $id
51            ));
52        }
53
54        $this->ids[] = $id;
55    }
56
57    /**
58     * The currently active paging requests from the client.
59     *
60     * @return PagingRequests
61     */
62    public function pagingRequest(): PagingRequests
63    {
64        return $this->pagingRequests;
65    }
66}
67