• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sdkconsultoria / whatsapp-cloud-api / #65

27 Mar 2024 10:39PM UTC coverage: 99.3% (-0.4%) from 99.717%
#65

push

hellsythe
Fix send template bug

709 of 714 relevant lines covered (99.3%)

6.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
src/Models/Template.php
1
<?php
2

3
namespace Sdkconsultoria\WhatsappCloudApi\Models;
4

5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7

8
class Template extends Model
9
{
10
    use HasFactory;
11

12
    public const STATUS_APPROVED = 'APPROVED';
13

14
    public const STATUS_PENDING = 'PENDING';
15

16
    public const STATUS_REJECTED = 'REJECTED';
17

18
    public array $componentsWithVars = [];
19

20
    public array $vars = [];
21

22
    public function getComponents()
23
    {
24
        return json_decode($this->content, true)['components'];
9✔
25
    }
26

27
    public function setVars(array $vars): void
28
    {
29
        $components = $this->getComponents();
6✔
30
        $fixedVars = [];
6✔
31
        foreach ($vars as $key => $var) {
6✔
32
            $key = strtoupper($key);
2✔
33
            $fixedVars[] = array_merge($var, ['type' => $key]);
2✔
34
            $components[$key] = $components[$key] ?? [];
2✔
35

36
            switch ($key) {
37
                case 'BODY':
2✔
38
                    $components[$key] = $this->replaceVars($components[$key], $var);
1✔
39
                    break;
1✔
40
                case 'BUTTON':
1✔
41
                    $components['BUTTON'] = $components['BUTTON'] ?? [];
×
42
                    $components['BUTTON'][] = $var;
×
43
                    break;
×
44

45
                default:
46
                    $components[$key]['parameters'] = $var;
1✔
47
                    break;
1✔
48
            }
49
        }
50

51
        $this->componentsWithVars = $components;
6✔
52
        $this->vars = $fixedVars;
6✔
53
    }
54

55
    private function replaceVars(array $component, array $var): array
56
    {
57
        $text = $component['text'];
1✔
58
        foreach ($var['parameters'] as $index => $value) {
1✔
59
            $text = str_replace('{{'.($index + 1).'}}', $value['text'], $text);
1✔
60
        }
61
        $component['text'] = $text;
1✔
62

63
        return $component;
1✔
64
    }
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc