Passed
Push — master ( a6d2f7...8a0b1b )
by Dāvis
02:57
created

NonActionTrait::batchActionDownload()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 1
nop 1
dl 0
loc 42
rs 8.439
c 0
b 0
f 0
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\RedirectResponse;
10
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
11
use Symfony\Component\HttpFoundation\StreamedResponse;
12
use Symfony\Component\Yaml\Dumper;
13
14
trait NonActionTrait
15
{
16
    /**
17
     * Execute a batch download
18
     *
19
     * @param ProxyQueryInterface $query
20
     *
21
     * @return RedirectResponse
22
     */
23
    public function batchActionDownload(ProxyQueryInterface $queryProxy)
24
    {
25
        $flashType = 'success';
26
27
        $dumper = new Dumper(4);
28
29
        $response = new StreamedResponse(function() use ($queryProxy, &$flashType, $dumper) {
30
            try {
31
                /**
32
                 * @var TransUnit $transUnit
33
                 */
34
                foreach ($queryProxy->getQuery()->iterate() as $pos => $object) {
35
                    foreach ($object as $transUnit) {
36
                        $chunkPrefix = $transUnit->getDomain().'__'.$transUnit->getKey().'__'.$transUnit->getId().'__';
37
                        $chunk = [];
38
                        /** @var TranslationInterface $translation */
39
                        foreach ($transUnit->getTranslations() as $translation) {
40
                            $chunk[$chunkPrefix.$translation->getLocale()] = $translation->getContent();
41
                        }
42
                        echo $dumper->dump($chunk, 2);
43
                        flush();
44
                    }
45
                }
46
            } catch (\PDOException $e) {
47
                $flashType = 'error';
48
                flush();
49
            } catch (DBALException $e) {
50
                $flashType = 'error';
51
                flush();
52
            }
53
        });
54
55
        $this->addFlash('sonata_flash_'.$flashType, 'translations.flash_batch_download_'.$flashType);
0 ignored issues
show
Bug introduced by
It seems like addFlash() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

55
        $this->/** @scrutinizer ignore-call */ 
56
               addFlash('sonata_flash_'.$flashType, 'translations.flash_batch_download_'.$flashType);
Loading history...
56
57
        $response->headers->set('Content-Type', 'text/x-yaml');
58
        $response->headers->set('Cache-Control', '');
59
        $response->headers->set('Transfer-Encoding', 'chunked');
60
        $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s'));
61
        $contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'translations.yml');
62
        $response->headers->set('Content-Disposition', $contentDisposition);
63
64
        return $response;
65
    }
66
67
    protected function getManagedLocales()
68
    {
69
        return $this->container->getParameter('lexik_translation.managed_locales');
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist on Sludio\HelperBundle\Lexi...ntroller\NonActionTrait. Did you maybe forget to declare it?
Loading history...
70
    }
71
72
    public function __toString()
73
    {
74
        return 'sludio_helper.lexik.crud.controller';
75
    }
76
}
77