<commit_before><?php

namespace BinSoul\Net\Mqtt\Packet;

use BinSoul\Net\Mqtt\Exception\MalformedPacketException;
use BinSoul\Net\Mqtt\PacketStream;
use BinSoul\Net\Mqtt\Packet;

/**
 * Represents the PUBLISH packet.
 */
class PublishRequestPacket extends BasePacket
{
    use IdentifiablePacket;

    /** @var string */
    private $topic;
    /** @var string */
    private $payload;
    /** @var bool */
    private $isDuplicate;
    /** @var bool */
    private $isRetained;
    /** @var int */
    private $qosLevel;

    protected $packetType = Packet::TYPE_PUBLISH;

    public function read(PacketStream $stream)
    {
        parent::read($stream);
        $this->assertRemainingPacketLength();

        $this->isDuplicate = $this->packetFlags & 8;
        $this->isRetained = $this->packetFlags & 1;
        $this->qosLevel = ($this->packetFlags & 6) >> 1;
        if ($this->qosLevel == 3) {
            throw new MalformedPacketException(
                sprintf(
                    'Malformed quality of service level: type=%02x, flags=%02x, length=%02x',
                    $this->packetType,
                    $this->packetFlags,
                    $this->remainingPacketLength
                )
            );
        }

        $originalPosition = $stream->getPosition();
        $this->topic = $stream->readString();
        $this->identifier = null;
        if ($this->qosLevel > 0) {
            $this->identifier = $stream->readWord();
        }

        $payloadLength = $this->remainingPacketLength - ($stream->getPosition() - $originalPosition);
        $this->payload = $stream->read($payloadLength);

        $this->assertValidString($this->topic);
    }

    public function getResponse()
    {
        $result = null;

        if ($this->qosLevel == 1) {
            $result = new PublishAckPacket();
            $result->setIdentifier($this->identifier);
        }

        if ($this->qosLevel == 2) {
            $result = new PublishReceivedPacket();
            $result->setIdentifier($this->identifier);
        }

        return $result;
    }

    /**
     * Returns the topic.
     *
     * @return string
     */
    public function getTopic()
    {
        return $this->topic;
    }

    /**
     * Sets the topic.
     *
     * @param string $value
     */
    public function setTopic($value)
    {
        $this->assertValidString($value);
        if (strlen($value) == 0) {
            throw new \InvalidArgumentException('The topic must not be empty.');
        }

        $this->topic = $value;
    }

    /**
     * Returns the payload.
     *
     * @return string
     */
    public function getPayload()
    {
        return $this->payload;
    }

    /**
     * Sets the payload.
     *
     * @param string $value
     */
    public function setPayload($value)
    {
        $this->payload = $value;
    }

    /**
     * Indicates if the packet is a duplicate.
     *
     * @return bool
     */
    public function isDuplicate()
    {
        return $this->isDuplicate;
    }

    /**
     * Marks the packet as duplicate.
     *
     * @param bool $value
     */
    public function setDuplicate($value)
    {
        $this->isDuplicate = $value;
    }

    /**
     * Indicates if the packet is retained.
     *
     * @return bool
     */
    public function isRetained()
    {
        return $this->isRetained;
    }

    /**
     * Marks the packet as retained.
     *
     * @param bool $value
     */
    public function setRetained($value)
    {
        $this->isRetained = $value;
    }

    /**
     * Returns the quality of service level.
     *
     * @return int
     */
    public function getQosLevel()
    {
        return $this->qosLevel;
    }

    /**
     * Sets the quality of service level.
     *
     * @param int $value
     */
    public function setQosLevel($value)
    {
        $this->qosLevel = $value;
    }
}
<commit_msg>Remove unused method<commit_after><?php

namespace BinSoul\Net\Mqtt\Packet;

use BinSoul\Net\Mqtt\Exception\MalformedPacketException;
use BinSoul\Net\Mqtt\PacketStream;
use BinSoul\Net\Mqtt\Packet;

/**
 * Represents the PUBLISH packet.
 */
class PublishRequestPacket extends BasePacket
{
    use IdentifiablePacket;

    /** @var string */
    private $topic;
    /** @var string */
    private $payload;
    /** @var bool */
    private $isDuplicate;
    /** @var bool */
    private $isRetained;
    /** @var int */
    private $qosLevel;

    protected $packetType = Packet::TYPE_PUBLISH;

    public function read(PacketStream $stream)
    {
        parent::read($stream);
        $this->assertRemainingPacketLength();

        $this->isDuplicate = $this->packetFlags & 8;
        $this->isRetained = $this->packetFlags & 1;
        $this->qosLevel = ($this->packetFlags & 6) >> 1;
        if ($this->qosLevel == 3) {
            throw new MalformedPacketException(
                sprintf(
                    'Malformed quality of service level: type=%02x, flags=%02x, length=%02x',
                    $this->packetType,
                    $this->packetFlags,
                    $this->remainingPacketLength
                )
            );
        }

        $originalPosition = $stream->getPosition();
        $this->topic = $stream->readString();
        $this->identifier = null;
        if ($this->qosLevel > 0) {
            $this->identifier = $stream->readWord();
        }

        $payloadLength = $this->remainingPacketLength - ($stream->getPosition() - $originalPosition);
        $this->payload = $stream->read($payloadLength);

        $this->assertValidString($this->topic);
    }

    /**
     * Returns the topic.
     *
     * @return string
     */
    public function getTopic()
    {
        return $this->topic;
    }

    /**
     * Sets the topic.
     *
     * @param string $value
     */
    public function setTopic($value)
    {
        $this->assertValidString($value);
        if (strlen($value) == 0) {
            throw new \InvalidArgumentException('The topic must not be empty.');
        }

        $this->topic = $value;
    }

    /**
     * Returns the payload.
     *
     * @return string
     */
    public function getPayload()
    {
        return $this->payload;
    }

    /**
     * Sets the payload.
     *
     * @param string $value
     */
    public function setPayload($value)
    {
        $this->payload = $value;
    }

    /**
     * Indicates if the packet is a duplicate.
     *
     * @return bool
     */
    public function isDuplicate()
    {
        return $this->isDuplicate;
    }

    /**
     * Marks the packet as duplicate.
     *
     * @param bool $value
     */
    public function setDuplicate($value)
    {
        $this->isDuplicate = $value;
    }

    /**
     * Indicates if the packet is retained.
     *
     * @return bool
     */
    public function isRetained()
    {
        return $this->isRetained;
    }

    /**
     * Marks the packet as retained.
     *
     * @param bool $value
     */
    public function setRetained($value)
    {
        $this->isRetained = $value;
    }

    /**
     * Returns the quality of service level.
     *
     * @return int
     */
    public function getQosLevel()
    {
        return $this->qosLevel;
    }

    /**
     * Sets the quality of service level.
     *
     * @param int $value
     */
    public function setQosLevel($value)
    {
        $this->qosLevel = $value;
    }
}

{'fraction_non_alphanumeric': 0.13169222491256388, 'fraction_numerical': 0.003766478342749529, 'mean_word_length': 1.7629134150873282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4873999', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2163, 'n_words': 479}