Passed
Push — master ( 9907da...28a731 )
by Nicolas
05:26
created

AbstractController::getSubscribedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Entity\User;
8
use App\Security\UserResolver;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController;
10
use Symfony\Component\Form\FormInterface;
11
12
/**
13
 * Intermediate controller allowing to provide additional shorthand methods to every child controller.
14
 */
15
class AbstractController extends BaseAbstractController
16
{
17
    public static function getSubscribedServices(): array
18
    {
19
        return \array_merge(parent::getSubscribedServices(), [
20
            // injecting the service here allows it to be used in the self::getCurrentUser() method
21
            // without having to inject it in every child controller __construct() method
22
            UserResolver::class,
23
        ]);
24
    }
25
26
    /**
27
     * Shorthand method to create named forms.
28
     */
29 12
    protected function createNamedForm(string $name, string $type, $data = null, array $options = []): FormInterface
30
    {
31 12
        return $this->get('form.factory')->createNamed($name, $type, $data, $options);
32
    }
33
34
    /**
35
     * Shorthand method to fetch the authenticated user, it also allows to forget about testing the user class
36
     * everywhere in the child controllers.
37
     */
38 16
    protected function getCurrentUser(): User
39
    {
40 16
        return $this->get(UserResolver::class)->getCurrentUser();
41
    }
42
}
43