Completed
Push — master ( f48699...0b66f1 )
by Michael
29s queued 22s
created

PhpMailerMessageProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 64
rs 10
c 2
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A getName() 0 3 1
A getEmailAddressByUser() 0 7 2
A sendMessage() 0 18 3
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of supporting
4
 developers from this source code or any supporting source code which is considered
5
 copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xmf\Assert;
13
use Xoops\Core\Kernel\Handlers\XoopsUser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsUser. 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...
14
use Xoops\Core\Service\AbstractContract;
15
use Xoops\Core\Service\Response;
16
use Xoops\Core\Service\Contract\UserEmailMessageInterface;
17
use Xoops\Core\Service\Data\Message;
18
use Xoops\Core\Service\Data\Email;
19
use Xoops\Core\Service\Data\EmailAddress;
20
21
/**
22
 * phpmailer module
23
 *
24
 * @copyright 2018 XOOPS Project (https://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @author    Richard Griffith <[email protected]>
27
 * @link      https://xoops.org
28
 */
29
class PhpMailerMessageProvider extends AbstractContract implements UserEmailMessageInterface
30
{
31
    /**
32
     * getName - get a short name for this service provider. This should be unique within the
33
     * scope of the named service, so using module dirname is suggested.
34
     *
35
     * @return string - a unique name for the service provider
36
     */
37
    public function getName()
38
    {
39
        return 'phpmailermessage';
40
    }
41
42
    /**
43
     * getDescription - get human readable description of the service provider
44
     *
45
     * @return string
46
     */
47
    public function getDescription()
48
    {
49
        return 'User messages by email using PHPMailer.';
50
    }
51
52
    /**
53
     * @param \Xoops\Core\Service\Response     $response
54
     * @param \Xoops\Core\Service\Data\Message $message
55
     *
56
     * @return void - reports success or failure through $response->success
57
     */
58
    public function sendMessage(Response $response, Message $message)
59
    {
60
        try {
61
            $email = new Email(
62
                $message->getSubject(),
63
                $message->getBody(),
64
                $this->getEmailAddressByUser($message->getFromId()),
65
                $this->getEmailAddressByUser($message->getToId())
66
            );
67
        } catch (\InvalidArgumentException | \LogicException $e) {
68
            $response->setSuccess(false)->addErrorMessage($e->getMessage());
69
            return;
70
        }
71
        // Relay to Email Service
72
        $emailResponse = \Xoops::getInstance()->service('email')->sendEmail($email);
73
        if (!$emailResponse->isSuccess()) {
74
            $response->setSuccess(false);
75
            $response->addErrorMessage($emailResponse->getErrorMessage());
76
        }
77
    }
78
79
    /**
80
     * @param int $userid
81
     *
82
     * @return \Xoops\Core\Service\Data\EmailAddress
83
     *
84
     * @throws \InvalidArgumentException -- bad userid
85
     */
86
    protected function getEmailAddressByUser(int $userid) : EmailAddress
87
    {
88
        $userHandler = \Xoops::getInstance()->getHandlerMember();
89
        $user = $userHandler->getUser($userid);
90
        Assert::isInstanceOf($user, XoopsUser::class);
91
        $name = empty($user->name()) ? $user->uname() : $user->name();
92
        return new EmailAddress($user->email(), $name);
93
    }
94
}
95