Passed
Pull Request — main (#29)
by
unknown
13:41 queued 08:13
created

WhatsAppTemplate::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
35
    /**
36
     * Template header, body and buttons can be personalized with custom variable values.
37
     *
38
     * @link https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates See how you can personalized your templates.
39
     */
40 10
    protected string $category = null;
41
42 10
    /**
43 10
     * The message type.
44 10
     */
45 10
    protected static string $type = 'template';
46 10
47 10
    protected function __construct($to = '', $name = '', $language = 'en_US')
48 10
    {
49 10
        $this->to = $to;
50
        $this->name = $name;
51
        $this->language = $language;
52 10
        $this->components = [
53
            'header' => [],
54 10
            'body' => [],
55
            'buttons' => [],
56
        ];
57 4
    }
58
59 4
    public static function create($to = '', $name = '', $language = 'en_US'): self
60
    {
61 4
        return new self($to, $name, $language);
62
    }
63
64 5
    public function to(string $to): self
65
    {
66 5
        $this->to = $to;
67
68 5
        return $this;
69
    }
70
71 1
    public function name(string $name): self
72
    {
73 1
        $this->name = $name;
74
75 1
        return $this;
76
    }
77
78 1
    public function language(string $language): self
79
    {
80 1
        $this->language = $language;
81
82 1
        return $this;
83
    }
84
85 1
    public function category(string $category): self
86
    {
87 1
        $this->category = $category;
88
89 1
        return $this;
90
    }
91
92 4
    public function header(Component $component): self
93
    {
94 4
        $this->components['header'][] = $component->toArray();
95
96
        return $this;
97 4
    }
98
99 4
    public function body(Component $component): self
100
    {
101
        $this->components['body'][] = $component->toArray();
102 4
103
        return $this;
104 4
    }
105
106
    public function recipient(): ?string
107 6
    {
108
        return $this->to;
109 6
    }
110 6
111 6
    public function configuredName(): ?string
112 6
    {
113 6
        return $this->name;
114
    }
115
116 1
    public function configuredLanguage(): string
117
    {
118 1
        return $this->language;
119 1
    }
120 1
121
    public function components(): CloudApiComponent
122 1
    {
123
        return new CloudApiComponent(
124
            $this->components['header'],
125 4
            $this->components['body'],
126
            $this->components['buttons']
127 4
        );
128
    }
129
130 3
    public function buttons(Button $component): self
131
    {
132 3
        $buttons = $this->components['buttons'];
133
        $component->setIndex(count($buttons));
134
        $this->components['buttons'][] = $component->toArray();
135
136
        return $this;
137
    }
138
139
    public function hasRecipient(): bool
140
    {
141
        return ! empty($this->to);
142
    }
143
144
    public function type(): string
145
    {
146
        return self::$type;
147
    }
148
}
149