Completed
Pull Request — 2.1 (#242)
by Łukasz
03:04
created

DependentElementImpl::getParentDataIndexer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Admin;
11
12
use FSi\Bundle\AdminBundle\Admin\CRUD\DataIndexerElement;
13
use FSi\Component\DataIndexer\DataIndexerInterface;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
16
trait DependentElementImpl
17
{
18
    /**
19
     * @var RequestStack
20
     */
21
    private $requestStack;
22
23
    /**
24
     * @var Element
25
     */
26
    private $parentElement;
27
28
    public function setParentElement(Element $element)
29
    {
30
        $this->parentElement = $element;
31
    }
32
33
    /**
34
     * @return Element
35
     */
36
    public function getParentElement()
37
    {
38
        return $this->parentElement;
39
    }
40
41
    public function setRequestStack(RequestStack $requestStack)
42
    {
43
        $this->requestStack = $requestStack;
44
    }
45
46
    /**
47
     * @return object|null
48
     */
49
    public function getParentObject()
50
    {
51
        $dataIndexer = $this->getParentDataIndexer();
52
        $parentObjectId = $this->getParentObjectId();
53
54
        if ($dataIndexer !== null && $parentObjectId !== null) {
55
            return $dataIndexer->getData($parentObjectId);
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    protected function getParentObjectId()
65
    {
66
        return $this->requestStack->getCurrentRequest()->get(DependentElement::REQUEST_PARENT_PARAMETER);
67
    }
68
69
    /**
70
     * @return DataIndexerInterface
71
     */
72
    protected function getParentDataIndexer()
73
    {
74
        if ($this->parentElement instanceof DataIndexerElement) {
75
            return $this->parentElement->getDataIndexer();
76
        }
77
78
        return null;
79
    }
80
}
81