Completed
Push — main ( 81f3f2...e66a81 )
by Alejandro
15s queued 13s
created

WhatsAppTemplate::configuredLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
3
namespace NotificationChannels\WhatsApp;
4
5
use Netflie\WhatsAppCloudApi\Message\Template\Component as CloudApiComponent;
6
use NotificationChannels\WhatsApp\Component\Button;
7
use NotificationChannels\WhatsApp\Component\Component;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NotificationChannels\WhatsApp\Component. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class WhatsAppTemplate
10
{
11
    /**
12
     * WhatsApp ID or phone number for the person you want to send a message to.
13
     */
14
    protected string $to;
15
16
    /**
17
     * Name of the template.
18
     *
19
     * @link https://business.facebook.com/wa/manage/message-templates/ Dashboard to manage (create, edit and delete) templates.
20
     */
21
    protected string $name;
22
23
    /**
24
     * @link https://developers.facebook.com/docs/whatsapp/api/messages/message-templates#supported-languages See supported language codes.
25
     */
26
    protected string $language;
27
28
    /**
29
     * Template header, body and buttons can be personalized with custom variable values.
30
     *
31
     * @link https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates See how you can personalized your templates.
32
     */
33
    protected array $components;
34 9
35
    protected function __construct($to = '', $name = '', $language = 'en_US')
36 9
    {
37 9
        $this->to = $to;
38 9
        $this->name = $name;
39 9
        $this->language = $language;
40 9
        $this->components = [
41 9
            'header' => [],
42 9
            'body' => [],
43
            'buttons' => [],
44
        ];
45 9
    }
46
47 9
    public static function create($to = '', $name = '', $language = 'en_US'): self
48
    {
49
        return new self($to, $name, $language);
50 4
    }
51
52 4
    public function to(string $to): self
53
    {
54 4
        $this->to = $to;
55
56
        return $this;
57 5
    }
58
59 5
    public function name(string $name): self
60
    {
61 5
        $this->name = $name;
62
63
        return $this;
64 1
    }
65
66 1
    public function language(string $language): self
67
    {
68 1
        $this->language = $language;
69
70
        return $this;
71 1
    }
72
73 1
    public function header(Component $component): self
74
    {
75 1
        $this->components['header'][] = $component->toArray();
76
77
        return $this;
78 1
    }
79
80 1
    public function body(Component $component): self
81
    {
82 1
        $this->components['body'][] = $component->toArray();
83
84
        return $this;
85 4
    }
86
87 4
    public function recipient(): ?string
88
    {
89
        return $this->to;
90 4
    }
91
92 4
    public function configuredName(): ?string
93
    {
94
        return $this->name;
95 4
    }
96
97 4
    public function configuredLanguage(): string
98
    {
99
        return $this->language;
100 5
    }
101
102 5
    public function components(): CloudApiComponent
103 5
    {
104 5
        return new CloudApiComponent(
105 5
            $this->components['header'],
106
            $this->components['body'],
107
            $this->components['buttons']
108 4
        );
109
    }
110 4
111
    public function buttons(Button $component): self
112
    {
113
        $buttons = $this->components['buttons'];
114
        $component->setIndex(count($buttons));
115
        $this->components['buttons'][] = $component->toArray();
116
117
        return $this;
118
    }
119
120
    public function hasRecipient(): bool
121
    {
122
        return ! empty($this->to);
123
    }
124
}
125