Completed
Branch master (ae19d9)
by Pierre-Henry
36:56
created

Report::add()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author         Pierre-Henry Soria <[email protected]>
4
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
5
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @package        PH7 / App / System / Module / Report / Inc / Class
7
 */
8
9
namespace PH7;
10
11
use PH7\Framework\Mail\Mail;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Mail.

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...
12
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
13
use PH7\Framework\Date\CDateTime;
14
use PH7\Framework\Mvc\Model\DbConfig;
15
16
class Report
17
{
18
    /** @var PH7Tpl */
19
    private $_oView;
20
21
    /** @var string|bool */
22
    private $_mStatus = false;
23
24
    public function __construct()
25
    {
26
        $this->_oView = new PH7Tpl;
27
    }
28
29
    /**
30
     * Add the fields in the database.
31
     *
32
     * @param array $aData The data to  add
33
     *
34
     * @return Report
35
     */
36
    public function add(array $aData)
37
    {
38
        $oExistsModel = new ExistsCoreModel;
39
40
        if ($oExistsModel->id($aData['reporter_id']) && $oExistsModel->id($aData['spammer_id'])) {
41
            $this->_mStatus = (new ReportModel)->add($aData);
42
43
            if ($this->_mStatus === true) {
44
                if (DbConfig::getSetting('sendReportMail')) {
45
                    $this->sendMail($aData);
46
                }
47
            }
48
        }
49
50
        return $this;
51
    }
52
53
    /**
54
     * Get status
55
     *
56
     * @return string|boolean Text of the statute or boolean
57
     */
58
    public function get()
59
    {
60
        return $this->_mStatus;
61
    }
62
63
    /**
64
     * @param array $aData Report's details.
65
     *
66
     * @return integer Number of recipients who were accepted for delivery.
67
     */
68
    protected function sendMail(array $aData)
69
    {
70
        $oUser = new UserCore;
71
        $oUserModel = new UserCoreModel;
72
        $sReporterUsername = $oUserModel->getUsername($aData['reporter_id']);
73
        $sSpammerUsername = $oUserModel->getUsername($aData['spammer_id']);
74
        $sDate = (new CDateTime)->get($aData['date'])->dateTime();
75
76
        $this->_oView->content =
77
        t('Reporter:') . ' <b><a href="' . $oUser->getProfileLink($sReporterUsername) . '">' . $sReporterUsername . '</a></b><br /><br /> ' .
78
        t('Spammer:') . ' <b><a href="' . $oUser->getProfileLink($sSpammerUsername) . '">' . $sSpammerUsername . '</a></b><br /><br /> ' .
79
        t('Contant Type:') . ' <b>' . $aData['type'] . '</b><br /><br /> ' .
80
        t('URL:') . ' <b>' . $aData['url'] . '</b><br /><br /> ' .
81
        t('Description of report:') . ' <b>' . $aData['desc'] . '</b><br /><br /> '.
82
        t('Date:') . ' <b>' . $sDate . '</b><br /><br />';
83
84
        unset($oUser, $oUserModel);
85
86
        $sHtmlMessage = $this->_oView->parseMail(PH7_PATH_SYS . 'global/' . PH7_VIEWS . PH7_TPL_MAIL_NAME . '/tpl/mail/sys/mod/report/abuse.tpl', DbConfig::getSetting('adminEmail'));
87
88
        $aInfo = [
89
           'subject' => t('Spam report from %site_name%')
90
        ];
91
92
        return (new Mail)->send($aInfo, $sHtmlMessage);
93
    }
94
}
95