Completed
Pull Request — master (#6194)
by Vincent
05:04
created

HelperController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A appendFormFieldElementAction() 0 6 1
A retrieveFormFieldElementAction() 0 6 1
A getShortObjectDescriptionAction() 0 6 1
A setObjectFieldValueAction() 0 6 1
A retrieveAutocompleteItemsAction() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Controller;
15
16
use Sonata\AdminBundle\Action\AppendFormFieldElementAction;
17
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
18
use Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction;
19
use Sonata\AdminBundle\Action\RetrieveFormFieldElementAction;
20
use Sonata\AdminBundle\Action\SetObjectFieldValueAction;
21
use Sonata\AdminBundle\Admin\AdminHelper;
22
use Sonata\AdminBundle\Admin\Pool;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
28
use Symfony\Component\Validator\Validator\ValidatorInterface;
29
use Twig\Environment;
30
31
@trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
32
    'The %s\HelperController class is deprecated since version 3.38.0 and will be removed in 4.0.'
33
    .' Use actions inside Sonata\AdminBundle\Action instead.',
34
    __NAMESPACE__
35
), E_USER_DEPRECATED);
36
37
/**
38
 * NEXT_MAJOR: remove this class.
39
 *
40
 * @deprecated since sonata-project/admin-bundle 3.38.0, to be removed in 4.0. Use actions inside Sonata\AdminBundle\Action instead.
41
 *
42
 * @author Thomas Rabaix <[email protected]>
43
 */
44
class HelperController
45
{
46
    /**
47
     * @var Environment
48
     */
49
    protected $twig;
50
51
    /**
52
     * @var AdminHelper
53
     */
54
    protected $helper;
55
56
    /**
57
     * @var Pool
58
     */
59
    protected $pool;
60
61
    /**
62
     * @var ValidatorInterface
63
     */
64
    protected $validator;
65
66
    /**
67
     * @param ValidatorInterface $validator
68
     */
69
    public function __construct(Environment $twig, Pool $pool, AdminHelper $helper, $validator)
70
    {
71
        // NEXT_MAJOR: Move ValidatorInterface check to method signature
72
        if (!($validator instanceof ValidatorInterface)) {
73
            throw new \InvalidArgumentException(sprintf(
74
                'Argument 4 is an instance of %s, expecting an instance of %s',
75
                \get_class($validator),
76
                ValidatorInterface::class
77
            ));
78
        }
79
80
        $this->twig = $twig;
81
        $this->pool = $pool;
82
        $this->helper = $helper;
83
        $this->validator = $validator;
84
    }
85
86
    /**
87
     * @throws NotFoundHttpException
88
     *
89
     * @return Response
90
     */
91
    public function appendFormFieldElementAction(Request $request)
92
    {
93
        $action = new AppendFormFieldElementAction($this->twig, $this->pool, $this->helper);
94
95
        return $action($request);
96
    }
97
98
    /**
99
     * @throws NotFoundHttpException
100
     *
101
     * @return Response
102
     */
103
    public function retrieveFormFieldElementAction(Request $request)
104
    {
105
        $action = new RetrieveFormFieldElementAction($this->twig, $this->pool, $this->helper);
106
107
        return $action($request);
108
    }
109
110
    /**
111
     * @throws NotFoundHttpException|\RuntimeException
112
     *
113
     * @return Response
114
     */
115
    public function getShortObjectDescriptionAction(Request $request)
116
    {
117
        $action = new GetShortObjectDescriptionAction($this->twig, $this->pool);
118
119
        return $action($request);
120
    }
121
122
    /**
123
     * @return Response
124
     */
125
    public function setObjectFieldValueAction(Request $request)
126
    {
127
        $action = new SetObjectFieldValueAction($this->twig, $this->pool, $this->validator);
128
129
        return $action($request);
130
    }
131
132
    /**
133
     * Retrieve list of items for autocomplete form field.
134
     *
135
     * @throws \RuntimeException
136
     * @throws AccessDeniedException
137
     *
138
     * @return JsonResponse
139
     */
140
    public function retrieveAutocompleteItemsAction(Request $request)
141
    {
142
        $action = new RetrieveAutocompleteItemsAction($this->pool);
143
144
        return $action($request);
145
    }
146
}
147