|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use Doctrine\ORM\NoResultException; |
|
9
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
10
|
|
|
use Oro\Bundle\ConfigBundle\Config\ConfigManager; |
|
11
|
|
|
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper; |
|
12
|
|
|
use Oro\Bundle\TrackingBundle\Entity\UniqueTrackingVisit; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @method AclHelper getAclHelper() |
|
16
|
|
|
* @method ManagerRegistry getManagerRegistry() |
|
17
|
|
|
*/ |
|
18
|
|
|
trait PrecalculatedVisitProviderTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ConfigManager |
|
22
|
|
|
*/ |
|
23
|
|
|
private $configManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ConfigManager $configManager |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setConfigManager(ConfigManager $configManager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->configManager = $configManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param QueryBuilder $queryBuilder |
|
35
|
|
|
* @return int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function getSingleIntegerResult(QueryBuilder $queryBuilder) |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
return (int)$this->getAclHelper()->apply($queryBuilder)->getSingleScalarResult(); |
|
41
|
|
|
} catch (NoResultException $ex) { |
|
42
|
|
|
return 0; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param QueryBuilder $queryBuilder |
|
48
|
|
|
* @param \DateTime $from |
|
49
|
|
|
* @param \DateTime $to |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function applyDateLimit(QueryBuilder $queryBuilder, \DateTime $from, \DateTime $to) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($from && $to && $this->getDate($from) === $this->getDate($to)) { |
|
54
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->eq('t.firstActionTime', ':date')) |
|
55
|
|
|
->setParameter('date', $this->getDate($from)); |
|
56
|
|
|
} else { |
|
57
|
|
|
if ($from) { |
|
58
|
|
|
$queryBuilder |
|
59
|
|
|
->andWhere($queryBuilder->expr()->gte('t.firstActionTime', ':from')) |
|
60
|
|
|
->setParameter('from', $this->getDate($from)); |
|
61
|
|
|
} |
|
62
|
|
|
if ($to) { |
|
63
|
|
|
$queryBuilder |
|
64
|
|
|
->andWhere($queryBuilder->expr()->lte('t.firstActionTime', ':to')) |
|
65
|
|
|
->setParameter('to', $this->getDate($to)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return QueryBuilder |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createUniqueVisitQueryBuilder() |
|
74
|
|
|
{ |
|
75
|
|
|
$queryBuilder = $this |
|
76
|
|
|
->getUniqueTrackingVisitRepository() |
|
77
|
|
|
->createQueryBuilder('t'); |
|
78
|
|
|
|
|
79
|
|
|
return $queryBuilder; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function isPrecalculatedStatisticEnabled() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->configManager->get('oro_tracking.precalculated_statistic_enabled'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return EntityRepository |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getUniqueTrackingVisitRepository() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getManagerRegistry()->getManagerForClass(UniqueTrackingVisit::class) |
|
96
|
|
|
->getRepository(UniqueTrackingVisit::class); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param \DateTime $dateTime |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getDate(\DateTime $dateTime) |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var Connection $connection */ |
|
106
|
|
|
$connection = $this->getManagerRegistry()->getConnection(); |
|
107
|
|
|
$dateFormat = $connection->getDatabasePlatform()->getDateFormatString(); |
|
108
|
|
|
|
|
109
|
|
|
return $dateTime->format($dateFormat); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|