1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Hyphper\Frame; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait for frames that contain priority data. Defines extra fields that can |
7
|
|
|
* be used and set by frames that contain priority data. |
8
|
|
|
* |
9
|
|
|
* @package Hyphper\Frame |
10
|
|
|
*/ |
11
|
|
|
trait PriorityTrait |
12
|
|
|
{ |
13
|
|
|
protected $depends_on; |
14
|
|
|
protected $stream_weight; |
15
|
|
|
protected $exclusive; |
16
|
|
|
|
17
|
6 |
View Code Duplication |
public function __construct(array $options = []) |
|
|
|
|
18
|
|
|
{ |
19
|
6 |
|
parent::__construct($options); |
20
|
5 |
|
$this->depends_on = (int) ($options['depends_on'] ?? 0); |
21
|
5 |
|
$this->stream_weight = (int) ($options['stream_weight'] ?? 0); |
22
|
5 |
|
$this->exclusive = (bool) ($options['exclusive'] ?? false); |
23
|
5 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
2 |
|
public function getExclusive() |
29
|
|
|
{ |
30
|
2 |
|
return $this->exclusive; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param bool $exclusive |
35
|
|
|
* |
36
|
|
|
* @return PriorityTrait |
|
|
|
|
37
|
|
|
*/ |
38
|
2 |
|
public function setExclusive(bool $exclusive) |
39
|
|
|
{ |
40
|
2 |
|
$this->exclusive = $exclusive; |
41
|
|
|
|
42
|
2 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
2 |
|
public function getDependsOn() |
49
|
|
|
{ |
50
|
2 |
|
return $this->depends_on; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $depends_on |
55
|
|
|
* |
56
|
|
|
* @return PriorityTrait |
|
|
|
|
57
|
|
|
*/ |
58
|
2 |
|
public function setDependsOn($depends_on) |
59
|
|
|
{ |
60
|
2 |
|
$this->depends_on = $depends_on; |
61
|
|
|
|
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
2 |
|
public function getStreamWeight() |
69
|
|
|
{ |
70
|
2 |
|
return $this->stream_weight; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $stream_weight |
75
|
|
|
* |
76
|
|
|
* @return PriorityTrait |
|
|
|
|
77
|
|
|
*/ |
78
|
2 |
|
public function setStreamWeight($stream_weight) |
79
|
|
|
{ |
80
|
2 |
|
$this->stream_weight = $stream_weight; |
81
|
|
|
|
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
protected function serializePriorityData() |
86
|
|
|
{ |
87
|
3 |
|
return pack( |
88
|
3 |
|
'NC', |
89
|
3 |
|
$this->depends_on | ((int) ($this->exclusive) << 31), |
90
|
3 |
|
$this->stream_weight |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
protected function parsePriorityData(string $data): int |
95
|
|
|
{ |
96
|
3 |
|
if ($unpack = @unpack('Ndepends_on/Cstream_weight', substr($data, 0, 5))) { |
97
|
2 |
|
$this->depends_on = $unpack['depends_on']; |
98
|
2 |
|
$this->stream_weight = $unpack['stream_weight']; |
99
|
2 |
|
$this->exclusive = (bool) ($this->depends_on & PriorityInterface::MASK); |
100
|
2 |
|
$this->depends_on &= ~PriorityInterface::MASK; |
101
|
|
|
|
102
|
2 |
|
return 5; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
throw new \Hyphper\Frame\Exception\InvalidFrameException('Invalid Priority data'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.