GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dd13da...b61209 )
by Richard
04:01 queued 54s
created

ManagementController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 28
lcom 1
cbo 9
dl 0
loc 264
rs 10
c 3
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 17 1
B helpAction() 0 29 2
F settingsAction() 0 212 25
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: wechsler
5
 * Date: 28/01/2017
6
 * Time: 19:12
7
 */
8
9
namespace Phase\TakeATicketBundle\Controller;
10
11
use Phase\TakeATicket\SongLoader;
12
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
13
use Symfony\Component\Form\Extension\Core\Type\FileType;
14
use Symfony\Component\Form\Extension\Core\Type\FormType;
15
use Symfony\Component\Form\Extension\Core\Type\NumberType;
16
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
17
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
18
use Symfony\Component\Form\Extension\Core\Type\TextType;
19
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
20
use Symfony\Component\Form\Forms;
21
use Symfony\Component\HttpFoundation\File\UploadedFile;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
25
class ManagementController extends BaseController
26
{
27
    public function indexAction()
28
    {
29
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
30
31
        $tickets = $this->getDataStore()->fetchUndeletedTickets();
32
33
        $performers = $this->getDataStore()->generatePerformerStats();
34
35
        return $this->render(
36
            'default/manage.html.twig',
37
            [
38
                'tickets' => $tickets,
39
                'performers' => $performers,
40
                'displayOptions' => $this->getDisplayOptions(),
41
            ]
42
        );
43
    }
44
45
46
    public function helpAction($section = 'readme')
47
    {
48
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
49
50
        /** @noinspection RealpathInSteamContextInspection */
51
        $rootDir = realpath(__DIR__ . '/../../../../');
52
        $map = [
53
            'readme' => $rootDir . '/README.md',
54
            'CONTRIBUTING' => $rootDir . '/docs/CONTRIBUTING.md',
55
            'TODO' => $rootDir . '/docs/TODO.md',
56
        ];
57
58
        if (!isset($map[$section])) {
59
            throw new NotFoundHttpException();
60
        }
61
62
        $markdown = file_get_contents($map[$section]);
63
64
        $markdown = preg_replace(
65
            '#\[docs/\w+.md\]\((./)?docs/(\w+).md\)#',
66
            '[docs/$2.md](/help/$2)',
67
            $markdown
68
        );
69
70
        return $this->render(
71
            ':default:help.html.twig',
72
            ['helpText' => $markdown]
73
        );
74
    }
75
76
    public function settingsAction(Request $request)
77
    {
78
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
79
80
        $requiredResetText = 'THIS DELETES ALL TICKETS';
81
82
        $settingKeys = [
83
            'freeze' => false,
84
            'freezeMessage' => '',
85
            'remotesUrl' => '/',
86
            'upcomingCount' => 3,
87
            'songInPreview' => false
88
        ];
89
90
        $formDefaults = $settingKeys;
91
92
        $dataStore = $this->getDataStore();
93
94
        foreach ($settingKeys as $key => $default) {
95
            $value = $dataStore->fetchSetting($key);
96
            if (is_null($value)) {
97
                $value = $default;
98
            } else {
99
                switch (gettype($default)) {
100
                    case 'integer':
101
                        $value = (int)$value;
102
                        break;
103
                    case 'boolean':
104
                        $value = (bool)$value;
105
                        break;
106
                }
107
            }
108
            $formDefaults[$key] = $value; // fixme handle type better
109
        }
110
111
        $formFactory = Forms::createFormFactoryBuilder()
112
            ->addExtension(new HttpFoundationExtension())
113
            ->getFormFactory();
114
115
        $settingsSubmit = 'Save Settings';
116
        $settingsForm = $formFactory->createNamedBuilder('settingsForm', FormType::class, $formDefaults)
117
            ->add('freeze', CheckboxType::class, ['label' => 'Display "Queue Frozen" message'])
118
            ->add('freezeMessage', TextType::class, ['label' => 'Customise "Queue Frozen" message'])
119
            ->add('remotesUrl', TextType::class, ['label' => 'URL to display on remote screens'])
120
            ->add('upcomingCount', NumberType::class, ['label' => 'Upcoming songs to display'])
121
            ->add('songInPreview', CheckboxType::class, ['label' => 'Display song titles on public queue'])
122
            ->add($settingsSubmit, SubmitType::class)
123
            ->getForm();
124
125
        $settingsFormSaved = false;
126
127
        if ($request->request->has('settingsForm')) {
128
            $settingsForm->handleRequest($request);
129
130
            /**
131
             * @noinspection PhpUndefinedMethodInspection
132
             */ // isClicked on Submit
133
            if ($settingsForm->isSubmitted()
134
                && $settingsForm->isValid()
135
                && $settingsForm->get($settingsSubmit)->isClicked()
136
            ) {
137
                $data = $settingsForm->getData();
138
                foreach ($data as $key => $default) {
139
                    $dataStore->updateSetting($key, $default);
140
                }
141
                $settingsFormSaved = true;
142
            }
143
        }
144
145
        // ----------------
146
147
        $resetSubmit = 'Reset all';
148
        $resetForm = $formFactory->createNamedBuilder('resetForm', FormType::class)
149
            ->add('resetMessage', TextType::class)
150
            ->add($resetSubmit, SubmitType::class)
151
            ->getForm();
152
153
154
        $resetFormSaved = false;
155
156
        if ($request->request->has('resetForm')) {
157
            $resetForm->handleRequest($request);
158
159
            /**
160
             * @noinspection PhpUndefinedMethodInspection
161
             */ // isClicked on Submit
162
            if ($resetForm->isSubmitted()
163
                && $resetForm->isValid()
164
                && $resetForm->get($resetSubmit)->isClicked()
165
            ) {
166
                $data = $resetForm->getData();
167
                //                var_dump($data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
                //                die();
169
                if (trim($data['resetMessage']) === $requiredResetText) {
170
                    $dataStore->resetAllSessionData();
171
                    $resetFormSaved = true;
172
                }
173
            }
174
        }
175
176
        // -------------------
177
178
        $songListSubmit = 'Upload song list';
179
        $songListForm = $formFactory->createNamedBuilder('songListForm', FormType::class)
180
            ->add('songListFile', FileType::class)
181
            ->add($songListSubmit, SubmitType::class)
182
            ->getForm();
183
184
        $songFormSaved = false;
185
        $songsLoaded = false;
186
187
        if ($request->request->has('songListForm')) {
188
            $songListForm->handleRequest($request);
189
190
            /**
191
             * @noinspection PhpUndefinedMethodInspection
192
             */ // isClicked on Submit
193
            if ($songListForm->isSubmitted()
194
                && $songListForm->isValid()
195
                && $songListForm->get($songListSubmit)->isClicked()
196
            ) {
197
                $data = $songListForm->getData();
198
199
                $file = $data['songListFile'];
200
                /**
201
                 * @var UploadedFile $file
202
                 */
203
204
                $loader = new SongLoader();
205
206
                $songsLoaded = $loader->run($file->getPathname(), $this->get('database_connection'));
207
208
                $songFormSaved = true;
209
            }
210
        }
211
212
        // -------------------
213
214
        $defaults = ['customCss' => $dataStore->fetchSetting('customCss')];
215
216
        $stylingSubmit = 'Update styles';
217
        $stylingForm = $formFactory->createNamedBuilder('stylingForm', FormType::class, $defaults)
218
            ->add('backgroundImageFile', FileType::class, ['label' => 'New background image'])
219
            ->add('customCss', TextareaType::class, ['attr' => ['rows' => 8, 'cols' => 60, 'label' => 'Custom CSS']])
220
            ->add($stylingSubmit, SubmitType::class)
221
            ->getForm();
222
223
        $styleFormSaved = false;
224
        $backgroundUpdated = false;
225
226
        if ($request->request->has('stylingForm')) {
227
            $stylingForm->handleRequest($request);
228
229
            /**
230
             * @noinspection PhpUndefinedMethodInspection
231
             */ // isClicked on Submit
232
            if ($stylingForm->isSubmitted()
233
                && $stylingForm->isValid()
234
                && $stylingForm->get($stylingSubmit)->isClicked()
235
            ) {
236
                $data = $stylingForm->getData();
237
238
                $file = $data['backgroundImageFile'];
239
                if ($file) {
240
                    $mimeType = $file->getMimeType();
241
                    $pathName = $file->getPathName();
242
                    /**
243
                     * @var UploadedFile $file
244
                     */
245
                    $suffixByMimeType = [
246
                        'image/jpeg' => 'jpg',
247
                        'image/gif' => 'gif',
248
                        'image/png' => 'png',
249
                    ];
250
251
                    if (array_key_exists($mimeType, $suffixByMimeType)) {
252
                        $suffix = $suffixByMimeType[$mimeType];
253
                        $targetFile = 'background.' . $suffix;
254
                        $destination = dirname($this->get('kernel')->getRootDir()) . '/web/uploads/' . $targetFile;
255
                        move_uploaded_file($pathName, $destination);
256
                        $dataStore->updateSetting('backgroundFilename', $targetFile);
257
                    } else {
258
                        throw new \UnexpectedValueException("Invalid mimetype '$mimeType'");
259
                    }
260
                    $backgroundUpdated = true;
261
                }
262
263
                $dataStore->updateSetting('customCss', $data['customCss']);
264
265
                $styleFormSaved = true;
266
            }
267
        }
268
269
        // -------------------
270
271
        return $this->render(
272
            ':admin:settings.html.twig',
273
            [
274
                'settingsFormSaved' => $settingsFormSaved,
275
                'settingsForm' => $settingsForm->createView(),
276
                'resetForm' => $resetForm->createView(),
277
                'resetFormSaved' => $resetFormSaved,
278
                'resetRequiredText' => $requiredResetText,
279
                'songListForm' => $songListForm->createView(),
280
                'songFormSaved' => $songFormSaved,
281
                'songsLoaded' => $songsLoaded,
282
                'stylingForm' => $stylingForm->createView(),
283
                'styleFormSaved' => $styleFormSaved,
284
                'backgroundUpdated' => $backgroundUpdated,
285
            ]
286
        );
287
    }
288
}
289