Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

ChannelsByEntitiesProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getChannelsByEntities() 0 10 2
A getChannelsByEntitiesQB() 0 4 1
A getChannelRepository() 0 9 2
1
<?php
2
3
namespace OroCRM\Bundle\ChannelBundle\Provider;
4
5
use Doctrine\ORM\QueryBuilder;
6
7
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
8
9
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
10
use OroCRM\Bundle\ChannelBundle\Entity\Repository\ChannelRepository;
11
12
class ChannelsByEntitiesProvider
13
{
14
    /**
15
     * @var DoctrineHelper
16
     */
17
    protected $doctrineHelper;
18
19
    /**
20
     * @var ChannelRepository
21
     */
22
    protected $channelRepository;
23
24
    /**
25
     * @var array [{parameters_hash} => {Channel[]}, ...]
26
     */
27
    protected $channelsCache = [];
28
29
    /**
30
     * @param DoctrineHelper $doctrineHelper
31
     */
32
    public function __construct(DoctrineHelper $doctrineHelper)
33
    {
34
        $this->doctrineHelper = $doctrineHelper;
35
    }
36
37
    /**
38
     * @param array $entities
39
     * @param bool  $status
40
     *
41
     * @return mixed
42
     */
43
    public function getChannelsByEntities(array $entities = [], $status = Channel::STATUS_ACTIVE)
44
    {
45
        sort($entities);
46
        $hash = md5(serialize([$entities, $status]));
47
        if (!isset($this->channelsCache[$hash])) {
48
            $this->channelsCache[$hash] = $this->getChannelRepository()->getChannelsByEntities($entities, $status);
49
        }
50
51
        return $this->channelsCache[$hash];
52
    }
53
54
    /**
55
     * @param array $entities
56
     * @param bool  $status
57
     *
58
     * @return QueryBuilder
59
     */
60
    public function getChannelsByEntitiesQB(array $entities = [], $status = Channel::STATUS_ACTIVE)
61
    {
62
        return $this->getChannelRepository()->getChannelsByEntitiesQB($entities, $status);
63
    }
64
65
    /**
66
     * @return ChannelRepository
67
     */
68
    protected function getChannelRepository()
69
    {
70
        if (null === $this->channelRepository) {
71
            $this->channelRepository = $this->doctrineHelper
72
                ->getEntityRepositoryForClass('OroCRMChannelBundle:Channel');
73
        }
74
75
        return $this->channelRepository;
76
    }
77
}
78