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

getChannelsByEntities()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 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