Completed
Push — master ( 77e9cc...a0b11e )
by Vladimir
08:12
created

TemplateCompiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 83
loc 83
ccs 0
cts 12
cp 0
c 0
b 0
f 0
rs 10
wmc 6
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
compileKeyboard() 1 1 ?
compilePayloadButton() 1 1 ?
compileReplyButton() 1 1 ?
compileUrlButton() 1 1 ?
B compile() 32 32 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Contracts\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 View Code Duplication
abstract class TemplateCompiler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    public function compile(Template $template, array $args = [])
66
    {
67
        // If template can compile itself we recursively compile subelements
68
        if ($template instanceof Arrayable) {
69
            $array = $template->toArray();
70
            $transformer = function ($value) use (&$transformer, $args) {
71
                if (is_array($value)) {
72
                    return array_map($transformer, $value);
73
                }
74
75
                if ($value instanceof Arrayable) {
76
                    return array_map($transformer, $value->toArray());
77
                }
78
79
                if ($value instanceof Template) {
80
                    return $this->compile($value, $args);
81
                }
82
83
                return $value;
84
            };
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().' can not be compiled.');
93
        }
94
95
        return $this->$method($template, $args);
96
    }
97
}
98