Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

APIController::fetchApiAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\APIBusinessEntityBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\PropertyAccess\PropertyAccessor;
11
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
12
13
/**
14
 * Api controller.
15
 *
16
 * @Route("/victoire-dcms/api")
17
 */
18
class APIController extends Controller
19
{
20
    /**
21
     * Method used to change of edit modefetch an api.
22
     *
23
     * @Route("/fetch/{businessEntity}", name="victoire_api_fetch", options={"expose"=true})
24
     *
25
     * @ParamConverter()
26
     *
27
     * @return JsonResponse Empty response
28
     */
29
    public function fetchApiAction(Request $request, BusinessEntity $businessEntity)
30
    {
31
        $accessor = new PropertyAccessor();
32
        $businessParameter = $businessEntity->getBusinessPropertiesByType(['textable', 'businessParameter'])->first();
0 ignored issues
show
Documentation introduced by
array('textable', 'businessParameter') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
        if (!$businessParameter) {
34
            throw new \Exception(
35
                sprintf('The BusinessEntity "%s" has no properties that are both "textable" and "BusinessParameter". it is needed to display a text to choose one',
36
                $businessEntity->getName())
37
            );
38
        }
39
40
        $filter = $request->query->get('q');
41
        $businessEntities = $this->get('victoire_business_entity.resolver.business_entity_resolver')->searchBusinessEntities($businessEntity, $businessParameter, $filter);
42
43
        $results = [];
44
        foreach ($businessEntities as $_businessEntity) {
45
            $results[] = [
46
                'id'   => $accessor->getValue($_businessEntity, $businessEntity->getBusinessIdentifiers()->first()->getName()),
47
                'text' => $accessor->getValue($_businessEntity, $businessParameter->getName()),
48
            ];
49
        }
50
51
        return new JsonResponse($results);
52
    }
53
}
54