Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

CustomerMonetaryProvider::getScalarValues()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 23

Duplication

Lines 33
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 33
loc 33
rs 8.8571
c 2
b 0
f 1
cc 2
eloc 23
nc 2
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Provider\Analytics;
4
5
use OroCRM\Bundle\AnalyticsBundle\Entity\RFMMetricCategory;
6
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
7
use OroCRM\Bundle\MagentoBundle\Entity\Order;
8
9 View Code Duplication
class CustomerMonetaryProvider extends AbstractCustomerRFMProvider
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function getType()
15
    {
16
        return RFMMetricCategory::TYPE_MONETARY;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function getScalarValues(Channel $channel, array $ids = [])
23
    {
24
        $date = new \DateTime('now', new \DateTimeZone('UTC'));
25
        $qb = $this->doctrineHelper
26
            ->getEntityRepository($this->className)
27
            ->createQueryBuilder('c');
28
29
        $qb
30
            ->select('SUM(
31
                CASE WHEN o.subtotalAmount IS NOT NULL THEN o.subtotalAmount ELSE 0 END -
32
                CASE WHEN o.discountAmount IS NOT NULL THEN ABS(o.discountAmount) ELSE 0 END
33
                ) as value', 'c.id')
34
            ->join('c.orders', 'o')
35
            ->where(
36
                $qb->expr()->andX(
37
                    $qb->expr()->neq($qb->expr()->lower('o.status'), ':status'),
38
                    $qb->expr()->gte('o.createdAt', ':date'),
39
                    $qb->expr()->eq('c.dataChannel', ':channel')
40
                )
41
            )
42
            ->groupBy('c.id')
43
            ->orderBy($qb->expr()->asc('c.id'))
44
            ->setParameter('status', Order::STATUS_CANCELED)
45
            ->setParameter('channel', $channel)
46
            ->setParameter('date', $date->sub(new \DateInterval('P365D')));
47
48
        if (!empty($ids)) {
49
            $qb->andWhere($qb->expr()->in('c.id', ':ids'))
50
                ->setParameter('ids', $ids);
51
        }
52
53
        return $qb->getQuery()->getScalarResult();
54
    }
55
}
56