Passed
Push — main ( 7ca792...577f5e )
by Dimitri
03:34
created

AbstractAdapter::parseMultipleAddresses()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 7
nop 3
dl 0
loc 19
rs 9.6111
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
    /**
84
     * Cree une adresse au format valide pour l'adapter
85
     */
86
    protected abstract function makeAddress(string $email, string $name);
87
88
    public function __call(string $method, array $arguments)
89
    {
90
        $name = static::methodName($method, 'set');
91
        if (method_exists($this, $name)) {
92
            return call_user_func_array([$this, $name], $arguments);
93
        }
94
95
        $name = static::methodName($method, 'get');
96
        if (method_exists($this, $name)) {
97
            return call_user_func_array([$this, $name], $arguments);
98
        }
99
100
        if ($this->mailer && method_exists($this->mailer, $method)) {
101
            return call_user_func_array([$this->mailer, $method], $arguments);
102
        }
103
104
        throw new BadMethodCallException('Method ' . $method . ' does not exist in ' . static::class);
105
    }
106
107
    protected static function methodName(string $name, string $prefix = 'set'): string
108
    {
109
        return Text::camel($prefix . '_' . $name);
110
    }
111
112
    protected function parseMultipleAddresses(array|string $address, bool|string $name = '', bool $set = false): array
113
    {
114
        if (is_string($address)) {
0 ignored issues
show
introduced by
The condition is_string($address) is always false.
Loading history...
115
            if (is_bool($name)) {
116
                throw new InvalidArgumentException('L\'argument 2 ($name) doit etre une chaine de caractères');
117
            }
118
119
            $address = [$address => $name];
120
        } else if (is_bool($name)) {
121
            $set = $name;
122
        }
123
        
124
        $addresses = [];
125
        
126
        foreach ($address As $key => $value) {
127
            $addresses[] = $this->makeAddress($key, $value);
128
        }
129
130
        return [$addresses, $set];
131
    }
132
}
133