IndexController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 56
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A categoryAction() 0 3 1
A productAction() 0 3 1
A getResponse() 0 10 1
A getLocale() 0 7 2
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Econda\Communication\Controller;
9
10
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\StreamedResponse;
13
14
/**
15
 * @method \SprykerEco\Zed\Econda\Business\EcondaFacadeInterface getFacade()
16
 * @method \SprykerEco\Zed\Econda\Persistence\EcondaQueryContainerInterface getQueryContainer()
17
 */
18
class IndexController extends AbstractController
19
{
20
    protected const CATEGORIES = 'categories';
21
    protected const PRODUCTS = 'products';
22
23
    /**
24
     * @param \Symfony\Component\HttpFoundation\Request $request
25
     *
26
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
27
     */
28
    public function categoryAction(Request $request): StreamedResponse
29
    {
30
        return $this->getResponse($request, static::CATEGORIES);
31
    }
32
33
    /**
34
     * @param \Symfony\Component\HttpFoundation\Request $request
35
     *
36
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
37
     */
38
    public function productAction(Request $request): StreamedResponse
39
    {
40
        return $this->getResponse($request, static::PRODUCTS);
41
    }
42
43
    /**
44
     * @param \Symfony\Component\HttpFoundation\Request $request
45
     * @param string $type
46
     *
47
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
48
     */
49
    private function getResponse(Request $request, $type): StreamedResponse
50
    {
51
        $fileContent = $this->getFacade()->getFileContent($type, $this->getLocale($request));
52
53
        return $this->streamedResponse(
54
            function () use ($fileContent) {
55
                echo $fileContent;
56
            },
57
            200,
58
            ['Content-type' => 'text/csv', 'Content-Disposition' => sprintf("attachment; filename='%s.csv'", $type)]
59
        );
60
    }
61
62
    /**
63
     * @param \Symfony\Component\HttpFoundation\Request $request
64
     *
65
     * @return string
66
     */
67
    protected function getLocale(Request $request): string
68
    {
69
        if ($request->query->get('locale') !== null) {
70
            return $request->query->get('locale');
71
        }
72
73
        return $this->getApplication()['locale'];
74
    }
75
}
76