Completed
Push — develop ( dd9c1c...e37f14 )
by greg
07:53
created

Message::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace PlaygroundCore\Mail\Service;
3
4
use Zend\ServiceManager\ServiceManager;
5
use Zend\Mail\Message as MailMessage;
6
use Zend\Mime\Message as MimeMessage;
7
use Zend\Mime\Mime;
8
use Zend\Mime\Part;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
class Message
12
{
13
    /**
14
     *
15
     * @var ServiceManager
16
     */
17
    protected $serviceLocator;
18
19
    public function __construct(ServiceLocatorInterface $locator)
20
    {
21
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
22
    }
23
24
    /**
25
     *
26
     * @var \Zend\View\Renderer\RendererInterface
27
     */
28
    protected $renderer;
29
30
    /**
31
     *
32
     * @var \Zend\Mail\Transport\TransportInterface
33
     */
34
    protected $transport;
35
36
    /**
37
     * Return a HTML message ready to be sent
38
     *
39
     * @param array|string $from
40
     *            A string containing the sender e-mail address, or if array with keys email and name
41
     * @param array|string $to
42
     *            An array containing the recipients of the mail
43
     * @param string $subject
44
     *            Subject of the mail
45
     * @param string|\Zend\View\Model\ModelInterface $nameOrModel
46
     *            Either the template to use, or a ViewModel
47
     * @param null|array $values
48
     *            Values to use when the template is rendered
49
     * @return Message
50
     */
51
    public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = array())
52
    {
53
        if (is_string($from)) {
54
            $from = array('email' => $from, 'name' => $from);
55
        }
56
        $renderer = $this->getRenderer();
57
        $content = $renderer->render($nameOrModel, $values);
58
        $resolver = $this->serviceLocator->get('Zend\View\Resolver\TemplatePathStack');
59
        // check if plain text email template exist
60
        if ($resolver->resolve($nameOrModel . '-plain')) {
61
            $contentText = $renderer->render($nameOrModel . '-plain', $values);
62
        } else {
63
            $contentText = '';
64
        }
65
66
        $mail = new MailMessage();
67
        $mail->addTo($to);
68
        $mail->addFrom($from['email'], $from['name']);
69
        $mail->setSubject($subject);
70
        $text              = new Part($contentText);
71
        $text->type        = Mime::TYPE_TEXT;
72
        $text->encoding    = Mime::ENCODING_QUOTEDPRINTABLE;
73
        $text->disposition = Mime::DISPOSITION_INLINE;
74
        $text->charset     = 'UTF-8';
75
        $html              = new Part($content);
76
        $html->type        = Mime::TYPE_HTML;
77
        $html->encoding    = Mime::ENCODING_QUOTEDPRINTABLE;
78
        $html->disposition = Mime::DISPOSITION_INLINE;
79
        $html->charset     = 'UTF-8';
80
        $bodyMessage     = new MimeMessage();
81
82
        $multiPartContentMessage = new MimeMessage();
83
        $multiPartContentMessage->addPart($text);
84
        $multiPartContentMessage->addPart($html);
85
        $multiPartContentMimePart           = new Part($multiPartContentMessage->generateMessage());
86
        $multiPartContentMimePart->charset  = 'UTF-8';
87
        $multiPartContentMimePart->type     = 'multipart/alternative';
88
        $multiPartContentMimePart->boundary = $multiPartContentMessage->getMime()->boundary();
89
        $bodyMessage->addPart($multiPartContentMimePart);
90
        $mail->setBody($bodyMessage);
91
        $mail->setEncoding('UTF-8');
92
93
        return $mail;
94
    }
95
96
    /**
97
     * Return a text message ready to be sent
98
     *
99
     * @param array|string $from
100
     *            A string containing the sender e-mail address, or if array with keys email and name
101
     * @param array|string $to
102
     *            An array containing the recipients of the mail
103
     * @param string $subject
104
     *            Subject of the mail
105
     * @param string|\Zend\View\Model\ModelInterface $nameOrModel
106
     *            Either the template to use, or a ViewModel
107
     * @param null|array $values
108
     *            Values to use when the template is rendered
109
     * @return Message
110
     */
111
    public function createTextMessage($from, $to, $subject, $nameOrModel, $values = array())
112
    {
113
        $renderer = $this->getRenderer();
114
        $content = $renderer->render($nameOrModel, $values);
115
116
        return $this->getDefaultMessage($from, 'utf-8', $to, $subject, $content);
117
    }
118
119
    /**
120
     * Send the message
121
     *
122
     * @param Message $message
123
     */
124
    public function send(MailMessage $message)
125
    {
126
        $this->getTransport()
127
            ->send($message);
128
    }
129
130
    /**
131
     * Get the renderer
132
     *
133
     * @return \Zend\View\Renderer\RendererInterface
134
     */
135
    protected function getRenderer()
136
    {
137
        if ($this->renderer === null) {
138
            $this->renderer = $this->serviceLocator->get('ViewRenderer');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->serviceLocator->get('ViewRenderer') can also be of type array. However, the property $renderer is declared as type object<Zend\View\Renderer\RendererInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
139
        }
140
141
        return $this->renderer;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->renderer; of type object|array adds the type array to the return on line 141 which is incompatible with the return type documented by PlaygroundCore\Mail\Service\Message::getRenderer of type Zend\View\Renderer\RendererInterface.
Loading history...
142
    }
143
144
    /**
145
     * Get the transport
146
     *
147
     * @return \Zend\Mail\Transport\TransportInterface
148
     */
149
    protected function getTransport()
150
    {
151
        if ($this->transport === null) {
152
            $this->transport = $this->serviceLocator->get('playgroundcore_transport');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->serviceLocator->g...ygroundcore_transport') can also be of type array. However, the property $transport is declared as type object<Zend\Mail\Transport\TransportInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
153
        }
154
155
        return $this->transport;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->transport; of type object|array adds the type array to the return on line 155 which is incompatible with the return type documented by PlaygroundCore\Mail\Service\Message::getTransport of type Zend\Mail\Transport\TransportInterface.
Loading history...
156
    }
157
158
    /**
159
     *
160
     * @return Message
161
     */
162
    protected function getDefaultMessage($from, $encoding, $to, $subject, $body)
163
    {
164
        if (is_string($from)) {
165
            $from = array('email' => $from, 'name' => $from);
166
        }
167
168
        $message = new MailMessage();
169
        $message->setFrom($from['email'], $from['name'])
170
            ->setEncoding($encoding)
171
            ->setSubject($subject)
172
            ->setBody($body)
173
            ->setTo($to);
174
175
        $message->getHeaders()->get('content-type')->setType('multipart/alternative');
176
177
        return $message;
178
    }
179
}
180