GridController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
eloc 78
c 2
b 0
f 0
dl 0
loc 137
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B data() 0 48 8
B grid() 0 33 8
A delete() 0 9 1
B show() 0 26 7
1
<?php
2
3
namespace Dtc\GridBundle\Controller;
4
5
use Dtc\GridBundle\Grid\Renderer\AbstractRenderer;
6
use Dtc\GridBundle\Util\CamelCase;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class GridController
13
{
14
    private $container;
15
16
    public function __construct(ContainerInterface $container)
17
    {
18
        $this->container = $container;
19
    }
20
21
    public function grid(Request $request)
22
    {
23
        $class = $request->get('class');
24
        if (!$class) {
25
            throw $this->createNotFoundException('No class passed in');
0 ignored issues
show
Bug introduced by
The method createNotFoundException() does not exist on Dtc\GridBundle\Controller\GridController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            throw $this->/** @scrutinizer ignore-call */ createNotFoundException('No class passed in');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
        }
27
28
        if ($rendererId = $request->get('renderer')) {
29
            if (!$this->container->has($rendererId)) {
30
                throw new \Exception("No renderer found with id $rendererId");
31
            }
32
            if (!($renderer = $this->container->get($rendererId)) instanceof AbstractRenderer) {
33
                throw new \Exception("Rennderer $rendererId must be instanace of Dtc\GridBundle\Grid\Renderer\AbstractRenderer");
34
            }
35
            if (!($view = $request->get('view'))) {
36
                throw new \Exception("No view parameter specified for renderer $rendererId");
37
            }
38
        } else {
39
            $rendererType = $request->get('type', 'table');
40
            $renderer = $this->container->get('dtc_grid.renderer.factory')->create($rendererType);
41
            $view = '@DtcGrid/Page/'.$rendererType.'.html.twig';
42
        }
43
44
        $gridSource = $this->container->get('dtc_grid.manager.source')->get($class);
45
        $renderer->bind($gridSource);
46
47
        if ($this->container->has('templating')) {
48
            return new Response($this->container->get('templating')->render($view, $renderer->getParams()));
49
        } elseif ($this->container->has('twig')) {
50
            return new Response($this->container->get('twig')->render($view, $renderer->getParams()));
51
        }
52
53
        throw new \Exception('Need Twig Bundle or Templating component installed');
54
    }
55
56
    public function data(Request $request)
57
    {
58
        $rendererService = $request->get('renderer', 'datatables');
59
        if ($this->container->has($rendererService)) {
60
            if (!($rendererService = $this->container->get($rendererService)) instanceof AbstractRenderer) {
61
                throw new \Exception("$rendererService not instance of Dtc\GridBundle\Grid\Renderer\AbstractRenderer");
62
            }
63
        } else {
64
            $renderer = $this->container->get('dtc_grid.renderer.factory')->create($rendererService);
65
        }
66
        $gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id'));
67
68
        $response = new Response();
69
        $gridSource->bind($request); // Sets limit, offset, sort, filter, etc
70
        $renderer->bind($gridSource); // Sets grid to renderer
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $renderer does not seem to be defined for all execution paths leading up to this point.
Loading history...
71
72
        $fields = $request->get('fields', null);
73
        if ($fields && is_array($fields)) {
74
            $gridSource->selectColumns($fields);
75
        }
76
77
        $content = null;
78
        // If changes to data is kept track using update_time, then we
79
        //   can skip querying for all data.
80
        if ($lastModified = $gridSource->getLastModified()) {
81
            $response->setLastModified($lastModified);
82
        } else {
83
            // generate etag from data
84
            $data = $renderer->getData();
85
            $content = json_encode($data);
86
            $eTag = hash('sha256', $content);
87
            $response->setEtag($eTag);
88
        }
89
90
        $response->setPublic();
91
        if ($response->isNotModified($request)) {
92
            return $response;
93
        } else {
94
            if (!$content) {
95
                $data = $renderer->getData();
96
                $content = json_encode($data);
97
            }
98
99
            $response->headers->set('Content-type', 'application/json');
100
            $response->setContent($content);
101
        }
102
103
        return $response;
104
    }
105
106
    /**
107
     * @return JsonResponse|Response
108
     */
109
    public function show(Request $request)
110
    {
111
        $gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id'));
112
        $id = $request->get('identifier');
113
        $result = $gridSource->find($id);
114
115
        $responseResult = [];
116
        if (!$result) {
117
            return new Response('Not Found', 404);
118
        }
119
        if (is_array($result)) {
120
            foreach ($result as $key => $value) {
121
                $responseResult[CamelCase::fromCamelCase($key)] = $value;
122
            }
123
        } elseif (method_exists($gridSource, 'getClassMetadata')) {
124
            $classMetadata = $gridSource->getClassMetadata();
125
            $fieldNames = $classMetadata->getFieldNames();
126
            foreach ($fieldNames as $fieldName) {
127
                $method = 'get'.ucfirst($fieldName);
128
                if (method_exists($result, $method)) {
129
                    $responseResult[CamelCase::fromCamelCase($fieldName)] = $result->$method();
130
                }
131
            }
132
        }
133
134
        return new JsonResponse($responseResult);
135
    }
136
137
    /**
138
     * @return Response
139
     */
140
    public function delete(Request $request)
141
    {
142
        $gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id'));
143
        $id = $request->get('identifier');
144
        $gridSource->remove($id);
145
        $response = new Response();
146
        $response->setStatusCode(204);
147
148
        return $response;
149
    }
150
}
151