Issues (3627)

LeadBundle/Entity/PointsChangeLogRepository.php (1 issue)

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\LeadBundle\Entity;
13
14
use Doctrine\DBAL\Query\QueryBuilder;
15
use Mautic\CoreBundle\Entity\CommonRepository;
16
17
class PointsChangeLogRepository extends CommonRepository
18
{
19
    use TimelineTrait;
20
21
    /**
22
     * Get a lead's point log.
23
     *
24
     * @param int|null $leadId
25
     *
26
     * @return array
27
     */
28
    public function getLeadTimelineEvents($leadId = null, array $options = [])
29
    {
30
        $query = $this->getEntityManager()->getConnection()->createQueryBuilder()
31
            ->from(MAUTIC_TABLE_PREFIX.'lead_points_change_log', 'lp')
32
            ->select('lp.event_name as eventName, lp.action_name as actionName, lp.date_added as dateAdded, lp.type, lp.delta, lp.id, lp.lead_id');
33
34
        if ($leadId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $leadId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
35
            $query->where('lp.lead_id = '.(int) $leadId);
36
        }
37
38
        if (isset($options['search']) && $options['search']) {
39
            $query->andWhere($query->expr()->orX(
40
                $query->expr()->like('lp.event_name', $query->expr()->literal('%'.$options['search'].'%')),
41
                $query->expr()->like('lp.action_name', $query->expr()->literal('%'.$options['search'].'%'))
42
            ));
43
        }
44
45
        return $this->getTimelineResults($query, $options, 'lp.event_name', 'lp.date_added', [], ['dateAdded']);
46
    }
47
48
    /**
49
     * Get table stat data from point log table.
50
     *
51
     * @return array
52
     *
53
     * @throws \Doctrine\ORM\NoResultException
54
     * @throws \Doctrine\ORM\NonUniqueResultException
55
     */
56
    public function getMostPoints(QueryBuilder $query, $limit = 10, $offset = 0)
57
    {
58
        $query->setMaxResults($limit)
59
                ->setFirstResult($offset);
60
61
        return $query->execute()->fetchAll();
62
    }
63
64
    /**
65
     * Get table stat data from lead table.
66
     *
67
     * @return array
68
     *
69
     * @throws \Doctrine\ORM\NoResultException
70
     * @throws \Doctrine\ORM\NonUniqueResultException
71
     */
72
    public function getMostLeads(QueryBuilder $query, $limit = 10, $offset = 0)
73
    {
74
        $query->setMaxResults($limit)
75
                ->setFirstResult($offset);
76
77
        return $query->execute()->fetchAll();
78
    }
79
80
    /**
81
     * Updates lead ID (e.g. after a lead merge).
82
     *
83
     * @param int $fromLeadId
84
     * @param int $toLeadId
85
     */
86
    public function updateLead($fromLeadId, $toLeadId)
87
    {
88
        $q = $this->_em->getConnection()->createQueryBuilder();
89
        $q->update(MAUTIC_TABLE_PREFIX.'lead_points_change_log')
90
            ->set('lead_id', (int) $toLeadId)
91
            ->where('lead_id = '.(int) $fromLeadId)
92
            ->execute();
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getTableAlias()
99
    {
100
        return 'lp';
101
    }
102
}
103