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

EndRequest::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 application sends a FCGI_END_REQUEST record to terminate a request, either because the application
19
 * has processed the request or because the application has rejected the request.
20
 *
21
 * @author Alexander.Lisachenko
22
 */
23
class EndRequest extends Record
24
{
25
    /**
26
     * The appStatus component is an application-level status code. Each role documents its usage of appStatus.
27
     */
28
    protected int $appStatus = 0;
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...
29
30
    /**
31
     * The protocolStatus component is a protocol-level status code.
32
     *
33
     * The possible protocolStatus values are:
34
     *   FCGI_REQUEST_COMPLETE: normal end of request.
35
     *   FCGI_CANT_MPX_CONN: rejecting a new request.
36
     *      This happens when a Web server sends concurrent requests over one connection to an application that is
37
     *      designed to process one request at a time per connection.
38
     *   FCGI_OVERLOADED: rejecting a new request.
39
     *      This happens when the application runs out of some resource, e.g. database connections.
40
     *   FCGI_UNKNOWN_ROLE: rejecting a new request.
41
     *      This happens when the Web server has specified a role that is unknown to the application.
42
     */
43
    protected int $protocolStatus = FCGI::REQUEST_COMPLETE;
44
45
    /**
46
     * Reserved data, 3 bytes maximum
47
     */
48
    protected string $reserved1;
49
50 2
    public function __construct(int $protocolStatus = FCGI::REQUEST_COMPLETE, int $appStatus = 0, string $reserved = '')
51
    {
52 2
        $this->type           = FCGI::END_REQUEST;
53 2
        $this->protocolStatus = $protocolStatus;
54 2
        $this->appStatus      = $appStatus;
55 2
        $this->reserved1      = $reserved;
56 2
        $this->setContentData($this->packPayload());
57 2
    }
58
59
    /**
60
     * Returns app status
61
     *
62
     * The appStatus component is an application-level status code. Each role documents its usage of appStatus.
63
     */
64
    public function getAppStatus(): int
65
    {
66 2
        return $this->appStatus;
67
    }
68 2
69
    /**
70
     * Returns the protocol status
71
     *
72
     * The possible protocolStatus values are:
73
     *   FCGI_REQUEST_COMPLETE: normal end of request.
74
     *   FCGI_CANT_MPX_CONN: rejecting a new request.
75
     *      This happens when a Web server sends concurrent requests over one connection to an application that is
76
     *      designed to process one request at a time per connection.
77
     *   FCGI_OVERLOADED: rejecting a new request.
78
     *      This happens when the application runs out of some resource, e.g. database connections.
79
     *   FCGI_UNKNOWN_ROLE: rejecting a new request.
80
     *      This happens when the Web server has specified a role that is unknown to the application.
81
     */
82
    public function getProtocolStatus(): int
83
    {
84
        return $this->protocolStatus;
85
    }
86 2
87
    /**
88 2
     * {@inheritdoc}
89
     */
90
    protected static function unpackPayload($self, string $binaryData): void
91
    {
92
        assert($self instanceof self);
93
94
        /** @phpstan-var false|array{appStatus: int, protocolStatus: int, reserved: string} */
95
        $payload = unpack("NappStatus/CprotocolStatus/a3reserved", $binaryData);
96
        if ($payload === false) {
97
            throw new \RuntimeException('Can not unpack data from the binary buffer');
98
        }
99 1
        [
100
            $self->appStatus,
101
            $self->protocolStatus,
102 1
            $self->reserved1
103 1
        ] = array_values($payload);
104 1
    }
105 1
106
    /**
107 1
     * {@inheritdoc}
108
     */
109
    protected function packPayload(): string
110
    {
111
        return pack(
112
            "NCa3",
113
            $this->appStatus,
114
            $this->protocolStatus,
115 2
            $this->reserved1
116
        );
117 2
    }
118
}
119