Completed
Pull Request — master (#575)
by Richard
06:57
created

PMProvider::sendMessage()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 20
rs 9.4285
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered 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\Module\Helper;
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\UserMessageInterface;
17
use Xoops\Core\Service\Data\Message;
18
19
/**
20
 * PM provider for service manager
21
 *
22
 * @category  class
23
 * @package   PMProvider
24
 * @author    Richard Griffith <[email protected]>
25
 * @copyright 2018 XOOPS Project (https://xoops.org)
26
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27
 * @link      https://xoops.org
28
 * @since     2.6.0
29
 */
30
class PMProvider extends AbstractContract implements UserMessageInterface
31
{
32
    /**
33
     * getName - get a short name for this service provider. This should be unique within the
34
     * scope of the named service, so using module dirname is suggested.
35
     *
36
     * @return string - a unique name for the service provider
37
     */
38
    public function getName()
39
    {
40
        return 'pm';
41
    }
42
43
    /**
44
     * getDescription - get human readable description of the service provider
45
     *
46
     * @return string
47
     */
48
    public function getDescription()
49
    {
50
        return 'Use PM for user messsages.';
51
    }
52
53
    /**
54
     * @param \Xoops\Core\Service\Response     $response
55
     * @param \Xoops\Core\Service\Data\Message $message
56
     *
57
     * @return void - reports success or failure through $response->success
58
     */
59
    public function sendMessage(Response $response, Message $message)
60
    {
61
        $pmHandler = Helper::getHelper('pm')->getHandler('message');
62
        /** @var \PmMessage */
63
        $pm = $pmHandler->create();
64
        $pm->setVar('msg_time', time());
65
        try {
66
            $pm->setVar('subject', $message->getSubject());
67
            $pm->setVar('msg_text', $message->getBody());
68
            $pm->setVar('to_userid', $message->getToId());
69
            $pm->setVar('from_userid', $message->getFromId());
70
        } catch (\LogicException $e) {
71
            $response->setSuccess(false)->addErrorMessage($e->getMessage());
72
            return;
73
        }
74
        //PMs are by default not saved in outbox
75
        //$pm->setVar('from_delete', 0);
76
77
        if (false === $pmHandler->insert($pm)) {
78
            $response->setSuccess(false)->addErrorMessage($pm->getErrors());
79
        }
80
    }
81
}
82