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