Passed
Push — main ( 577f5e...2e91a7 )
by Dimitri
02:59
created

AbstractAdapter::makeAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace BlitzPHP\Mail\Adapters;
4
5
use BadMethodCallException;
6
use BlitzPHP\Mail\MailerInterface;
7
use BlitzPHP\Utilities\String\Text;
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
abstract class AbstractAdapter implements MailerInterface
12
{  
13
    /**
14
     * Dependances necessaires a l'adapter
15
     * 
16
     * @var array<string, string>[]
17
     */
18
    protected array $dependancies = [];
19
20
    protected $mailer;
21
22
    protected const PRIORITY_MAP = [
23
        self::PRIORITY_HIGH, 
24
        self::PRIORITY_NORMAL, 
25
        self::PRIORITY_LOW
26
    ];
27
28
    public function __construct(bool $debug = false)
29
    {
30
        foreach ($this->dependancies as $dependency) {
31
            if (empty($dependency['class']) || empty($dependency['package'])) {
32
                throw new InvalidArgumentException('Invalid dependencies property');
33
            }            
34
            if (! is_string($dependency['class']) || ! is_string($dependency['package'])) {
35
                throw new InvalidArgumentException('Invalid dependencies property');
36
            }
37
38
            if (! class_exists($dependency['class'])) {
39
                throw new RuntimeException(lang('Mail.dependancyNotFound', [$dependency['class'], static::class, $dependency['package']]));
40
            }
41
        }
42
        
43
        if ($debug) {
44
            $this->setDebug();
45
        }
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function init(array $config): self
52
    {
53
        foreach ($config as $key => $value) {
54
            $method = static::methodName($key);
55
            if (method_exists($this, $method)) {
56
                call_user_func([$this, $method], $value);
57
            }
58
        }
59
60
        return $this;
61
    }
62
63
    public abstract function setPort(int $port): self;
64
65
    public abstract function setHost(string $host): self;
66
    
67
    public abstract function setUsername(string $username): self;
68
69
    public abstract function setPassword(string $password): self;
70
71
    public abstract function setDebug(int $debug = 1): self;
72
73
    public abstract function setProtocol(string $protocol): self;
74
75
    public abstract function setTimeout(int $timeout): self;
76
    
77
    public abstract function setCharset(string $charset): self;
78
79
    public abstract function setPriority(int $priority): self;
80
81
    public abstract function setEncryption(?string $encryption): self;
82
83
    public function __call(string $method, array $arguments)
84
    {
85
        $name = static::methodName($method, 'set');
86
        if (method_exists($this, $name)) {
87
            return call_user_func_array([$this, $name], $arguments);
88
        }
89
90
        $name = static::methodName($method, 'get');
91
        if (method_exists($this, $name)) {
92
            return call_user_func_array([$this, $name], $arguments);
93
        }
94
95
        if ($this->mailer && method_exists($this->mailer, $method)) {
96
            return call_user_func_array([$this->mailer, $method], $arguments);
97
        }
98
99
        throw new BadMethodCallException('Method ' . $method . ' does not exist in ' . static::class);
100
    }
101
102
    protected static function methodName(string $name, string $prefix = 'set'): string
103
    {
104
        return Text::camel($prefix . '_' . $name);
105
    }
106
107
    /**
108
     * Cree une adresse au format valide pour l'adapter
109
     * 
110
     * @return array
111
     */
112
    protected function makeAddress(string $email, string $name)
113
    {
114
        if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
115
            $tmp = $email;
116
            $email = $name;
117
            $name = $tmp;
118
        }
119
120
        return [$email, $name];
121
    }
122
123
    protected function parseMultipleAddresses(array|string $address, bool|string $name = '', bool $set = false): array
124
    {
125
        if (is_string($address)) {
0 ignored issues
show
introduced by
The condition is_string($address) is always false.
Loading history...
126
            if (is_bool($name)) {
127
                throw new InvalidArgumentException('L\'argument 2 ($name) doit etre une chaine de caractères');
128
            }
129
130
            $address = [$address => $name];
131
        } else if (is_bool($name)) {
132
            $set = $name;
133
        }
134
        
135
        $addresses = [];
136
        
137
        foreach ($address As $key => $value) {
138
            $addresses[] = $this->makeAddress($key, $value);
139
        }
140
141
        return [$addresses, $set];
142
    }
143
}
144