1 | <?php |
||
10 | class ActivityContactProvider |
||
11 | { |
||
12 | /** @var DirectionProviderInterface[] */ |
||
13 | protected $providers; |
||
14 | |||
15 | /** |
||
16 | * @param DirectionProviderInterface $provider |
||
17 | */ |
||
18 | public function addProvider(DirectionProviderInterface $provider) |
||
22 | |||
23 | /** |
||
24 | * Return direction of given activity. |
||
25 | * |
||
26 | * @param object $activity |
||
27 | * @param object $target |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function getActivityDirection($activity, $target) |
||
32 | { |
||
33 | $provider = $this->getActivityDirectionProvider($activity); |
||
34 | if ($provider) { |
||
35 | return $provider->getDirection($activity, $target); |
||
36 | } |
||
37 | |||
38 | return DirectionProviderInterface::DIRECTION_UNKNOWN; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get contact date |
||
43 | * |
||
44 | * @param $activity |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function getActivityDate($activity) |
||
49 | { |
||
50 | $provider = $this->getActivityDirectionProvider($activity); |
||
51 | if ($provider) { |
||
52 | return $provider->getDate($activity); |
||
53 | } |
||
54 | |||
55 | return false; |
||
56 | } |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Return list of supported activity classes |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | public function getSupportedActivityClasses() |
||
68 | |||
69 | /** |
||
70 | * Check if given entity class is supports |
||
71 | * |
||
72 | * @param $activityClass |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isSupportedEntity($activityClass) |
||
80 | |||
81 | /** |
||
82 | * Get array with all and direction dates for given target |
||
83 | * |
||
84 | * @param EntityManager $em |
||
85 | * @param object $targetEntity |
||
86 | * @param string $direction |
||
87 | * @param integer $skippedId |
||
88 | * @param string $class |
||
89 | * |
||
90 | * @return array of all and direction dates |
||
91 | * - all: Last activity date without regard to the direction |
||
92 | * - direction: Last activity date for given direction |
||
93 | */ |
||
94 | public function getLastContactActivityDate( |
||
95 | EntityManager $em, |
||
96 | $targetEntity, |
||
97 | $direction, |
||
98 | $skippedId = null, |
||
99 | $class = null |
||
100 | ) { |
||
101 | $allDate = null; |
||
102 | $directionDate = null; |
||
103 | $allDates = []; |
||
104 | $directionDates = []; |
||
105 | foreach ($this->providers as $supportedClass => $provider) { |
||
106 | $skippedId = ($skippedId && $supportedClass === $class) ? $skippedId : null; |
||
|
|||
107 | $result = $provider->getLastActivitiesDateForTarget($em, $targetEntity, $direction, $skippedId); |
||
108 | if (!empty($result)) { |
||
109 | $allDates[] = $result['all']; |
||
110 | if ($result['direction']) { |
||
111 | $directionDates[] = $result['direction']; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if ($allDates) { |
||
117 | $allDate = $this->getMaxDate($allDates); |
||
118 | } |
||
119 | |||
120 | if ($directionDates) { |
||
121 | $directionDate = $this->getMaxDate($directionDates); |
||
122 | } |
||
123 | |||
124 | return ['all' => $allDate, 'direction' => $directionDate]; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get max date from the array of dates |
||
129 | * |
||
130 | * @param \DateTime[] $datesArray |
||
131 | * |
||
132 | * @return \DateTime |
||
133 | */ |
||
134 | protected function getMaxDate($datesArray) |
||
153 | |||
154 | /** |
||
155 | * Get contact activity direction provider |
||
156 | * |
||
157 | * @param $activity |
||
158 | * |
||
159 | * @return bool|DirectionProviderInterface |
||
160 | */ |
||
161 | public function getActivityDirectionProvider($activity) |
||
170 | } |
||
171 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: