Completed
Push — master ( 27a2ba...782061 )
by
unknown
13s
created

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