GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ReportController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 57
c 4
b 2
f 0
dl 0
loc 128
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A slugify() 0 3 1
A createJsonResponse() 0 16 3
A embedAction() 0 5 1
A createCsvResponse() 0 6 1
A getReportDataFetcher() 0 6 1
A exportAction() 0 31 3
A renderAction() 0 24 2
A getReportDataFetcherConfigurationForm() 0 13 2
A getReportRenderer() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReportPlugin\Controller;
6
7
use Odiseo\SyliusReportPlugin\DataFetcher\Data;
8
use Odiseo\SyliusReportPlugin\DataFetcher\DelegatingDataFetcherInterface;
9
use Odiseo\SyliusReportPlugin\Model\ReportInterface;
10
use Odiseo\SyliusReportPlugin\Renderer\DelegatingRendererInterface;
11
use Odiseo\SyliusReportPlugin\Response\CsvResponse;
12
use Odiseo\SyliusReportPlugin\Form\Type\ReportDataFetcherConfigurationType;
13
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
14
use Sylius\Component\Resource\ResourceActions;
15
use Symfony\Component\Form\FormFactoryInterface;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 * @author Łukasz Chruściel <[email protected]>
24
 * @author Fernando Caraballo Ortiz <[email protected]>
25
 * @author Diego D'amico <[email protected]>
26
 */
27
class ReportController extends ResourceController
28
{
29
    public function renderAction(Request $request): Response
30
    {
31
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
32
33
        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
34
35
        /** @var ReportInterface $report */
36
        $report = $this->findOr404($configuration);
37
        $reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request);
38
        $report = $reportDataFetcherConfigurationForm->getData();
39
40
        $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $report);
41
42
        if ($configuration->isHtmlRequest()) {
43
            return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [
44
                'configuration' => $configuration,
45
                'metadata' => $this->metadata,
46
                'resource' => $report,
47
                'form' => $reportDataFetcherConfigurationForm->createView(),
48
                $this->metadata->getName() => $report,
49
            ]);
50
        }
51
52
        return $this->createRestView($configuration, $report);
53
    }
54
55
    public function exportAction(Request $request): Response
56
    {
57
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
58
59
        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
60
61
        /** @var ReportInterface $report */
62
        $report = $this->findOr404($configuration);
63
        $reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request);
64
        $report = $reportDataFetcherConfigurationForm->getData();
65
66
        $dataFetcherConfiguration = $report->getDataFetcherConfiguration();
67
68
        /** @var Data $data */
69
        $data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration);
70
71
        $filename = $this->slugify($report->getName());
72
73
        $format = $request->get('_format');
74
        $response = null;
75
        switch ($format) {
76
            case 'json':
77
                $response = $this->createJsonResponse($filename, $data);
78
                break;
79
            case 'csv':
80
            default:
81
                $response = $this->createCsvResponse($filename, $data);
82
                break;
83
        }
84
85
        return $response;
86
    }
87
88
    public function embedAction(ReportInterface $report, array $dataFetcherConfiguration = []): Response
89
    {
90
        $data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration);
91
92
        return new Response($this->getReportRenderer()->render($report, $data));
93
    }
94
95
    private function getReportRenderer(): DelegatingRendererInterface
96
    {
97
        /** @var DelegatingRendererInterface $renderer */
98
        $renderer = $this->container->get('odiseo_sylius_report.renderer');
99
100
        return $renderer;
101
    }
102
103
    private function getReportDataFetcher(): DelegatingDataFetcherInterface
104
    {
105
        /** @var DelegatingDataFetcherInterface $dataFetcher */
106
        $dataFetcher = $this->container->get('odiseo_sylius_report.data_fetcher');
107
108
        return $dataFetcher;
109
    }
110
111
    protected function createJsonResponse(string $filename, Data $data): Response
112
    {
113
        $responseData = [];
114
        foreach ($data->getData() as $key => $value) {
115
            $responseData[] = [$key, $value];
116
        }
117
118
        $response = JsonResponse::create($responseData);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...\JsonResponse::create() has been deprecated: since Symfony 5.1, use __construct() instead. ( Ignorable by Annotation )

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

118
        $response = /** @scrutinizer ignore-deprecated */ JsonResponse::create($responseData);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
120
        $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename.'.json'));
121
122
        if (!$response->headers->has('Content-Type')) {
123
            $response->headers->set('Content-Type', 'text/json');
124
        }
125
126
        return $response;
127
    }
128
129
    protected function createCsvResponse(string $filename, Data $data): Response
130
    {
131
        $response = new CsvResponse($data);
132
        $response->setFilename($filename.'.csv');
133
134
        return $response;
135
    }
136
137
    protected function getReportDataFetcherConfigurationForm(ReportInterface $report, Request $request): FormInterface
138
    {
139
        /** @var FormFactoryInterface $formFactory */
140
        $formFactory = $this->container->get('form.factory');
141
142
        /** @var FormInterface $configurationForm */
143
        $configurationForm = $formFactory->create(ReportDataFetcherConfigurationType::class, $report);
144
145
        if ($request->query->has($configurationForm->getName())) {
146
            $configurationForm->handleRequest($request);
147
        }
148
149
        return $configurationForm;
150
    }
151
152
    private function slugify(string $string): string
153
    {
154
        return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
155
    }
156
}
157