Test Failed
Push — master ( b03174...e1812b )
by Vladimir
03:18
created

TemplateCompiler::compile()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.4822

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 14
nop 2
dl 0
loc 39
ccs 11
cts 14
cp 0.7856
crap 7.4822
rs 6.7272
c 0
b 0
f 0
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\Contracts\Arrayable;
11
use FondBot\Templates\Keyboard\UrlButton;
12
use FondBot\Templates\Keyboard\ReplyButton;
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[] $templates
61
     * @param array               $args
62
     *
63
     * @return mixed
64
     */
65 2
    public function compile($templates, array $args = [])
66
    {
67 2
        if ($templates instanceof Template) {
68 2
            $templates = [$templates];
69
        }
70
71 2
        $result = [];
72
73 2
        foreach ($templates as $template) {
74
            // If template can compile itself there is no need to create additional method
75 2
            if ($template instanceof Arrayable) {
76
                // Firstly, we compile the template
77
                // Then we go through elements and compiled remaining templates
78
                $result[] = collect($template->toArray())
79
                    ->map(function ($element) use ($args) {
80
                        if ($element instanceof Template) {
81
                            return $this->compile($element, $args);
82
                        }
83
84 2
                        return $element;
85 2
                    })
86 1
                    ->toArray();
87
            } else {
88
                // Otherwise, we look for a compile method
89 1
                $method = 'compile'.ucfirst($template->getName());
90
                if (!method_exists($this, $method)) {
91
                    throw new RuntimeException('No compile method for "'.$template->getName().'".');
92
                }
93 1
94
                $result[] = $this->$method($template, $args);
95
            }
96
        }
97
98
        if (count($result) === 1) {
99
            return $result[0];
100
        }
101
102
        return $result;
103
    }
104
}
105