Completed
Push — 1.9 ( 9955b8...d085a6 )
by
unknown
11:29
created

getDataChannelById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Autocomplete;
4
5
use Oro\Bundle\FormBundle\Autocomplete\SearchHandler;
6
use Oro\Bundle\SecurityBundle\SecurityFacade;
7
8
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
9
10
abstract class IntegrationAwareSearchHandler extends SearchHandler
11
{
12
    /** @var string */
13
    protected $dataChannelClass;
14
15
    /** @var SecurityFacade */
16
    protected $securityFacade;
17
18
    /**
19
     * @param string $dataChannelClass
20
     * @return IntegrationAwareSearchHandler
21
     */
22
    public function setDataChannelClass($dataChannelClass)
23
    {
24
        $this->dataChannelClass = $dataChannelClass;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param SecurityFacade $securityFacade
31
     */
32
    public function setSecurityFacade(SecurityFacade $securityFacade)
33
    {
34
        $this->securityFacade = $securityFacade;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function checkAllDependenciesInjected()
41
    {
42
        if (!$this->entityRepository || !$this->idFieldName || !$this->dataChannelClass) {
43
            throw new \RuntimeException('Search handler is not fully configured');
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function findById($query)
51
    {
52
        $parts = explode(';', $query);
53
        $id = $parts[0];
54
        $channelId = !empty($parts[1]) ? $parts[1] : false;
55
56
        $criteria = [$this->idFieldName => $id];
57
        if (false !== $channelId) {
58
            $dataChannel = $this->getDataChannelById($channelId);
59
            if ($dataChannel) {
60
                $criteria['channel'] = $dataChannel->getDataSource();
61
            }
62
        }
63
64
        return [$this->entityRepository->findOneBy($criteria, null)];
65
    }
66
67
    /**
68
     * @param int $dataChannelId
69
     * @return Channel
70
     */
71
    protected function getDataChannelById($dataChannelId)
72
    {
73
        /** @var Channel $dataChannel */
74
        return $this->objectManager->find($this->dataChannelClass, $dataChannelId);
75
    }
76
}
77