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\Operation\Request;
12
13use FreeDSx\Asn1\Asn1;
14use FreeDSx\Asn1\Type\AbstractType;
15use FreeDSx\Asn1\Type\IntegerType;
16use FreeDSx\Asn1\Type\SequenceType;
17use FreeDSx\Ldap\Exception\ProtocolException;
18use FreeDSx\Ldap\Protocol\LdapMessage;
19
20/**
21 * RFC 3909. A request to cancel an operation.
22 *
23 * cancelRequestValue ::= SEQUENCE {
24 *     cancelID        MessageID
25 *     -- MessageID is as defined in [RFC2251]
26 * }
27 *
28 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
29 */
30class CancelRequest extends ExtendedRequest
31{
32    /**
33     * @var int
34     */
35    protected $messageId;
36
37    /**
38     * @param int $messageId
39     */
40    public function __construct(int $messageId)
41    {
42        $this->messageId = $messageId;
43        parent::__construct(self::OID_CANCEL);
44    }
45
46    /**
47     * @return int
48     */
49    public function getMessageId(): int
50    {
51        return $this->messageId;
52    }
53
54    /**
55     * @return $this
56     */
57    public function setMessageId(int $messageId)
58    {
59        $this->messageId = $messageId;
60
61        return $this;
62    }
63
64    /**
65     * {@inheritdoc}
66     */
67    public function toAsn1(): AbstractType
68    {
69        $this->requestValue = Asn1::sequence(Asn1::integer($this->messageId));
70
71        return parent::toAsn1();
72    }
73
74    /**
75     * {@inheritdoc}
76     */
77    public static function fromAsn1(AbstractType $type)
78    {
79        $value = self::decodeEncodedValue($type);
80        if (!($value instanceof SequenceType && $value->getChild(0) instanceof IntegerType)) {
81            throw new ProtocolException('The cancel request value is malformed.');
82        }
83
84        return new self($value->getChild(0)->getValue());
85    }
86}
87