Issues (3627)

CampaignBundle/Entity/ContactLimiterTrait.php (8 issues)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CampaignBundle\Entity;
13
14
use Doctrine\DBAL\Connection;
15
use Doctrine\DBAL\Query\QueryBuilder as DbalQueryBuilder;
16
use Doctrine\ORM\QueryBuilder as OrmQueryBuilder;
17
use Mautic\CampaignBundle\Executioner\ContactFinder\Limiter\ContactLimiter;
18
19
trait ContactLimiterTrait
20
{
21
    /**
22
     * @param string $alias
23
     * @param bool   $isCount
24
     */
25
    private function updateQueryFromContactLimiter($alias, DbalQueryBuilder $qb, ContactLimiter $contactLimiter, $isCount = false)
26
    {
27
        $minContactId = $contactLimiter->getMinContactId();
28
        $maxContactId = $contactLimiter->getMaxContactId();
29
        if ($contactId = $contactLimiter->getContactId()) {
30
            $qb->andWhere(
31
                $qb->expr()->eq("$alias.lead_id", ':contactId')
32
            )
33
                ->setParameter('contactId', $contactId, \Doctrine\DBAL\ParameterType::INTEGER);
34
        } elseif ($contactIds = $contactLimiter->getContactIdList()) {
35
            $qb->andWhere(
36
                $qb->expr()->in("$alias.lead_id", ':contactIds')
37
            )
38
                ->setParameter('contactIds', $contactIds, Connection::PARAM_INT_ARRAY);
39
        } elseif ($minContactId && $maxContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $minContactId 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...
Bug Best Practice introduced by
The expression $maxContactId 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...
40
            $qb->andWhere(
41
                "$alias.lead_id BETWEEN :minContactId AND :maxContactId"
42
            )
43
                ->setParameter('minContactId', $minContactId, \Doctrine\DBAL\ParameterType::INTEGER)
44
                ->setParameter('maxContactId', $maxContactId, \Doctrine\DBAL\ParameterType::INTEGER);
45
        } elseif ($minContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $minContactId 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...
46
            $qb->andWhere(
47
                $qb->expr()->gte("$alias.lead_id", ':minContactId')
48
            )
49
                ->setParameter('minContactId', $minContactId, \Doctrine\DBAL\ParameterType::INTEGER);
50
        } elseif ($maxContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxContactId 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...
51
            $qb->andWhere(
52
                $qb->expr()->lte("$alias.lead_id", ':maxContactId')
53
            )
54
                ->setParameter('maxContactId', $maxContactId, \Doctrine\DBAL\ParameterType::INTEGER);
55
        }
56
57
        if ($threadId = $contactLimiter->getThreadId()) {
58
            if ($maxThreads = $contactLimiter->getMaxThreads()) {
59
                if ($threadId <= $maxThreads) {
60
                    $qb->andWhere("MOD(($alias.lead_id + :threadShift), :maxThreads) = 0")
61
                        ->setParameter('threadShift', $threadId - 1, \Doctrine\DBAL\ParameterType::INTEGER)
62
                        ->setParameter('maxThreads', $maxThreads, \Doctrine\DBAL\ParameterType::INTEGER);
63
                }
64
            }
65
        }
66
67
        if (!$isCount && $limit = $contactLimiter->getBatchLimit()) {
68
            $qb->setMaxResults($limit);
69
        }
70
    }
71
72
    /**
73
     * @param string $alias
74
     * @param bool   $isCount
75
     */
76
    private function updateOrmQueryFromContactLimiter($alias, OrmQueryBuilder $qb, ContactLimiter $contactLimiter, $isCount = false)
77
    {
78
        $minContactId = $contactLimiter->getMinContactId();
79
        $maxContactId = $contactLimiter->getMaxContactId();
80
        if ($contactId = $contactLimiter->getContactId()) {
81
            $qb->andWhere(
82
                $qb->expr()->eq("IDENTITY($alias.lead)", ':contact')
83
            )
84
                ->setParameter('contact', $contactId, \Doctrine\DBAL\ParameterType::INTEGER);
85
        } elseif ($contactIds = $contactLimiter->getContactIdList()) {
86
            $qb->andWhere(
87
                $qb->expr()->in("IDENTITY($alias.lead)", ':contactIds')
88
            )
89
                ->setParameter('contactIds', $contactIds, Connection::PARAM_INT_ARRAY);
90
        } elseif ($minContactId && $maxContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxContactId 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...
Bug Best Practice introduced by
The expression $minContactId 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...
91
            $qb->andWhere(
92
                "IDENTITY($alias.lead) BETWEEN :minContactId AND :maxContactId"
93
            )
94
                ->setParameter('minContactId', $minContactId, \Doctrine\DBAL\ParameterType::INTEGER)
95
                ->setParameter('maxContactId', $maxContactId, \Doctrine\DBAL\ParameterType::INTEGER);
96
        } elseif ($minContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $minContactId 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...
97
            $qb->andWhere(
98
                $qb->expr()->gte("IDENTITY($alias.lead)", ':minContactId')
99
            )
100
                ->setParameter('minContactId', $minContactId, \Doctrine\DBAL\ParameterType::INTEGER);
101
        } elseif ($maxContactId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxContactId 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...
102
            $qb->andWhere(
103
                $qb->expr()->lte("IDENTITY($alias.lead)", ':maxContactId')
104
            )
105
                ->setParameter('maxContactId', $maxContactId, \Doctrine\DBAL\ParameterType::INTEGER);
106
        }
107
108
        if ($threadId = $contactLimiter->getThreadId()) {
109
            if ($maxThreads = $contactLimiter->getMaxThreads()) {
110
                $qb->andWhere("MOD((IDENTITY($alias.lead) + :threadShift), :maxThreads) = 0")
111
                    ->setParameter('threadShift', $threadId - 1, \Doctrine\DBAL\ParameterType::INTEGER)
112
                    ->setParameter('maxThreads', $maxThreads, \Doctrine\DBAL\ParameterType::INTEGER);
113
            }
114
        }
115
116
        if (!$isCount && $limit = $contactLimiter->getBatchLimit()) {
117
            $qb->setMaxResults($limit);
118
        }
119
    }
120
}
121