NonActionTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getManagedLocales() 0 3 1
B batchActionDownload() 0 46 6
1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Controller;
4
5
use Doctrine\DBAL\DBALException;
6
use Lexik\Bundle\TranslationBundle\Entity\TransUnit;
7
use Lexik\Bundle\TranslationBundle\Manager\TranslationInterface;
8
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
9
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
use Symfony\Component\Yaml\Dumper;
12
13
trait NonActionTrait
14
{
15
    /**
16
     * Execute a batch download
17
     *
18
     * @param ProxyQueryInterface $queryProxy
19
     *
20
     * @return StreamedResponse
21
     * @internal param ProxyQueryInterface $query
22
     * @throws \InvalidArgumentException
23
     */
24
    public function batchActionDownload(ProxyQueryInterface $queryProxy)
25
    {
26
        $flashType = 'success';
27
28
        $dumper = new Dumper();
29
        $dumper->setIndentation(4);
0 ignored issues
show
Bug introduced by
The method setIndentation() does not exist on Symfony\Component\Yaml\Dumper. ( Ignorable by Annotation )

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

29
        $dumper->/** @scrutinizer ignore-call */ 
30
                 setIndentation(4);

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...
30
31
        $response = new StreamedResponse(function() use ($queryProxy, &$flashType, $dumper) {
32
            try {
33
                /**
34
                 * @var TransUnit $transUnit
35
                 */
36
                $iterates = $queryProxy->getQuery()->iterate();
37
                /** @var $iterates array */
38
                foreach ($iterates as $pos => $object) {
39
                    /** @var $object array */
40
                    foreach ($object as $transUnit) {
41
                        $chunkPrefix = $transUnit->getDomain().'__'.$transUnit->getKey().'__'.$transUnit->getId().'__';
42
                        $chunk = [];
43
                        /** @var TranslationInterface $translation */
44
                        foreach ($transUnit->getTranslations() as $translation) {
45
                            $chunk[$chunkPrefix.$translation->getLocale()] = $translation->getContent();
46
                        }
47
                        echo $dumper->dump($chunk, 2);
48
                        flush();
49
                    }
50
                }
51
            } catch (\PDOException $e) {
52
                $flashType = 'error';
53
                flush();
54
            } catch (DBALException $e) {
55
                $flashType = 'error';
56
                flush();
57
            }
58
        });
59
60
        $this->addFlash('sonata_flash_'.$flashType, 'translations.flash_batch_download_'.$flashType);
61
62
        $response->headers->set('Content-Type', 'text/x-yaml');
63
        $response->headers->set('Cache-Control', '');
64
        $response->headers->set('Transfer-Encoding', 'chunked');
65
        $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s'));
66
        $contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'translations.yml');
67
        $response->headers->set('Content-Disposition', $contentDisposition);
68
69
        return $response;
70
    }
71
72
    abstract protected function addFlash($type, $message);
73
74
    public function __toString()
75
    {
76
        return 'sludio_helper.lexik.crud.controller';
77
    }
78
79
    protected function getManagedLocales()
80
    {
81
        return $this->container->getParameter('lexik_translation.managed_locales');
82
    }
83
}
84