Completed
Push — master ( b87c3d...fa1d47 )
by Vladimir
08:07
created

TemplateCompiler::compile()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
cc 6
eloc 16
nc 3
nop 2
ccs 12
cts 12
cp 1
crap 6
rs 8.439
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Drivers;
6
7
use RuntimeException;
8
use FondBot\Contracts\Template;
9
use FondBot\Templates\Keyboard;
10
use FondBot\Templates\Keyboard\UrlButton;
11
use FondBot\Templates\Keyboard\ReplyButton;
12
use Illuminate\Contracts\Support\Arrayable;
13
use FondBot\Templates\Keyboard\PayloadButton;
14
15
abstract class TemplateCompiler
16
{
17
    /**
18
     * Compile keyboard.
19
     *
20
     * @param Keyboard $keyboard
21
     * @param array    $args
22
     *
23
     * @return mixed
24
     */
25
    abstract protected function compileKeyboard(Keyboard $keyboard, array $args);
26
27
    /**
28
     * Compile payload button.
29
     *
30
     * @param PayloadButton $button
31
     * @param array         $args
32
     *
33
     * @return mixed
34
     */
35
    abstract protected function compilePayloadButton(PayloadButton $button, array $args);
36
37
    /**
38
     * Compile reply button.
39
     *
40
     * @param ReplyButton $button
41
     * @param array       $args
42
     *
43
     * @return mixed
44
     */
45
    abstract protected function compileReplyButton(ReplyButton $button, array $args);
46
47
    /**
48
     * Compile url button.
49
     *
50
     * @param UrlButton $button
51
     * @param array     $args
52
     *
53
     * @return mixed
54
     */
55
    abstract protected function compileUrlButton(UrlButton $button, array $args);
56
57
    /**
58
     * Compile template.
59
     *
60
     * @param Template $template
61
     * @param array    $args
62
     *
63
     * @return mixed
64
     */
65 3
    public function compile(Template $template, array $args = [])
66
    {
67
        // If template can compile itself we recursively compile subelements
68 3
        if ($template instanceof Arrayable) {
69 1
            $array = $template->toArray();
70 1
            $transformer = function ($value) use (&$transformer, $args) {
71 1
                if (is_array($value)) {
72 1
                    return array_map($transformer, $value);
73
                }
74
75 1
                if ($value instanceof Arrayable) {
76 1
                    return array_map($transformer, $value->toArray());
77
                }
78
79 1
                if ($value instanceof Template) {
80 1
                    return $this->compile($value, $args);
81
                }
82
83 1
                return $value;
84 1
            };
85
86
            return array_map($transformer, $array);
87
        }
88
89
        // Otherwise, we look for a compile method
90
        $method = 'compile'.ucfirst($template->getName());
91
        if (!method_exists($this, $method)) {
92
            throw new RuntimeException('"'.$template->getName().'" cannot be compiled.');
93
        }
94
95
        return $this->$method($template, $args);
96
    }
97
}
98