EventType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
eloc 21
dl 0
loc 55
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 18 3
A __construct() 0 3 1
A configureOptions() 0 5 1
A endsWith() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the thealternativezurich/feedback project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Form\Event;
13
14
use App\Entity\Event;
15
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19
use Symfony\Component\Form\Extension\Core\Type\DateType;
20
use Symfony\Component\Form\Extension\Core\Type\TextType;
21
use Symfony\Component\Form\Extension\Core\Type\TimeType;
22
use Symfony\Component\Form\FormBuilderInterface;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
class EventType extends AbstractType
26
{
27
    /** @var string */
28
    private $publicDir;
29
30
    /**
31
     * EventType constructor.
32
     */
33
    public function __construct(ParameterBagInterface $parameterBag)
34
    {
35
        $this->publicDir = $parameterBag->get('PUBLIC_DIR');
36
    }
37
38
    public function buildForm(FormBuilderInterface $builder, array $options)
39
    {
40
        $builder->add('name', TextType::class);
41
        $builder->add('date', DateType::class, ['input' => 'string', 'widget' => 'single_text']);
42
43
        $builder->add('feedbackStartTime', TimeType::class, ['input' => 'string', 'widget' => 'single_text']);
44
        $builder->add('feedbackEndTime', TimeType::class, ['input' => 'string', 'widget' => 'single_text']);
45
46
        //get all templates
47
        $templates = [];
48
        foreach (scandir($this->publicDir.'/templates') as $template) {
49
            if ($this->endsWith($template, '.json')) {
50
                $templates[mb_substr($template, 0, -5)] = $template;
51
            }
52
        }
53
        $builder->add('templateName', ChoiceType::class, ['choices' => $templates, 'choice_translation_domain' => false, 'help' => 'help.template_name']);
54
        $builder->add('hasExercise', CheckboxType::class, ['required' => false, 'help' => 'help.has_exercise']);
55
        $builder->add('hasLecture', CheckboxType::class, ['required' => false, 'help' => 'help.has_lecture']);
56
    }
57
58
    /**
59
     * @param string $haystack
60
     * @param string $needle
61
     *
62
     * @return bool
63
     */
64
    private function endsWith($haystack, $needle)
65
    {
66
        // search forward starting from end minus needle length characters
67
        if ('' === $needle) {
68
            return true;
69
        }
70
        $diff = mb_strlen($haystack) - mb_strlen($needle);
71
72
        return $diff >= 0 && false !== mb_strpos($haystack, $needle, $diff);
73
    }
74
75
    public function configureOptions(OptionsResolver $resolver)
76
    {
77
        $resolver->setDefaults([
78
            'data_class' => Event::class,
79
            'translation_domain' => 'entity_event',
80
        ]);
81
    }
82
}
83