Passed
Push — devel-3.0 ( 67d9a6...e0263f )
by Rubén
04:37
created

ConfigBackupService::packConfigData()   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 1
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\Services\Config;
26
27
use SP\Config\ConfigData;
28
use SP\Http\Json;
29
use SP\Repositories\NoSuchItemException;
30
use SP\Services\Service;
31
use SP\Services\ServiceException;
32
use SP\Util\Util;
33
34
/**
35
 * Class ConfigBackupService
36
 *
37
 * @package SP\Services\Config
38
 */
39
final class ConfigBackupService extends Service
40
{
41
    /**
42
     * @var ConfigService
43
     */
44
    protected $configService;
45
46
    /**
47
     * @param string $configData
48
     *
49
     * @return string
50
     * @throws \SP\Core\Exceptions\SPException
51
     */
52
    public static function configToJson(string $configData): string
53
    {
54
        return Json::getJson(Util::unserialize(ConfigData::class, $configData), JSON_PRETTY_PRINT);
55
    }
56
57
    /**
58
     * @param string $configData
59
     */
60
    public static function configToXml(string $configData)
0 ignored issues
show
Unused Code introduced by
The parameter $configData is not used and could be removed. ( Ignorable by Annotation )

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

60
    public static function configToXml(/** @scrutinizer ignore-unused */ string $configData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        throw new \RuntimeException('Not implemented');
63
    }
64
65
    /**
66
     * Backs up the config data into the database
67
     *
68
     * @param ConfigData $configData
69
     */
70
    public function backup(ConfigData $configData)
71
    {
72
        try {
73
            $this->configService->save('config_backup', $this->packConfigData($configData));
74
            $this->configService->save('config_backup_date', time());
75
        } catch (\Exception $e) {
76
            processException($e);
77
        }
78
    }
79
80
    /**
81
     * @param ConfigData $configData
82
     *
83
     * @return string
84
     */
85
    private function packConfigData(ConfigData $configData)
86
    {
87
        return bin2hex(gzcompress(serialize($configData)));
88
    }
89
90
    /**
91
     * @return ConfigData
92
     * @throws ServiceException
93
     * @throws \DI\DependencyException
94
     * @throws \DI\NotFoundException
95
     * @throws \SP\Storage\File\FileException
96
     */
97
    public function restore()
98
    {
99
        return $this->config->saveConfig(Util::unserialize(
0 ignored issues
show
Bug introduced by
It seems like SP\Util\Util::unserializ...ss, $this->getBackup()) can also be of type string; however, parameter $configData of SP\Config\Config::saveConfig() does only seem to accept SP\Config\ConfigData, maybe add an additional type check? ( Ignorable by Annotation )

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

99
        return $this->config->saveConfig(/** @scrutinizer ignore-type */ Util::unserialize(
Loading history...
100
            ConfigData::class,
101
            $this->getBackup())
102
        )->getConfigData();
103
    }
104
105
    /**
106
     * @return ConfigData
107
     * @throws ServiceException
108
     */
109
    public function getBackup(): string
110
    {
111
        try {
112
            $data = $this->configService->getByParam('config_backup');
113
114
            if ($data === null) {
115
                throw new ServiceException(__u('No es posible restaurar la configuración'));
116
            }
117
118
            return gzuncompress(hex2bin($data));
0 ignored issues
show
Bug Best Practice introduced by
The expression return gzuncompress(hex2bin($data)) returns the type string which is incompatible with the documented return type SP\Config\ConfigData.
Loading history...
119
        } catch (NoSuchItemException $e) {
120
            processException($e);
121
122
            throw new ServiceException(__u('No es posible restaurar la configuración'));
123
        }
124
    }
125
126
    protected function initialize()
127
    {
128
        $this->configService = $this->dic->get(ConfigService::class);
129
    }
130
}