Passed
Pull Request — master (#10)
by
unknown
09:54
created

CarouselTemplate::addRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the LineMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
13
namespace LineMob\Core\Template\Carousel;
14
15
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder;
16
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder;
17
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder;
18
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
19
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
20
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder;
21
use LineMob\Core\Template\AbstractTemplate;
22
use LineMob\Core\Template\Action;
23
24
/**
25
 * @author Ishmael Doss <[email protected]>
26
 */
27
class CarouselTemplate extends AbstractTemplate
28
{
29
    /**
30
     * @var Row[]
31
     */
32
    public $rows;
33
34
    /**
35
     * @var string
36
     */
37
    public $altText = 'This is carousel template.';
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getTemplate()
43
    {
44
        $rows = [];
45
46
        foreach ($this->rows as $row) {
47
            $actions = [];
48
49
            foreach ($row->actions as $action) {
50
                switch (strtolower($action->type)) {
51
                    case Action::TYPE_POSTBACK:
52
                        $actions[] = new PostbackTemplateActionBuilder($action->label, $action->value);
53
                        break;
54
                    case Action::TYPE_URI:
55
                        $actions[] =  new UriTemplateActionBuilder($action->label, $action->value);
56
                        break;
57
                    default:
58
                        $actions[] =  new MessageTemplateActionBuilder($action->label, $action->value);;
59
                }
60
            }
61
62
            $rows[] = new CarouselColumnTemplateBuilder(
63
                mb_substr($row->title, 0, 40), mb_substr($row->text, 0, 60),
64
                $row->thumbnail,
65
                $actions
66
            );
67
        }
68
69
        return new TemplateMessageBuilder($this->altText, new CarouselTemplateBuilder($rows));
70
    }
71
72
    /**
73
     * @param string $title
74
     * @param string $text
75
     * @param string $thumbnail
76
     * @param [{'label', 'value', 'type'}]|Action[] $actions
77
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'value', at position 0 could not be parsed: Unknown type name ''value'' at position 0 in 'value',.
Loading history...
78
    public function addRow($title, $text, $thumbnail, array $actions = [])
79
    {
80
        $this->rows[] = new Row($title, $text, $thumbnail, $actions);
81
    }
82
}
83