Issues (3627)

app/bundles/CoreBundle/Model/AuditLogModel.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Model;
13
14
use Mautic\CoreBundle\Entity\AuditLog;
15
use Mautic\UserBundle\Entity\User;
16
17
/**
18
 * Class AuditLogModel.
19
 */
20
class AuditLogModel extends AbstractCommonModel
21
{
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * @return \Mautic\CoreBundle\Entity\AuditLogRepository
26
     */
27
    public function getRepository()
28
    {
29
        return $this->em->getRepository('MauticCoreBundle:AuditLog');
30
    }
31
32
    /**
33
     * Writes an entry to the audit log.
34
     *
35
     * @param array $args [bundle, object, objectId, action, details, ipAddress]
36
     */
37
    public function writeToLog(array $args)
38
    {
39
        $bundle    = (isset($args['bundle'])) ? $args['bundle'] : '';
40
        $object    = (isset($args['object'])) ? $args['object'] : '';
41
        $objectId  = (isset($args['objectId'])) ? $args['objectId'] : '';
42
        $action    = (isset($args['action'])) ? $args['action'] : '';
43
        $details   = (isset($args['details'])) ? $args['details'] : '';
44
        $ipAddress = (isset($args['ipAddress'])) ? $args['ipAddress'] : '';
45
46
        $log = new AuditLog();
47
        $log->setBundle($bundle);
48
        $log->setObject($object);
49
        $log->setObjectId($objectId);
0 ignored issues
show
It seems like $objectId can also be of type string; however, parameter $objectId of Mautic\CoreBundle\Entity\AuditLog::setObjectId() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $log->setObjectId(/** @scrutinizer ignore-type */ $objectId);
Loading history...
50
        $log->setAction($action);
51
        $log->setDetails($details);
0 ignored issues
show
It seems like $details can also be of type string; however, parameter $details of Mautic\CoreBundle\Entity\AuditLog::setDetails() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $log->setDetails(/** @scrutinizer ignore-type */ $details);
Loading history...
52
        $log->setIpAddress($ipAddress);
53
        $log->setDateAdded(new \DateTime());
54
55
        $user     = (!defined('MAUTIC_IGNORE_AUDITLOG_USER') && !defined('MAUTIC_AUDITLOG_USER')) ? $this->userHelper->getUser() : null;
56
        $userId   = 0;
57
        $userName = defined('MAUTIC_AUDITLOG_USER') ? MAUTIC_AUDITLOG_USER : $this->translator->trans('mautic.core.system');
58
        if ($user instanceof User && $user->getId()) {
59
            $userId   = $user->getId();
60
            $userName = $user->getName();
61
        }
62
        $log->setUserId($userId);
63
        $log->setUserName($userName);
64
65
        $this->em->getRepository('MauticCoreBundle:AuditLog')->saveEntity($log);
66
67
        $this->em->detach($log);
68
    }
69
70
    /**
71
     * Get the audit log for specific object.
72
     *
73
     * @param string                  $object
74
     * @param string|int              $id
75
     * @param \DateTimeInterface|null $afterDate
76
     * @param int                     $limit
77
     * @param string|null             $bundle
78
     *
79
     * @return mixed
80
     */
81
    public function getLogForObject($object, $id, $afterDate = null, $limit = 10, $bundle = null)
82
    {
83
        return $this->getRepository()->getLogForObject($object, $id, $limit, $afterDate, $bundle);
84
    }
85
}
86