Completed
Pull Request — master (#11)
by Alexander
02:53
created

BeginRequest::packPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.9666
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * Protocol FCGI library
4
 *
5
 * @copyright Copyright 2021. Lisachenko Alexander <[email protected]>
6
 * This source file is subject to the license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Lisachenko\Protocol\FCGI\Record;
13
14
use Lisachenko\Protocol\FCGI;
15
use Lisachenko\Protocol\FCGI\Record;
16
17
/**
18
 * The Web server sends a FCGI_BEGIN_REQUEST record to start a request.
19
 *
20
 * @author Alexander.Lisachenko
21
 */
22
class BeginRequest extends Record
23
{
24
    /**
25
     * The role component sets the role the Web server expects the application to play.
26
     * The currently-defined roles are:
27
     *   FCGI_RESPONDER
28
     *   FCGI_AUTHORIZER
29
     *   FCGI_FILTER
30
     */
31
    protected int $role = FCGI::UNKNOWN_ROLE;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
32
33
    /**
34
     * The flags component contains a bit that controls connection shutdown.
35
     *
36
     * flags & FCGI_KEEP_CONN:
37
     *   If zero, the application closes the connection after responding to this request.
38
     *   If not zero, the application does not close the connection after responding to this request;
39
     *   the Web server retains responsibility for the connection.
40
     */
41
    protected int $flags;
42
43
    /**
44
     * Reserved data, 5 bytes maximum
45
     */
46
    protected string $reserved1;
47
48 3
    public function __construct(int $role = FCGI::UNKNOWN_ROLE, int $flags = 0, string $reserved = '')
49
    {
50 3
        $this->type      = FCGI::BEGIN_REQUEST;
51 3
        $this->role      = $role;
52 3
        $this->flags     = $flags;
53 3
        $this->reserved1 = $reserved;
54 3
        $this->setContentData($this->packPayload());
55 3
    }
56
57
    /**
58
     * Returns the role
59
     *
60
     * The role component sets the role the Web server expects the application to play.
61
     * The currently-defined roles are:
62
     *   FCGI_RESPONDER
63
     *   FCGI_AUTHORIZER
64
     *   FCGI_FILTER
65
     */
66
    public function getRole(): int
67
    {
68 2
        return $this->role;
69
    }
70 2
71
    /**
72
     * Returns the flags
73
     *
74
     * The flags component contains a bit that controls connection shutdown.
75
     *
76
     * flags & FCGI_KEEP_CONN:
77
     *   If zero, the application closes the connection after responding to this request.
78
     *   If not zero, the application does not close the connection after responding to this request;
79
     *   the Web server retains responsibility for the connection.
80
     */
81
    public function getFlags(): int
82
    {
83
        return $this->flags;
84
    }
85 2
86
    /**
87 2
     * {@inheritdoc}
88
     */
89
    protected static function unpackPayload($self, string $binaryData): void
90
    {
91
        assert($self instanceof self);
92
93
        /** @phpstan-var false|array{role: int, flags: int, reserved: string} */
94
        $payload = unpack("nrole/Cflags/a5reserved", $binaryData);
95
        if ($payload === false) {
96
            throw new \RuntimeException('Can not unpack data from the binary buffer');
97
        }
98 2
        [
99
            $self->role,
100
            $self->flags,
101 2
            $self->reserved1
102 2
        ] = array_values($payload);
103 2
    }
104 2
105
    /**
106 2
     * {@inheritdoc}
107
     */
108
    protected function packPayload(): string
109
    {
110
        return pack(
111
            "nCa5",
112
            $this->role,
113
            $this->flags,
114 3
            $this->reserved1
115
        );
116 3
    }
117
}
118