DependentElementImpl::setRequestStack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Admin;
13
14
use FSi\Bundle\AdminBundle\Admin\CRUD\DataIndexerElement;
15
use FSi\Component\DataIndexer\DataIndexerInterface;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
18
trait DependentElementImpl
19
{
20
    /**
21
     * @var RequestStack
22
     */
23
    private $requestStack;
24
25
    /**
26
     * @var Element
27
     */
28
    private $parentElement;
29
30
    public function setParentElement(Element $element): void
31
    {
32
        $this->parentElement = $element;
33
    }
34
35
    public function getParentElement(): Element
36
    {
37
        return $this->parentElement;
38
    }
39
40
    public function setRequestStack(RequestStack $requestStack): void
41
    {
42
        $this->requestStack = $requestStack;
43
    }
44
45
    /**
46
     * @return object|null
47
     */
48
    public function getParentObject()
49
    {
50
        $dataIndexer = $this->getParentDataIndexer();
51
        $parentObjectId = $this->getParentObjectId();
52
53
        if ($dataIndexer !== null && $parentObjectId !== null) {
54
            return $dataIndexer->getData($parentObjectId);
55
        }
56
57
        return null;
58
    }
59
60
    protected function getParentObjectId(): ?string
61
    {
62
        $currentRequest = $this->requestStack->getCurrentRequest();
63
64
        if ($currentRequest === null) {
65
            return null;
66
        }
67
68
        return $currentRequest->get(DependentElement::PARENT_REQUEST_PARAMETER);
69
    }
70
71
    protected function getParentDataIndexer(): ?DataIndexerInterface
72
    {
73
        if ($this->parentElement instanceof DataIndexerElement) {
74
            return $this->parentElement->getDataIndexer();
75
        }
76
77
        return null;
78
    }
79
}
80