Completed
Push — master ( df1767...093280 )
by Camilo
02:16
created

ReadableContent   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 55
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiateObject() 0 7 1
A checkControlPacketValue() 0 12 2
A performSpecialActions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\Exceptions\InvalidResponseType;
8
9
/**
10
 * Trait ReadableContent
11
 * @package unreal4u\MQTT\Internals
12
 */
13
trait ReadableContent
14
{
15
    /**
16
     * Length of variable header
17
     * @var int
18
     */
19
    protected $variableHeaderSize = 0;
20
21 5
    final public function instantiateObject(string $rawMQTTHeaders, ClientInterface $client): bool
22
    {
23
        //var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests
24 5
        $this->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4);
25 4
        $this->fillObject($rawMQTTHeaders, $client);
26
27 4
        return true;
28
    }
29
30
    /**
31
     * Checks whether the control packet corresponds to this object
32
     *
33
     * @param int $controlPacketValue
34
     * @return bool
35
     * @throws \unreal4u\MQTT\Exceptions\InvalidResponseType
36
     */
37 5
    private function checkControlPacketValue(int $controlPacketValue): bool
38
    {
39
        // Check whether the first byte corresponds to the expected control packet value
40 5
        if (static::CONTROL_PACKET_VALUE !== $controlPacketValue) {
0 ignored issues
show
Bug introduced by
The constant unreal4u\MQTT\Internals\...t::CONTROL_PACKET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41 1
            throw new InvalidResponseType(sprintf(
42 1
                'Value of received value does not correspond to response (Expected: %d, Actual: %d)',
43 1
                static::CONTROL_PACKET_VALUE,
44 1
                $controlPacketValue
45
            ));
46
        }
47
48 4
        return true;
49
    }
50
51
    /**
52
     * All classes must implement how to handle the object filling
53
     * @param string $rawMQTTHeaders
54
     * @param ClientInterface $client
55
     * @return ReadableContentInterface
56
     */
57
    abstract public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface;
58
59
    /**
60
     * Any class can overwrite the default behaviour
61
     * @param ClientInterface $client
62
     * @param WritableContentInterface $originalRequest
63
     * @return bool
64
     */
65 1
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
0 ignored issues
show
Unused Code introduced by
The parameter $client is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    public function performSpecialActions(/** @scrutinizer ignore-unused */ ClientInterface $client, WritableContentInterface $originalRequest): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $originalRequest is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    public function performSpecialActions(ClientInterface $client, /** @scrutinizer ignore-unused */ WritableContentInterface $originalRequest): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67 1
        return false;
68
    }
69
}
70