RoutesMarking::outbound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Marked of custom routes
4
 * User: moyo
5
 * Date: 2018/4/20
6
 * Time: 3:40 PM
7
 */
8
9
namespace Carno\HRPC\Client\Handlers;
10
11
use Carno\Chain\Layered;
12
use Carno\Cluster\Contracts\Tags;
13
use Carno\Coroutine\Context;
14
use Carno\HRPC\Client\Contracts\Defined;
15
use Carno\RPC\Protocol\Request;
16
use Throwable;
17
18
class RoutesMarking implements Layered
19
{
20
    /**
21
     * flag in ctx
22
     */
23
    public const FLAG = 'routes.marked';
24
25
    /**
26
     * @param Request $request
27
     * @param Context $ctx
28
     * @return Request
29
     */
30
    public function inbound($request, Context $ctx)
31
    {
32
        $tags = $ctx->get(self::FLAG) ?? $request->getTags();
33
        if ($tags !== Tags::DEFAULT) {
34
            $request->opsExtra('h-headers', static function (&$headers) use ($request, $tags) {
35
                $request->setTags(...$tags);
36
                $headers[Defined::X_ROUTE_TAGS] = implode(',', $tags);
37
            });
38
        }
39
40
        return $request;
41
    }
42
43
    public function outbound($response, Context $ctx)
44
    {
45
        return $response;
46
    }
47
48
    public function exception(Throwable $e, Context $ctx)
49
    {
50
        throw $e;
51
    }
52
}
53