Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminBundle/Helper/FormWidgets/Tabs/TabPane.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
7
use Symfony\Component\Form\Extension\Core\Type\FormType;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * A tab pane is a container which holds tabs
16
 */
17
class TabPane
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $identifier;
23
24
    /**
25
     * @var TabInterface[]
26
     */
27
    protected $tabs = [];
28
29
    /**
30
     * @var string
31
     */
32
    protected $activeTab;
33
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    protected $formFactory;
38
39
    /**
40
     * @var Form
41
     */
42
    protected $form;
43
44
    /**
45
     * @var FormView
46
     */
47
    protected $formView;
48
49
    /** @var Slugifier */
50
    private $slugifier;
51
52
    /**
53
     * @param string               $identifier  The identifier
54
     * @param Request              $request     The request
55
     * @param FormFactoryInterface $formFactory The form factory
56
     */
57 2
    public function __construct($identifier, Request $request, FormFactoryInterface $formFactory)
58
    {
59 2
        $this->identifier = $identifier;
60 2
        $this->formFactory = $formFactory;
61
62 2
        $this->slugifier = new Slugifier();
63 2
        if ($request->request->get('currenttab')) {
64 1
            $this->activeTab = $request->request->get('currenttab');
65 2
        } elseif ($request->get('currenttab')) {
66 1
            $this->activeTab = $request->get('currenttab');
67
        }
68 2
    }
69
70
    /**
71
     * @return FormInterface
72
     */
73 1
    public function buildForm()
74
    {
75 1
        $builder = $this->formFactory->createBuilder(FormType::class, null);
76
77 1
        foreach ($this->tabs as $tab) {
78 1
            $tab->buildForm($builder);
79
        }
80
81 1
        $this->form = $builder->getForm();
82
83 1
        return $this->form;
84
    }
85
86 1
    public function bindRequest(Request $request)
87
    {
88 1
        $this->form->handleRequest($request);
89
90 1
        foreach ($this->tabs as $tab) {
91 1
            $tab->bindRequest($request);
92
        }
93 1
    }
94
95
    /**
96
     * @param EntityManager $em The entity manager
97
     */
98 1
    public function persist(EntityManager $em)
99
    {
100 1
        foreach ($this->tabs as $tab) {
101 1
            $tab->persist($em);
102
        }
103 1
    }
104
105
    /**
106
     * @return string
107
     */
108 2
    private function generateIdentifier(TabInterface $tab)
109
    {
110 2
        return $this->slugifier->slugify($tab->getTitle());
111
    }
112
113
    /**
114
     * @param TabInterface $tab      The tab
115
     * @param int|null     $position The position
116
     *
117
     * @return TabPane
118
     */
119 2
    public function addTab(TabInterface $tab, $position = null)
120
    {
121 2
        $identifier = $tab->getIdentifier();
122 2
        if (!$identifier || empty($identifier)) {
123 2
            $tab->setIdentifier($this->generateIdentifier($tab));
124
        }
125
126 2
        if (!\is_null($position) && is_numeric($position) && $position < \count($this->tabs)) {
127 1
            array_splice($this->tabs, $position, 0, [$tab]);
128
        } else {
129 2
            $this->tabs[] = $tab;
130
        }
131
132 2
        return $this;
133
    }
134
135
    /**
136
     * @return TabPane
137
     */
138 1
    public function removeTab(TabInterface $tab)
139
    {
140 1
        if (\in_array($tab, $this->tabs)) {
141 1
            unset($this->tabs[array_search($tab, $this->tabs)]);
142 1
            $this->reindexTabs();
143
        }
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * @param string $title
150
     *
151
     * @return TabPane
152
     */
153 1
    public function removeTabByTitle($title)
154
    {
155 1
        foreach ($this->tabs as $key => $tab) {
156 1
            if ($tab->getTitle() === $title) {
157 1
                unset($this->tabs[$key]);
158 1
                $this->reindexTabs();
159
160 1
                return $this;
161
            }
162
        }
163
164 1
        return $this;
165
    }
166
167
    /**
168
     * @param int $position
169
     *
170
     * @return TabPane
171
     */
172 1 View Code Duplication
    public function removeTabByPosition($position)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174 1
        if (is_numeric($position) && $position < \count($this->tabs)) {
175 1
            array_splice($this->tabs, $position, 1);
176
        }
177
178 1
        return $this;
179
    }
180
181
    /**
182
     * @return TabInterface[]
183
     */
184 2
    public function getTabs()
185
    {
186 2
        return $this->tabs;
187
    }
188
189
    /**
190
     * @param string $title
191
     *
192
     * @return TabInterface|null
193
     */
194 1
    public function getTabByTitle($title)
195
    {
196 1
        foreach ($this->tabs as $key => $tab) {
197 1
            if ($tab->getTitle() === $title) {
198 1
                return $this->tabs[$key];
199
            }
200
        }
201
202 1
        return null;
203
    }
204
205
    /**
206
     * @param int $position
207
     *
208
     * @return TabInterface|null
209
     */
210 1 View Code Duplication
    public function getTabByPosition($position)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212 1
        if (is_numeric($position) && $position < \count($this->tabs)) {
213 1
            return $this->tabs[$position];
214
        }
215
216 1
        return null;
217
    }
218
219
    /**
220
     * @return string
221
     */
222 1
    public function getActiveTab()
223
    {
224 1
        return !empty($this->activeTab) ? $this->activeTab : $this->tabs[0]->getIdentifier();
225
    }
226
227
    /**
228
     * @return Form
229
     */
230 1
    public function getForm()
231
    {
232 1
        return $this->form;
233
    }
234
235
    /**
236
     * @return FormView
237
     */
238 1
    public function getFormView()
239
    {
240 1
        if (\is_null($this->formView)) {
241 1
            $this->formView = $this->form->createView();
242
        }
243
244 1
        return $this->formView;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250 1
    public function isValid()
251
    {
252 1
        return $this->form->isValid();
253
    }
254
255
    /**
256
     * Reset the indexes of the tabs
257
     */
258 1
    private function reindexTabs()
259
    {
260 1
        $this->tabs = array_values($this->tabs);
261 1
    }
262
263
    /**
264
     * @return array
265
     */
266 1
    public function getExtraParams(Request $request)
267
    {
268 1
        $extraParams = [];
269 1
        foreach ($this->getTabs() as $tab) {
270 1
            $extraParams = array_merge($extraParams, $tab->getExtraParams($request));
271
        }
272
273 1
        return $extraParams;
274
    }
275
}
276