1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bridge\CrmCall\Provider; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
|
8
|
|
|
use Oro\Bundle\ActivityBundle\EntityConfig\ActivityScope; |
9
|
|
|
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper; |
10
|
|
|
use Oro\Bundle\ActivityBundle\Manager\ActivityManager; |
11
|
|
|
|
12
|
|
|
use OroCRM\Bundle\ActivityContactBundle\Direction\DirectionProviderInterface; |
13
|
|
|
use OroCRM\Bundle\CallBundle\Entity\Call; |
14
|
|
|
|
15
|
|
|
class CallDirectionProvider implements DirectionProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ActivityManager */ |
18
|
|
|
protected $activityManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ActivityManager $activityManager |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ActivityManager $activityManager) |
24
|
|
|
{ |
25
|
|
|
$this->activityManager = $activityManager; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getSupportedClass() |
32
|
|
|
{ |
33
|
|
|
return 'OroCRM\Bundle\CallBundle\Entity\Call'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getDirection($activity, $target) |
40
|
|
|
{ |
41
|
|
|
/** @var $activity Call */ |
42
|
|
|
return $activity->getDirection()->getName(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function isDirectionChanged($changeSet = []) |
49
|
|
|
{ |
50
|
|
|
if (!empty($changeSet)) { |
51
|
|
|
return in_array('direction', array_keys($changeSet)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getDate($activity) |
61
|
|
|
{ |
62
|
|
|
/** @var $activity Call */ |
63
|
|
|
return $activity->getCallDateTime() ? : new \DateTime('now', new \DateTimeZone('UTC')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function getLastActivitiesDateForTarget(EntityManager $em, $target, $direction, $skipId = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$result = []; |
72
|
|
|
$resultActivity = $this->getLastActivity($em, $target, $skipId); |
73
|
|
|
if ($resultActivity) { |
74
|
|
|
$result['all'] = $this->getDate($resultActivity); |
75
|
|
|
if ($this->getDirection($resultActivity, $target) !== $direction) { |
76
|
|
|
$resultActivity = $this->getLastActivity($em, $target, $skipId, $direction); |
77
|
|
|
if ($resultActivity) { |
78
|
|
|
$result['direction'] = $this->getDate($resultActivity); |
79
|
|
|
} else { |
80
|
|
|
$result['direction'] = null; |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
$result['direction'] = $result['all']; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param EntityManager $em |
92
|
|
|
* @param object $target |
93
|
|
|
* @param integer $skipId |
94
|
|
|
* @param string $direction |
95
|
|
|
* |
96
|
|
|
* @return Call |
97
|
|
|
*/ |
98
|
|
|
protected function getLastActivity(EntityManager $em, $target, $skipId, $direction = null) |
99
|
|
|
{ |
100
|
|
|
if (!$this->activityManager->hasActivityAssociation( |
101
|
|
|
ClassUtils::getClass($target), |
102
|
|
|
$this->getSupportedClass() |
103
|
|
|
)) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$qb = $em->getRepository('OroCRM\Bundle\CallBundle\Entity\Call') |
108
|
|
|
->createQueryBuilder('call') |
109
|
|
|
->select('call') |
110
|
|
|
->innerJoin( |
111
|
|
|
sprintf( |
112
|
|
|
'call.%s', |
113
|
|
|
ExtendHelper::buildAssociationName(ClassUtils::getClass($target), ActivityScope::ASSOCIATION_KIND) |
114
|
|
|
), |
115
|
|
|
'target' |
116
|
|
|
) |
117
|
|
|
->andWhere('target = :target') |
118
|
|
|
->orderBy('call.callDateTime', 'DESC') |
119
|
|
|
->setMaxResults(1) |
120
|
|
|
->setParameter('target', $target->getId()); |
121
|
|
|
if ($skipId) { |
122
|
|
|
$qb->andWhere('call.id <> :skipId') |
123
|
|
|
->setParameter('skipId', $skipId); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($direction) { |
|
|
|
|
127
|
|
|
$qb->join('call.direction', 'direction') |
128
|
|
|
->andWhere('direction.name = :direction') |
129
|
|
|
->setParameter('direction', $direction); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $qb->getQuery()->getOneOrNullResult(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.