ContextUnpacking::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
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Server unpacking of attachments
4
 * User: moyo
5
 * Date: 19/03/2018
6
 * Time: 9:53 PM
7
 */
8
9
namespace Carno\HRPC\Handlers;
10
11
use Carno\Chain\Layered;
12
use Carno\Coroutine\Context;
13
use Carno\HRPC\Client\Handlers\ContextPacking;
14
use Carno\HTTP\Standard\Request;
15
use Throwable;
16
17
class ContextUnpacking implements Layered
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $keys = [];
23
24
    /**
25
     * @param string ...$allowed
26
     */
27
    public function keys(string ...$allowed) : void
28
    {
29
        $this->keys = $allowed;
30
    }
31
32
    public function inbound($request, Context $ctx)
33
    {
34
        /**
35
         * @var Request $http
36
         */
37
        $http = $ctx->get(ServerWrapper::REQUESTING);
38
39
        if ($dat = $http->getHeaderLine(ContextPacking::HTTP_HEADER)) {
40
            if ($attachments = json_decode(base64_decode($dat), true)) {
41
                foreach ($attachments as $key => $val) {
42
                    in_array($key, $this->keys) && $ctx->set($key, $val);
43
                }
44
            }
45
        }
46
47
        return $request;
48
    }
49
50
    public function outbound($response, Context $ctx)
51
    {
52
        return $response;
53
    }
54
55
    public function exception(Throwable $e, Context $ctx)
56
    {
57
        throw $e;
58
    }
59
}
60