Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

AdminExporter::setTokenStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Admin;
13
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Symfony\Bundle\FrameworkBundle\Routing\Router;
17
use Blast\Bundle\CoreBundle\Exporter\Exporter;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
20
class AdminExporter
21
{
22
    /**
23
     * @var TokenStorageInterface
24
     */
25
    protected $tokenStorage;
26
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    protected $translator;
31
32
    /**
33
     * @var \Twig_Environment
34
     */
35
    protected $twig;
36
37
    /**
38
     * @var Router
39
     */
40
    protected $router; /**
41
     * @var Exporter service from the exporter bundle
42
     */
43
    private $exporter;
44
45
    /**
46
     * @param Exporter will be used to get global settings
47
     */
48
    public function __construct(Exporter $exporter)
49
    {
50
        $this->exporter = $exporter;
51
    }
52
53
    /**
54
     * Queries an admin for its default export formats, and falls back on global settings.
55
     *
56
     * @param AdminInterface $admin the current admin object
57
     *
58
     * @return string[] an array of formats
59
     */
60
    public function getAvailableFormats(AdminInterface $admin)
61
    {
62
        $adminExportFormats = $admin->getExportFormats();
63
64
        // NEXT_MAJOR : compare with null
65
        if ($adminExportFormats != array('json', 'xml', 'csv', 'xls')) {
66
            return $adminExportFormats;
67
        }
68
69
        return $this->exporter->getAvailableFormats();
70
    }
71
72
    /**
73
     * Builds an export filename from the class associated with the provided admin,
74
     * the current date, and the provided format.
75
     *
76
     * @param AdminInterface $admin  the current admin object
77
     * @param string         $format the format of the export file
78
     */
79
    public function getExportFilename(AdminInterface $admin, $format)
80
    {
81
        $class = $admin->getClass();
82
83
        return sprintf(
84
            'export_%s_%s.%s',
85
            strtolower(substr($class, strripos($class, '\\') + 1)),
86
            date('Y_m_d_H_i_s', strtotime('now')),
87
            $format
88
        );
89
    }
90
91
    /**
92
     * setTokenStorage.
93
     *
94
     * @param $tokenStorage     TokenStorageInterface
95
     */
96
    public function setTokenStorage(TokenStorageInterface $tokenStorage)
97
    {
98
        $this->tokenStorage = $tokenStorage;
99
100
        return $this;
101
    }
102
103
    /**
104
     * setTranslator.
105
     *
106
     * @param $translator     TokenStorageInterface
107
     */
108
    public function setTranslator(TranslatorInterface $translator)
109
    {
110
        $this->translator = $translator;
111
112
        return $this;
113
    }
114
115
    /**
116
     * setTwig.
117
     *
118
     * @param $twig     \Twig_Environment
119
     */
120
    public function setTwig(\Twig_Environment $twig)
121
    {
122
        $this->twig = $twig;
123
124
        return $this;
125
    }
126
127
    /**
128
     * setRouter.
129
     *
130
     * @param $router     Router
131
     */
132
    public function setRouter(Router $router)
133
    {
134
        $this->router = $router;
135
136
        return $this;
137
    }
138
}
139