Completed
Push — 1.9 ( 482137...30c3e5 )
by
unknown
43:10
created

ActivityContactProvider::getActivityDirection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\ActivityContactBundle\Provider;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Doctrine\ORM\EntityManager;
7
8
use OroCRM\Bundle\ActivityContactBundle\Direction\DirectionProviderInterface;
9
10
class ActivityContactProvider
11
{
12
    /** @var DirectionProviderInterface[] */
13
    protected $providers;
14
15
    /**
16
     * @param DirectionProviderInterface $provider
17
     */
18
    public function addProvider(DirectionProviderInterface $provider)
19
    {
20
        $this->providers[$provider->getSupportedClass()] = $provider;
21
    }
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()
65
    {
66
        return array_keys($this->providers);
67
    }
68
69
    /**
70
     * Check if given entity class is supports
71
     *
72
     * @param $activityClass
73
     *
74
     * @return bool
75
     */
76
    public function isSupportedEntity($activityClass)
77
    {
78
        return isset($this->providers[$activityClass]);
79
    }
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression $skippedId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $allDates of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
117
            $allDate = $this->getMaxDate($allDates);
118
        }
119
120
        if ($directionDates) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $directionDates of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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)
135
    {
136
        if (count($datesArray) > 1) {
137
            usort(
138
                $datesArray,
139
                function (\DateTime $a, \DateTime $b) {
140
                    $firstStamp  = $a->getTimestamp();
141
                    $secondStamp = $b->getTimestamp();
142
                    if ($firstStamp === $secondStamp) {
143
                        return 0;
144
                    }
145
146
                    return ($firstStamp > $secondStamp) ? -1 : 1;
147
                }
148
            );
149
        }
150
151
        return array_shift($datesArray);
152
    }
153
154
    /**
155
     * Get contact activity direction provider
156
     *
157
     * @param $activity
158
     *
159
     * @return bool|DirectionProviderInterface
160
     */
161
    public function getActivityDirectionProvider($activity)
162
    {
163
        $activityClass = ClassUtils::getClass($activity);
164
        if (isset($this->providers[$activityClass])) {
165
            return $this->providers[$activityClass];
166
        }
167
168
        return false;
169
    }
170
}
171