|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\AnalyticsBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
|
6
|
|
|
|
|
7
|
|
|
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper; |
|
8
|
|
|
|
|
9
|
|
|
use OroCRM\Bundle\AnalyticsBundle\Command\CalculateAnalyticsCommand; |
|
10
|
|
|
use OroCRM\Bundle\AnalyticsBundle\Entity\RFMMetricCategory; |
|
11
|
|
|
use OroCRM\Bundle\ChannelBundle\Entity\Channel; |
|
12
|
|
|
|
|
13
|
|
|
class RFMMetricStateManager extends StateManager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $interface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $channelClass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param DoctrineHelper $doctrineHelper |
|
27
|
|
|
* @param string $interface |
|
28
|
|
|
* @param string $channelClass |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(DoctrineHelper $doctrineHelper, $interface, $channelClass) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($doctrineHelper); |
|
33
|
|
|
|
|
34
|
|
|
$this->interface = $interface; |
|
35
|
|
|
$this->channelClass = $channelClass; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Channel $channel |
|
40
|
|
|
*/ |
|
41
|
|
|
public function resetMetrics(Channel $channel = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$criteria = []; |
|
44
|
|
|
|
|
45
|
|
|
if ($channel) { |
|
46
|
|
|
$criteria = ['id' => $this->doctrineHelper->getSingleEntityIdentifier($channel)]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @var Channel[] $channels */ |
|
50
|
|
|
$channels = $this->doctrineHelper->getEntityRepository($this->channelClass)->findBy($criteria); |
|
51
|
|
|
|
|
52
|
|
|
$channelsByCustomerIdentity = []; |
|
53
|
|
|
foreach ($channels as $channel) { |
|
54
|
|
|
$customerIdentity = $channel->getCustomerIdentity(); |
|
55
|
|
|
|
|
56
|
|
|
if (!$customerIdentity) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!in_array($this->interface, class_implements($customerIdentity), true)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$channelsByCustomerIdentity[$customerIdentity][] = $this->doctrineHelper |
|
65
|
|
|
->getSingleEntityIdentifier($channel); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($channelsByCustomerIdentity as $className => $channelIds) { |
|
69
|
|
|
$this->executeResetQuery($className, $channelIds); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $className |
|
75
|
|
|
* @param array $ids |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function executeResetQuery($className, $ids) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$ids) { |
|
|
|
|
|
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$qb = $this->doctrineHelper |
|
84
|
|
|
->getEntityManager($className) |
|
85
|
|
|
->createQueryBuilder() |
|
86
|
|
|
->update($className, 'e'); |
|
87
|
|
|
|
|
88
|
|
|
foreach (RFMMetricCategory::$types as $type) { |
|
89
|
|
|
$qb |
|
90
|
|
|
->set(sprintf('e.%s', $type), sprintf(':%s', $type)) |
|
91
|
|
|
->setParameter($type, null); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$qb |
|
95
|
|
|
->where($qb->expr()->in('e.dataChannel', ':dataChannels')) |
|
96
|
|
|
->setParameter('dataChannels', $ids); |
|
97
|
|
|
|
|
98
|
|
|
$qb->getQuery()->execute(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Channel $channel |
|
103
|
|
|
*/ |
|
104
|
|
|
public function scheduleRecalculation(Channel $channel = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($channel) { |
|
107
|
|
|
$argument = sprintf('--channel=%s', $channel->getId()); |
|
108
|
|
|
|
|
109
|
|
|
if ($this->isJobRunning($argument)) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$isActiveChannel = $channel->getStatus() === Channel::STATUS_ACTIVE; |
|
114
|
|
|
$channelData = $channel->getData(); |
|
115
|
|
|
$rfmEnabled = !empty($channelData[RFMAwareInterface::RFM_STATE_KEY]); |
|
116
|
|
|
|
|
117
|
|
|
if (!$isActiveChannel || !$rfmEnabled) { |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($this->getJob()) { |
|
123
|
|
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$args = []; |
|
127
|
|
|
if ($channel) { |
|
128
|
|
|
$argument = sprintf('--channel=%s', $channel->getId()); |
|
129
|
|
|
$channelJob = $this->getJob($argument); |
|
130
|
|
|
if ($channelJob) { |
|
|
|
|
|
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$args = [$argument]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$job = new Job(CalculateAnalyticsCommand::COMMAND_NAME, $args); |
|
138
|
|
|
$em = $this->doctrineHelper->getEntityManager($job); |
|
139
|
|
|
|
|
140
|
|
|
if (!$channel) { |
|
141
|
|
|
$channelJobs = $this->getJob('--channel'); |
|
142
|
|
|
|
|
143
|
|
|
if ($channelJobs) { |
|
|
|
|
|
|
144
|
|
|
foreach ($channelJobs as $channelJob) { |
|
145
|
|
|
$em->remove($channelJob); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$em->persist($job); |
|
151
|
|
|
$em->flush($job); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.