Completed
Pull Request — master (#169)
by De Cramer
03:49
created

GridWindowFactory::setDataCollectionFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace eXpansion\Framework\Core\Plugins\Gui;
5
6
use eXpansion\Framework\Core\Helpers\Time;
7
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
9
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use FML\Controls\Frame;
12
13
/**
14
 * Class RecordsWindowFactory
15
 *
16
 * @package eXpansion\Bundle\LocalRecords\Plugins\Gui;
17
 * @author  reaby
18
 */
19
abstract class GridWindowFactory extends WindowFactory
20
{
21
    /** @var array */
22
    protected $genericData = [];
23
24
    /** @var GridBuilderFactory */
25
    protected $gridBuilderFactory;
26
27
    /** @var DataCollectionFactory */
28
    protected $dataCollectionFactory;
29
30
    /** @var Time */
31
    protected $timeFormatter;
32
33
    /**
34
     * @var GridBuilder
35
     */
36
    protected $gridBuilder;
37
38
    /** @var  Frame */
39
    protected $gridFrame;
40
41
    /** @var  float|null */
42
    protected $gridWidth;
43
44
    /** @var  float|null */
45
    protected $gridHeight;
46
47
    /** @var  float|null */
48
    protected $gridPosX = 0.;
49
50
    /** @var  float|null */
51
    protected $gridPosY = 0;
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function createContent(ManialinkInterface $manialink)
57
    {
58
        parent::createContent($manialink);
59
        $gridFrame = new Frame();
60
        $manialink->setData("gridFrame", $gridFrame);
61
62
        $manialink->getContentFrame()->addChild($manialink->getData('gridFrame'));
63
64
        $collection = $this->dataCollectionFactory->create([]);
65
        $manialink->setData("dataCollection", $collection);
66
67
        $this->createGrid($manialink);
68
    }
69
70
71
    /**
72
     * @param ManialinkInterface $manialink
73
     */
74
    abstract protected function createGrid(ManialinkInterface $manialink);
75
76
77
    /**
78
     * @inheritdoc
79
     */
80
    protected function updateContent(ManialinkInterface $manialink)
81
    {
82
        /** @var Frame $contentFrame */
83
        $contentFrame = $manialink->getContentFrame();
84
85
        $manialink->getData('gridFrame')->removeAllChildren();
86
87
        /** @var GridBuilder $gridBuilder */
88
        $gridBuilder = $manialink->getData('grid');
89
        $gridBuilder->setDataCollection($manialink->getData('dataCollection'));
90
91
        if (!$this->gridHeight) {
92
            $this->gridHeight = $contentFrame->getHeight();
93
        }
94
95
        if (!$this->gridWidth) {
96
            $this->gridWidth = $contentFrame->getWidth();
97
        }
98
99
        $manialink->getData('gridFrame')->setPosition($this->gridPosX, $this->gridPosY);
100
101
        $manialink->getData("gridFrame")->addChild($gridBuilder->build($this->gridWidth,
102
            $this->gridHeight));
103
104
        parent::updateContent($manialink);
105
    }
106
107
    /**
108
     * @param ManialinkInterface $manialink
109
     * @param array $data
110
     */
111
    public function setData(ManialinkInterface $manialink, $data)
112
    {
113
        $collection = $this->dataCollectionFactory->create($data);
114
        $manialink->setData('dataCollection', $collection);
115
    }
116
117
    /**
118
     * @param ManialinkInterface $manialink
119
     * @return array
120
     */
121
    public function getData(ManialinkInterface $manialink)
122
    {
123
        return $manialink->getData('dataCollection')->getAll();
124
    }
125
126
    /**
127
     * @param Time $time
128
     */
129
    public function setTimerFormatter(Time $time)
130
    {
131
        $this->timeFormatter = $time;
132
    }
133
134
135
    public function setGridSize($x, $y)
136
    {
137
        $this->gridWidth = $x;
138
        $this->gridHeight = $y;
139
    }
140
141
    public function setGridPosition($x, $y)
142
    {
143
        $this->gridPosX = $x;
144
        $this->gridPosY = $y;
145
    }
146
147
}
148