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

CustomerRecencyProvider::getValue()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 2
eloc 19
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
class CustomerRecencyProvider extends AbstractCustomerRFMProvider
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function getType()
15
    {
16
        return RFMMetricCategory::TYPE_RECENCY;
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
        $qb
29
            ->select('MAX(o.createdAt) as value', 'c.id')
30
            ->join('c.orders', 'o')
31
            ->where($qb->expr()->andX(
32
                $qb->expr()->neq($qb->expr()->lower('o.status'), ':status'),
33
                $qb->expr()->eq('c.dataChannel', ':channel')
34
            ))
35
            ->groupBy('c.id')
36
            ->setParameter('status', Order::STATUS_CANCELED)
37
            ->setParameter('channel', $channel);
38
39
        if (!empty($ids)) {
40
            $qb->andWhere($qb->expr()->in('c.id', ':ids'))
41
                ->setParameter('ids', $ids);
42
        }
43
44
        $timezone = new \DateTimeZone('UTC');
45
        $now = new \DateTime('now', $timezone);
46
47
        return array_map(function ($value) use ($now, $timezone) {
48
            $value['value'] = $now->diff(new \DateTime($value['value'], $timezone))->days;
49
            return $value;
50
        }, $qb->getQuery()->getScalarResult());
51
    }
52
}
53