Ajde_Mailer::addAddress()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once 'Mailer'.DS.'class.phpmailer.php';
4
require_once 'Mailer'.DS.'class.smtp.php';
5
require_once 'Mailer'.DS.'class.pop3.php';
6
7
class Ajde_Mailer extends PHPMailer
8
{
9
    public function __construct($exceptions = false)
10
    {
11
        parent::__construct($exceptions);
12
        if (config('mail.mailer') == 'smtp') {
13
            $this->isSMTP();
14
            $configs = config('mail.config');
15
            foreach ($configs as $k => $v) {
16
                $this->$k = $v;
17
            }
18
        } else {
19
            $this->isMail();
20
        }
21
    }
22
23
    public function sendUsingModel($identifier, $toEmail, $toName = '', $data = [])
24
    {
25
        $email = new EmailModel();
26
        if ($email->loadByField('identifier', $identifier)) {
27
            $template = $email->getTemplate();
28
29
            $fromName = $email->getFromName();
30
            $fromEmail = $email->getFromEmail();
31
            $subject = $this->replaceData($template->getSubject(), $data);
32
33
            $markup = $this->rel2abs($this->replaceData($template->getMarkup(), $data));
0 ignored issues
show
Documentation Bug introduced by
The method getMarkup does not exist on object<TemplateModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34
            $body = PHP_EOL.$this->rel2abs($this->replaceData($template->getContent($markup), $data));
35
36
            // reset recipients
37
            $this->clearAllRecipients();
38
39
            // to
40
            $this->addAddress($toEmail, $toName);
41
42
            // from
43
            $this->From = $fromEmail;
44
45
            // fromName
46
            $this->FromName = $fromName;
47
48
            // subject
49
            $this->Subject = $subject;
50
51
            // utf8 please
52
            $this->CharSet = 'utf-8';
53
54
            // body
55
            $this->msgHTML($body);
56
57
            // send!
58
            $status = $this->send();
59
60
            // log
61 View Code Duplication
            if (class_exists('MailerlogModel')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                MailerlogModel::log($fromEmail, $fromName, $toEmail, $toName, $subject, $body, $status ? 1 : 0);
63
            }
64
65
            return $status;
66
        } else {
67
            throw new Ajde_Exception('Email with identifier '.$identifier.' not found');
68
        }
69
    }
70
71
    private function rel2abs($text)
72
    {
73
        $base = config('app.rootUrl');
74
        $replace = '$1'.$base.'$2$3';
75
76
        // Look for images
77
        $pattern = "#(<\s*?img\s*?[^>]*src\s*?=[\"'])(?!http)([^\"'>]+)([\"'>]+)#";
78
        $text = preg_replace($pattern, $replace, $text);
79
80
        // Look for links
81
        $pattern = "#(<\s*?a\s*?[^>]*href\s*?=[\"'])(?!http)([^\"'>]+)([\"'>]+)#";
82
        $text = preg_replace($pattern, $replace, $text);
83
84
        return $text;
85
    }
86
87
    private function mergeData($data)
88
    {
89
        $defaultData = [
90
            'sitename' => config('app.title'),
91
        ];
92
93
        return array_merge($defaultData, $data);
94
    }
95
96
    private function replaceData($string, $data)
97
    {
98
        $data = $this->mergeData($data);
99
100
        foreach ($data as $key => $value) {
101
            $string = str_replace('%'.$key.'%', $value, $string);
102
        }
103
104
        return $string;
105
    }
106
107
    public function SendQuickMail($to, $from, $fromName, $subject, $body, $toName = '')
108
    {
109
        // set class to use PHP mail function
110
        // $this->IsMail();
111
112
        // reset recipients
113
        $this->clearAllRecipients();
114
115
        // to
116
        $this->addAddress($to, $toName);
117
118
        // from
119
        $this->From = $from;
120
121
        // fromName
122
        $this->FromName = $fromName;
123
124
        // subject
125
        $this->Subject = $subject;
126
127
        // body
128
        $this->Body = $body;
129
130
        // alt body
131
        $this->AltBody = strip_tags($body);
132
133
        // set html content type
134
        $this->isHTML(true);
135
136
        // send!
137
        $status = $this->send();
138
139
        // log
140 View Code Duplication
        if (class_exists('MailerlogModel')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
            MailerlogModel::log($from, $fromName, $to, $toName, $subject, $body, $status ? 1 : 0);
142
        }
143
144
        return $status;
145
    }
146
147
    public function addAddress($address, $name = '')
148
    {
149
        if (config('mail.debug') === true) {
150
            $address = config('app.email');
151
        }
152
153
        return parent::addAnAddress('to', $address, $name);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (addAnAddress() instead of addAddress()). Are you sure this is correct? If so, you might want to change this to $this->addAnAddress().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
154
    }
155
}
156