CallableParser::parse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
/**
3
 * Copyright Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 **/
17
18
namespace ArekX\DataStreamer\Data;
19
20
use ArekX\DataStreamer\Contracts\Message;
21
use ArekX\DataStreamer\Contracts\MessageParser;
22
use Exception;
23
24
/**
25
 * Class CallableParser
26
 * @package ArekX\DataStreamer\Data
27
 *
28
 * Represents a callable handler which
29
 * resolves message data by type to a specific
30
 * callable function which will return an instance of
31
 * Message
32
 *
33
 * @see Message
34
 */
35
class CallableParser implements MessageParser
36
{
37
    /**
38
     * Registered builders by type.
39
     * Type is the key and callable is the value.
40
     *
41
     * @var callable[]
42
     */
43
    protected $builders = [];
44
45
    /**
46
     * Represents a default callable builder
47
     * when no type is registered.
48
     *
49
     * If this is not set and no type is found
50
     * this class will throw an exception.
51
     *
52
     * @see CallableParser::parse()
53
     * @var callable
54
     */
55
    protected $defaultBuilder = null;
56
57
    /**
58
     *
59
     * Sets a callable which should return an instance of
60
     * Message.
61
     *
62
     * Callable should be in the format:
63
     * ```php
64
     * function(string $id, string $type, array $payload) {
65
     *    return // Should return an instance of Message.
66
     * }
67
     * ```
68
     *
69
     * @param string $type
70
     * @param callable $builder
71
     */
72 2
    public function setBuilder(string $type, callable $builder)
73
    {
74 2
        $this->builders[$type] = $builder;
75 2
    }
76
77
    /**
78
     *
79
     * Sets a callable which will run if parser cannot
80
     * resolve to a specific type.
81
     *
82
     * Callable should be in the format:
83
     * ```php
84
     * function(string $id, string $type, array $payload) {
85
     *    return // Should return an instance of Message.
86
     * }
87
     * ```
88
     *
89
     * @param callable $builder
90
     */
91 5
    public function setDefaultBuilder(callable $builder)
92
    {
93 5
        $this->defaultBuilder = $builder;
94 5
    }
95
96
    /**
97
     * Resolves a message to a specific builder based on the
98
     * message type.
99
     *
100
     * @param string $id ID of the message
101
     * @param array $message Data of the message
102
     * @return Message Resolved instance of Message
103
     * @throws Exception Exception which will be thrown if there is no default builder set and type is not resolved.
104
     */
105 6
    public function parse(string $id, array $message): Message
106
    {
107 6
        $buildMessage = $this->defaultBuilder;
108 6
        $type = $message['type'];
109
110 6
        if (!empty($this->builders[$type])) {
111 1
            $buildMessage = $this->builders[$type];
112
        }
113
114 6
        if (!is_callable($buildMessage)) {
115 1
            throw new Exception('Could not build message ID:' . $id);
116
        }
117
118 5
        $payload = @json_decode($message['payload'], true) ?: [];
119 5
        return $buildMessage($id, $type, $payload);
120
    }
121
}