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

CustomerFrequencyProvider::getValue()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 29
Ratio 100 %
Metric Value
dl 29
loc 29
rs 8.8571
cc 2
eloc 20
nc 2
nop 1
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 CustomerFrequencyProvider 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_FREQUENCY;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function getScalarValues(Channel $channel, array $ids = [])
23
    {
24
        $qb = $this->doctrineHelper
25
            ->getEntityRepository($this->className)
26
            ->createQueryBuilder('c');
27
28
        $date = new \DateTime('now', new \DateTimeZone('UTC'));
29
        $qb
30
            ->select('COUNT(o) as value', 'c.id')
31
            ->join('c.orders', 'o')
32
            ->where(
33
                $qb->expr()->andX(
34
                    $qb->expr()->neq($qb->expr()->lower('o.status'), ':status'),
35
                    $qb->expr()->gte('o.createdAt', ':date'),
36
                    $qb->expr()->eq('c.dataChannel', ':channel')
37
                )
38
            )
39
            ->groupBy('c.id')
40
            ->orderBy($qb->expr()->asc('c.id'))
41
            ->setParameter('status', Order::STATUS_CANCELED)
42
            ->setParameter('channel', $channel)
43
            ->setParameter('date', $date->sub(new \DateInterval('P365D')));
44
45
        if (!empty($ids)) {
46
            $qb->andWhere($qb->expr()->in('c.id', ':ids'))
47
                ->setParameter('ids', $ids);
48
        }
49
50
        return $qb->getQuery()->getScalarResult();
51
    }
52
}
53