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

sirn-se / websocket-php / 5610959835

pending completion
5610959835

push

github

Sören Jensen
Middleware support

14 of 14 new or added lines in 4 files covered. (100.0%)

283 of 676 relevant lines covered (41.86%)

1.66 hits per line

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

83.33
/src/Middleware/MiddlewareHandler.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2023 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Middleware;
11

12
use Closure;
13
use Psr\Log\{
14
    LoggerInterface,
15
    LoggerAwareInterface,
16
    NullLogger
17
};
18
use WebSocket\Connection;
19
use WebSocket\Message\Message;
20

21
/**
22
 * WebSocket\Middleware\MiddlewareHandler class.
23
 * Middleware handling.
24
 */
25
class MiddlewareHandler implements LoggerAwareInterface
26
{
27
    private $incoming = [];
28
    private $outgoing = [];
29

30
    public function __construct()
31
    {
32
        $this->setLogger(new NullLogger());
12✔
33
    }
34

35
    public function setLogger(LoggerInterface $logger): void
36
    {
37
        $this->logger = $logger;
12✔
38
        foreach ($this->incoming as $middleware) {
12✔
39
            if ($middleware instanceof LoggerAwareInterface) {
×
40
                $middleware->setLogger($this->logger);
×
41
            }
42
        }
43
        foreach ($this->outgoing as $middleware) {
12✔
44
            if ($middleware instanceof LoggerAwareInterface) {
×
45
                $middleware->setLogger($this->logger);
×
46
            }
47
        }
48
    }
49

50
    public function add(MiddlewareInterface $middleware): void
51
    {
52
        if ($middleware instanceof ProcessIncomingInterface) {
1✔
53
            $this->logger->info("[middleware-handler] Added incoming: {$middleware}");
1✔
54
            if ($middleware instanceof LoggerAwareInterface) {
1✔
55
                $middleware->setLogger($this->logger);
1✔
56
            }
57
            $this->incoming[] = $middleware;
1✔
58
        }
59
        if ($middleware instanceof ProcessOutgoingInterface) {
1✔
60
            $this->logger->info("[middleware-handler] Added outgoing: {$middleware}");
1✔
61
            if ($middleware instanceof LoggerAwareInterface) {
1✔
62
                $middleware->setLogger($this->logger);
1✔
63
            }
64
            $this->outgoing[] = $middleware;
1✔
65
        }
66
    }
67

68
    public function processIncoming(Connection $connection, Closure $cb): Message
69
    {
70
        $this->logger->info("[middleware-handler] Processing incoming");
2✔
71
        $stack = new ProcessStack($connection, $this->incoming, $cb);
2✔
72
        return $stack->handleIncoming();
2✔
73
    }
74

75
    public function processOutgoing(Connection $connection, Message $message, Closure $cb): Message
76
    {
77
        $this->logger->info("[middleware-handler] Processing outgoing");
9✔
78
        $stack = new ProcessStack($connection, $this->outgoing, $cb);
9✔
79
        return $stack->handleOutgoing($message);
9✔
80
    }
81
}
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