Passed
Push — main ( e66a81...665c4d )
by Alejandro
02:51 queued 48s
created

WhatsAppTemplate::hasRecipient()   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 0
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 10
    protected function __construct($to = '', $name = '', $language = 'en_US')
36
    {
37 10
        $this->to = $to;
38 10
        $this->name = $name;
39 10
        $this->language = $language;
40 10
        $this->components = [
41 10
            'header' => [],
42 10
            'body' => [],
43 10
            'buttons' => [],
44 10
        ];
45
    }
46
47 10
    public static function create($to = '', $name = '', $language = 'en_US'): self
48
    {
49 10
        return new self($to, $name, $language);
50
    }
51
52 4
    public function to(string $to): self
53
    {
54 4
        $this->to = $to;
55
56 4
        return $this;
57
    }
58
59 5
    public function name(string $name): self
60
    {
61 5
        $this->name = $name;
62
63 5
        return $this;
64
    }
65
66 1
    public function language(string $language): self
67
    {
68 1
        $this->language = $language;
69
70 1
        return $this;
71
    }
72
73 1
    public function header(Component $component): self
74
    {
75 1
        $this->components['header'][] = $component->toArray();
76
77 1
        return $this;
78
    }
79
80 1
    public function body(Component $component): self
81
    {
82 1
        $this->components['body'][] = $component->toArray();
83
84 1
        return $this;
85
    }
86
87 4
    public function recipient(): ?string
88
    {
89 4
        return $this->to;
90
    }
91
92 4
    public function configuredName(): ?string
93
    {
94 4
        return $this->name;
95
    }
96
97 4
    public function configuredLanguage(): string
98
    {
99 4
        return $this->language;
100
    }
101
102 6
    public function components(): CloudApiComponent
103
    {
104 6
        return new CloudApiComponent(
105 6
            $this->components['header'],
106 6
            $this->components['body'],
107 6
            $this->components['buttons']
108 6
        );
109
    }
110
111 1
    public function buttons(Button $component): self
112
    {
113 1
        $buttons = $this->components['buttons'];
114 1
        $component->setIndex(count($buttons));
115 1
        $this->components['buttons'][] = $component->toArray();
116
117 1
        return $this;
118
    }
119
120 4
    public function hasRecipient(): bool
121
    {
122 4
        return ! empty($this->to);
123
    }
124
}
125