Passed
Push — devel-3.0 ( 617c3b...e28ec2 )
by Rubén
04:01
created

ConfigController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Api\Controllers;
26
27
use SP\Core\Acl\ActionsInterface;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\Modules\Api\Controllers\Help\ConfigHelp;
31
use SP\Services\Api\ApiResponse;
32
use SP\Services\Backup\FileBackupService;
33
use SP\Services\Export\XmlExportService;
34
35
/**
36
 * Class ConfigController
37
 *
38
 * @package SP\Modules\Api\Controllers
39
 */
40
final class ConfigController extends ControllerBase
41
{
42
    /**
43
     * backupAction
44
     */
45
    public function backupAction()
46
    {
47
        try {
48
            $this->setupApi(ActionsInterface::CONFIG_BACKUP);
49
50
            $path = $this->apiService->getParamString('path', false, BACKUP_PATH);
51
52
            $this->dic->get(FileBackupService::class)
53
                ->doBackup($path);
54
55
            $this->eventDispatcher->notifyEvent('run.backup.end',
56
                new Event($this, EventMessage::factory()
57
                    ->addDescription(__u('Copia de la aplicación y base de datos realizada correctamente'))
58
                    ->addDetail(__u('Ruta'), $path))
59
            );
60
61
            $this->returnResponse(new ApiResponse(__u('Proceso de backup finalizado')));
62
        } catch (\Exception $e) {
63
            processException($e);
64
65
            $this->returnResponseException($e);
66
        }
67
    }
68
69
    /**
70
     * exportAction
71
     */
72
    public function exportAction()
73
    {
74
        try {
75
            $this->setupApi(ActionsInterface::CONFIG_EXPORT);
76
77
            $password = $this->apiService->getParamString('password');
78
            $path = $this->apiService->getParamString('path', false, BACKUP_PATH);
79
80
            $this->eventDispatcher->notifyEvent('run.export.start',
81
                new Event($this, EventMessage::factory()
82
                    ->addDescription(__u('Exportación de sysPass en XML'))
83
                    ->addDetail(__u('Ruta'), $path))
84
            );
85
86
            $this->dic->get(XmlExportService::class)
87
                ->doExport($path, $password);
88
89
            $this->eventDispatcher->notifyEvent('run.export.end',
90
                new Event($this, EventMessage::factory()
91
                    ->addDescription(__u('Proceso de exportación finalizado')))
92
            );
93
94
            $this->returnResponse(new ApiResponse(__u('Proceso de exportación finalizado')));
95
        } catch (\Exception $e) {
96
            processException($e);
97
98
            $this->returnResponseException($e);
99
        }
100
    }
101
102
    /**
103
     * @throws \SP\Core\Exceptions\InvalidClassException
104
     */
105
    protected function initialize()
106
    {
107
        $this->apiService->setHelpClass(ConfigHelp::class);
108
    }
109
}