Completed
Push — master ( 43fb10...664b94 )
by
unknown
12:57
created

CallDirectionProvider::getLastActivity()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 5
nop 4
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $direction of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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