Passed
Push — master ( b52ead...b03174 )
by Vladimir
03:29
created

TemplateCompiler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
compileKeyboard() 0 1 ?
compilePayloadButton() 0 1 ?
compileReplyButton() 0 1 ?
compileUrlButton() 0 1 ?
B compile() 0 30 5
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
                        return $this->compile($element, $args);
81
                    });
82
            } else {
83
                // Otherwise, we look for a compile method
84 2
                $method = 'compile'.ucfirst($template->getName());
85 2
                if (!method_exists($this, $method)) {
86 1
                    throw new RuntimeException('No compile method for "'.$template->getName().'".');
87
                }
88
89 1
                $result[] = $this->$method($template, $args);
90
            }
91
        }
92
93 1
        return $result;
94
    }
95
}
96