Completed
Push — master ( 325ba3...57475a )
by Camilo
98:41 queued 96:24
created

ReadableContent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 70
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiateObject() 0 7 1
A checkControlPacketValue() 0 12 2
A performSpecialActions() 0 3 1
A fillObject() 0 3 1
A extractPacketIdentifier() 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
use unreal4u\MQTT\Utilities;
9
10
/**
11
 * Trait ReadableContent
12
 * @package unreal4u\MQTT\Internals
13
 */
14
trait ReadableContent
15
{
16
    /**
17
     * Length of variable header
18
     * @var int
19
     */
20
    protected $variableHeaderSize = 0;
21
22 2
    final public function instantiateObject(string $rawMQTTHeaders, ClientInterface $client): bool
23
    {
24
        //var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests
25 2
        $this->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4);
26 1
        $this->fillObject($rawMQTTHeaders, $client);
27
28 1
        return true;
29
    }
30
31
    /**
32
     * Checks whether the control packet corresponds to this object
33
     *
34
     * @param int $controlPacketValue
35
     * @return bool
36
     * @throws \unreal4u\MQTT\Exceptions\InvalidResponseType
37
     */
38 2
    private function checkControlPacketValue(int $controlPacketValue): bool
39
    {
40
        // Check whether the first byte corresponds to the expected control packet value
41 2
        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...
42 1
            throw new InvalidResponseType(sprintf(
43 1
                'Value of received value does not correspond to response (Expected: %d, Actual: %d)',
44 1
                static::CONTROL_PACKET_VALUE,
45 1
                $controlPacketValue
46
            ));
47
        }
48
49 1
        return true;
50
    }
51
52
    /**
53
     * Extracts the packet identifier from the raw headers
54
     *
55
     * @param string $rawMQTTHeaders
56
     * @return int
57
     * @throws \OutOfRangeException
58
     */
59
    private function extractPacketIdentifier(string $rawMQTTHeaders): int
60
    {
61
        return Utilities::convertBinaryStringToNumber($rawMQTTHeaders{2} . $rawMQTTHeaders{3});
62
    }
63
64
    /**
65
     * Any class can overwrite the default behaviour (which is do nothing)
66
     * @param string $rawMQTTHeaders
67
     * @param ClientInterface $client
68
     * @return ReadableContentInterface
69
     */
70 1
    public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
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

70
    public function fillObject(string $rawMQTTHeaders, /** @scrutinizer ignore-unused */ ClientInterface $client): ReadableContentInterface

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 $rawMQTTHeaders 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

70
    public function fillObject(/** @scrutinizer ignore-unused */ string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface

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...
71
    {
72 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type unreal4u\MQTT\Internals\ReadableContent which is incompatible with the type-hinted return unreal4u\MQTT\Internals\ReadableContentInterface.
Loading history...
73
    }
74
75
    /**
76
     * Any class can overwrite the default behaviour
77
     * @param ClientInterface $client
78
     * @param WritableContentInterface $originalRequest
79
     * @return bool
80
     */
81
    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

81
    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

81
    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...
82
    {
83
        return false;
84
    }
85
}
86