These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Action; |
||
15 | |||
16 | use Sonata\AdminBundle\Admin\Pool; |
||
17 | use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension; |
||
18 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
19 | use Symfony\Component\HttpFoundation\Request; |
||
20 | use Symfony\Component\HttpFoundation\Response; |
||
21 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
22 | use Symfony\Component\PropertyAccess\PropertyPath; |
||
23 | use Symfony\Component\Validator\Validator\ValidatorInterface; |
||
24 | use Twig\Environment; |
||
25 | |||
26 | final class SetObjectFieldValueAction |
||
27 | { |
||
28 | /** |
||
29 | * @var Pool |
||
30 | */ |
||
31 | private $pool; |
||
32 | |||
33 | /** |
||
34 | * @var Environment |
||
35 | */ |
||
36 | private $twig; |
||
37 | |||
38 | /** |
||
39 | * @var ValidatorInterface |
||
40 | */ |
||
41 | private $validator; |
||
42 | |||
43 | public function __construct(Environment $twig, Pool $pool, $validator) |
||
44 | { |
||
45 | // NEXT_MAJOR: Move ValidatorInterface check to method signature |
||
46 | if (!($validator instanceof ValidatorInterface)) { |
||
47 | throw new \InvalidArgumentException( |
||
48 | 'Argument 3 is an instance of '.\get_class($validator).', expecting an instance of' |
||
49 | .' \Symfony\Component\Validator\Validator\ValidatorInterface' |
||
50 | ); |
||
51 | } |
||
52 | $this->pool = $pool; |
||
53 | $this->twig = $twig; |
||
54 | $this->validator = $validator; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @throws NotFoundHttpException |
||
59 | */ |
||
60 | public function __invoke(Request $request): JsonResponse |
||
61 | { |
||
62 | $field = $request->get('field'); |
||
63 | $code = $request->get('code'); |
||
64 | $objectId = $request->get('objectId'); |
||
65 | $value = $originalValue = $request->get('value'); |
||
66 | $context = $request->get('context'); |
||
67 | |||
68 | $admin = $this->pool->getInstance($code); |
||
69 | $admin->setRequest($request); |
||
70 | |||
71 | // alter should be done by using a post method |
||
72 | if (!$request->isXmlHttpRequest()) { |
||
73 | return new JsonResponse('Expected an XmlHttpRequest request header', Response::HTTP_METHOD_NOT_ALLOWED); |
||
74 | } |
||
75 | |||
76 | if (Request::METHOD_POST !== $request->getMethod()) { |
||
77 | return new JsonResponse(sprintf('Invalid request method given "%s", %s expected', $request->getMethod(), Request::METHOD_POST), Response::HTTP_METHOD_NOT_ALLOWED); |
||
78 | } |
||
79 | |||
80 | $rootObject = $object = $admin->getObject($objectId); |
||
81 | |||
82 | if (!$object) { |
||
83 | return new JsonResponse('Object does not exist', Response::HTTP_NOT_FOUND); |
||
84 | } |
||
85 | |||
86 | // check user permission |
||
87 | if (false === $admin->hasAccess('edit', $object)) { |
||
88 | return new JsonResponse('Invalid permissions', Response::HTTP_FORBIDDEN); |
||
89 | } |
||
90 | |||
91 | if ('list' === $context) { |
||
92 | $fieldDescription = $admin->getListFieldDescription($field); |
||
93 | } else { |
||
94 | return new JsonResponse('Invalid context', Response::HTTP_BAD_REQUEST); |
||
95 | } |
||
96 | |||
97 | if (!$fieldDescription) { |
||
98 | return new JsonResponse('The field does not exist', Response::HTTP_BAD_REQUEST); |
||
99 | } |
||
100 | |||
101 | if (!$fieldDescription->getOption('editable')) { |
||
102 | return new JsonResponse('The field cannot be edited, editable option must be set to true', Response::HTTP_BAD_REQUEST); |
||
103 | } |
||
104 | |||
105 | $propertyPath = new PropertyPath($field); |
||
106 | |||
107 | // If property path has more than 1 element, take the last object in order to validate it |
||
108 | if ($propertyPath->getLength() > 1) { |
||
109 | $object = $this->pool->getPropertyAccessor()->getValue($object, $propertyPath->getParent()); |
||
0 ignored issues
–
show
|
|||
110 | |||
111 | $elements = $propertyPath->getElements(); |
||
112 | $field = end($elements); |
||
113 | $propertyPath = new PropertyPath($field); |
||
114 | } |
||
115 | |||
116 | // Handle date and datetime types have setter expecting a DateTime object |
||
117 | if ('' !== $value && \in_array($fieldDescription->getType(), ['date', 'datetime'], true)) { |
||
118 | $value = new \DateTime($value); |
||
119 | } |
||
120 | |||
121 | // Handle boolean type transforming the value into a boolean |
||
122 | if ('' !== $value && 'boolean' === $fieldDescription->getType()) { |
||
123 | $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
124 | } |
||
125 | |||
126 | // Handle entity choice association type, transforming the value into entity |
||
127 | if ('' !== $value |
||
128 | && 'choice' === $fieldDescription->getType() |
||
129 | && null !== $fieldDescription->getOption('class') |
||
130 | && $fieldDescription->getOption('class') === $fieldDescription->getTargetEntity() |
||
0 ignored issues
–
show
The method
Sonata\AdminBundle\Admin...face::getTargetEntity() has been deprecated with message: since sonata-project/admin-bundle 3.69. Use `getTargetModel()` instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. ![]() |
|||
131 | ) { |
||
132 | $value = $admin->getModelManager()->find($fieldDescription->getOption('class'), $value); |
||
133 | |||
134 | if (!$value) { |
||
135 | return new JsonResponse(sprintf( |
||
136 | 'Edit failed, object with id: %s not found in association: %s.', |
||
137 | $originalValue, |
||
138 | $field |
||
139 | ), Response::HTTP_NOT_FOUND); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | $this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null); |
||
144 | |||
145 | $violations = $this->validator->validate($object); |
||
146 | |||
147 | if (\count($violations)) { |
||
148 | $messages = []; |
||
149 | |||
150 | foreach ($violations as $violation) { |
||
151 | $messages[] = $violation->getMessage(); |
||
152 | } |
||
153 | |||
154 | return new JsonResponse(implode("\n", $messages), Response::HTTP_BAD_REQUEST); |
||
155 | } |
||
156 | |||
157 | $admin->update($object); |
||
0 ignored issues
–
show
It seems like
$object can also be of type array ; however, Sonata\AdminBundle\Admin...iderInterface::update() does only seem to accept object , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
158 | |||
159 | // render the widget |
||
160 | // todo : fix this, the twig environment variable is not set inside the extension ... |
||
161 | $extension = $this->twig->getExtension(SonataAdminExtension::class); |
||
162 | \assert($extension instanceof SonataAdminExtension); |
||
163 | |||
164 | $content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription); |
||
0 ignored issues
–
show
It seems like
$rootObject defined by $object = $admin->getObject($objectId) on line 80 can also be of type null ; however, Sonata\AdminBundle\Twig\...on::renderListElement() does only seem to accept object , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
165 | |||
166 | return new JsonResponse($content, Response::HTTP_OK); |
||
167 | } |
||
168 | } |
||
169 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: