• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 8346487915

19 Mar 2024 04:15PM UTC coverage: 22.584% (-77.4%) from 100.0%
8346487915

push

github

sirn-se
Temp test verification

2 of 2 new or added lines in 1 file covered. (100.0%)

742 existing lines in 32 files now uncovered.

222 of 983 relevant lines covered (22.58%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.41
/src/Message/MessageHandler.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2024 Textalk and contributors.
5
 * This file is part of Websocket PHP and is free software under the ISC License.
6
 */
7

8
namespace WebSocket\Message;
9

10
use Psr\Log\{
11
    LoggerAwareInterface,
12
    LoggerInterface,
13
    NullLogger
14
};
15
use Stringable;
16
use WebSocket\Exception\BadOpcodeException;
17
use WebSocket\Frame\FrameHandler;
18
use WebSocket\Trait\StringableTrait;
19

20
/**
21
 * WebSocket\Message\MessageHandler class.
22
 * Message/Frame handling.
23
 */
24
class MessageHandler implements LoggerAwareInterface, Stringable
25
{
26
    use StringableTrait;
27

28
    private const DEFAULT_SIZE = 4096;
29

30
    private $frameHandler;
31
    private $logger;
32
    private $readBuffer;
33

34
    public function __construct(FrameHandler $frameHandler)
35
    {
36
        $this->frameHandler = $frameHandler;
1✔
37
        $this->setLogger(new NullLogger());
1✔
38
    }
39

40
    public function setLogger(LoggerInterface $logger): void
41
    {
42
        $this->logger = $logger;
1✔
43
        $this->frameHandler->setLogger($logger);
1✔
44
    }
45

46
    // Push message
47
    public function push(Message $message, int $size = self::DEFAULT_SIZE): Message
48
    {
UNCOV
49
        $frames = $message->getFrames($size);
×
UNCOV
50
        foreach ($frames as $frame) {
×
UNCOV
51
            $this->frameHandler->push($frame);
×
52
        }
UNCOV
53
        $this->logger->info("[message-handler] Pushed {$message}", [
×
UNCOV
54
            'opcode' => $message->getOpcode(),
×
UNCOV
55
            'content-length' => $message->getLength(),
×
UNCOV
56
            'frames' => count($frames),
×
UNCOV
57
        ]);
×
UNCOV
58
        return $message;
×
59
    }
60

61
    // Pull message
62
    public function pull(): Message
63
    {
64
        do {
UNCOV
65
            $frame = $this->frameHandler->pull();
×
UNCOV
66
            $final = $frame->isFinal();
×
UNCOV
67
            $continuation = $frame->isContinuation();
×
UNCOV
68
            $opcode = $frame->getOpcode();
×
UNCOV
69
            $payload = $frame->getPayload();
×
70

71
            // Continuation and factual opcode
UNCOV
72
            $payload_opcode = $continuation ? $this->readBuffer['opcode'] : $opcode;
×
73

74
            // First continuation frame, create buffer
UNCOV
75
            if (!$final && !$continuation) {
×
UNCOV
76
                $this->readBuffer = ['opcode' => $opcode, 'payload' => $payload, 'frames' => 1];
×
UNCOV
77
                continue; // Continue reading
×
78
            }
79

80
            // Subsequent continuation frames, add to buffer
UNCOV
81
            if ($continuation) {
×
UNCOV
82
                $this->readBuffer['payload'] .= $payload;
×
UNCOV
83
                $this->readBuffer['frames']++;
×
84
            }
UNCOV
85
        } while (!$final);
×
86

87
        // Final, return payload
UNCOV
88
        $frames = 1;
×
UNCOV
89
        if ($continuation) {
×
UNCOV
90
            $payload = $this->readBuffer['payload'];
×
UNCOV
91
            $frames = $this->readBuffer['frames'];
×
UNCOV
92
            $this->readBuffer = null;
×
93
        }
94

95
        // Create message instance
96
        switch ($payload_opcode) {
UNCOV
97
            case 'text':
×
UNCOV
98
                $message = new Text();
×
UNCOV
99
                break;
×
UNCOV
100
            case 'binary':
×
UNCOV
101
                $message = new Binary();
×
UNCOV
102
                break;
×
UNCOV
103
            case 'ping':
×
UNCOV
104
                $message = new Ping();
×
UNCOV
105
                break;
×
UNCOV
106
            case 'pong':
×
UNCOV
107
                $message = new Pong();
×
UNCOV
108
                break;
×
UNCOV
109
            case 'close':
×
UNCOV
110
                $message = new Close();
×
UNCOV
111
                break;
×
112
            default:
UNCOV
113
                throw new BadOpcodeException("Invalid opcode '{$payload_opcode}' provided");
×
114
        }
UNCOV
115
        $message->setPayload($payload);
×
116

UNCOV
117
        $this->logger->info("[message-handler] Pulled {$message}", [
×
UNCOV
118
            'opcode' => $message->getOpcode(),
×
UNCOV
119
            'content-length' => $message->getLength(),
×
UNCOV
120
            'frames' => $frames,
×
UNCOV
121
        ]);
×
122

UNCOV
123
        return $message;
×
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc