Issues (3627)

Entity/ObjectMappingRepository.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright   2018 Mautic Contributors. All rights reserved
7
 * @author      Mautic, Inc.
8
 *
9
 * @link        https://mautic.org
10
 *
11
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Mautic\IntegrationsBundle\Entity;
15
16
use Doctrine\DBAL\Connection;
17
use Mautic\CoreBundle\Entity\CommonRepository;
18
19
class ObjectMappingRepository extends CommonRepository
20
{
21
    /**
22
     * @param $integration
23
     * @param $integrationObjectName
24
     * @param $integrationObjectId
25
     * @param $internalObjectName
26
     *
27
     * @return array|null
28
     */
29
    public function getInternalObject($integration, $integrationObjectName, $integrationObjectId, $internalObjectName)
30
    {
31
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();
32
        $qb->select('*')
33
            ->from(MAUTIC_TABLE_PREFIX.'sync_object_mapping', 'i')
34
            ->where(
35
                $qb->expr()->andX(
36
                    $qb->expr()->eq('i.integration', ':integration'),
37
                    $qb->expr()->eq('i.integration_object_name', ':integrationObjectName'),
38
                    $qb->expr()->eq('i.integration_object_id', ':integrationObjectId'),
39
                    $qb->expr()->eq('i.internal_object_name', ':internalObjectName')
40
                )
41
            )
42
            ->setParameter('integration', $integration)
43
            ->setParameter('integrationObjectName', $integrationObjectName)
44
            ->setParameter('integrationObjectId', $integrationObjectId)
45
            ->setParameter('internalObjectName', $internalObjectName);
46
47
        $result = $qb->execute()->fetch();
48
49
        return $result ?: null;
50
    }
51
52
    /**
53
     * @param $integration
54
     * @param $internalObjectName
55
     * @param $internalObjectId
56
     * @param $integrationObjectName
57
     *
58
     * @return array|null
59
     */
60
    public function getIntegrationObject($integration, $internalObjectName, $internalObjectId, $integrationObjectName)
61
    {
62
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();
63
        $qb->select('*')
64
            ->from(MAUTIC_TABLE_PREFIX.'sync_object_mapping', 'i')
65
            ->where(
66
                $qb->expr()->andX(
67
                    $qb->expr()->eq('i.integration', ':integration'),
68
                    $qb->expr()->eq('i.internal_object_name', ':internalObjectName'),
69
                    $qb->expr()->eq('i.internal_object_id', ':internalObjectId'),
70
                    $qb->expr()->eq('i.integration_object_name', ':integrationObjectName')
71
                )
72
            )
73
            ->setParameter('integration', $integration)
74
            ->setParameter('internalObjectName', $internalObjectName)
75
            ->setParameter('internalObjectId', $internalObjectId)
76
            ->setParameter('integrationObjectName', $integrationObjectName);
77
78
        $result = $qb->execute()->fetch();
79
80
        return $result ?: null;
81
    }
82
83
    /**
84
     * @param string $integration
85
     * @param string $oldObjectName
86
     * @param mixed  $oldObjectId
87
     * @param string $newObjectName
88
     * @param mixed  $newObjectId
89
     *
90
     * @return int
91
     */
92
    public function updateIntegrationObject($integration, $oldObjectName, $oldObjectId, $newObjectName, $newObjectId)
93
    {
94
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();
95
96
        $qb->update(MAUTIC_TABLE_PREFIX.'sync_object_mapping', 'i')
97
            ->set('integration_object_name', ':newObjectName')
98
            ->set('integration_object_id', ':newObjectId')
99
            ->where(
100
                $qb->expr()->andX(
101
                    $qb->expr()->eq('i.integration', ':integration'),
102
                    $qb->expr()->eq('i.integration_object_name', ':oldObjectName'),
103
                    $qb->expr()->eq('i.integration_object_id', ':oldObjectId')
104
                )
105
            )
106
            ->setParameter('newObjectName', $newObjectName)
107
            ->setParameter('newObjectId', $newObjectId)
108
            ->setParameter('integration', $integration)
109
            ->setParameter('oldObjectName', $oldObjectName)
110
            ->setParameter('oldObjectId', $oldObjectId);
111
112
        return $qb->execute();
113
    }
114
115
    /**
116
     * @param $integration
117
     * @param $objectName
118
     * @param string[]|string $objectIds
119
     *
120
     * @return \Doctrine\DBAL\Driver\Statement|int
121
     */
122
    public function markAsDeleted(string $integration, string $objectName, $objectIds): int
123
    {
124
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder();
125
126
        $qb->update(MAUTIC_TABLE_PREFIX.'sync_object_mapping', 'm')
127
            ->set('is_deleted', 1)
128
            ->where(
129
                $qb->expr()->andX(
130
                    $qb->expr()->eq('m.integration', ':integration'),
131
                    $qb->expr()->eq('m.integration_object_name', ':objectName')
132
                )
133
            )
134
            ->setParameter('integration', $integration)
135
            ->setParameter('objectName', $objectName);
136
137
        if (is_array($objectIds)) {
138
            $qb->setParameter('objectId', $objectIds, Connection::PARAM_STR_ARRAY);
139
            $qb->andWhere($qb->expr()->in('m.integration_object_id', ':objectId'));
140
        } else {
141
            $qb->setParameter('objectId', $objectIds);
142
            $qb->andWhere($qb->expr()->eq('m.integration_object_id', ':objectId'));
143
        }
144
145
        return $qb->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->execute() could return the type Doctrine\DBAL\Driver\Statement which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
146
    }
147
148
    public function deleteEntitiesForObject(int $internalObjectId, string $internalObject): void
149
    {
150
        $qb = $this->_em->createQueryBuilder();
151
        $qb->delete(ObjectMapping::class, 'm');
152
        $qb->where('m.internalObjectName = :internalObject');
153
        $qb->andWhere('m.internalObjectId = :internalObjectId');
154
        $qb->setParameter('internalObject', $internalObject);
155
        $qb->setParameter('internalObjectId', $internalObjectId);
156
        $qb->getQuery()->execute();
157
    }
158
159
    /**
160
     * @return ObjectMapping[]
161
     */
162
    public function getIntegrationMappingsForInternalObject(string $internalObject, int $internalObjectId): array
163
    {
164
        $qb = $this->createQueryBuilder('m');
165
        $qb->select('m')
166
            ->where(
167
                $qb->expr()->andX(
168
                    $qb->expr()->eq('m.internalObjectName', ':internalObject'),
169
                    $qb->expr()->eq('m.internalObjectId', ':internalObjectId')
170
                )
171
            )
172
            ->setParameter('internalObject', $internalObject)
173
            ->setParameter('internalObjectId', $internalObjectId);
174
175
        return $qb->getQuery()->getResult();
176
    }
177
}
178