VersionAPI.php 2.65 KB
Newer Older
Karamel's avatar
Karamel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
//    Pasteque API
//
//    Copyright (C) 2012-2017 Pasteque contributors
//
//    This file is part of Pasteque.
//
//    Pasteque is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Pasteque is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Pasteque.  If not, see <http://www.gnu.org/licenses/>.

21
namespace Pasteque\Server\CommonAPI;
Karamel's avatar
Karamel committed
22

23
use \Pasteque\Server\Model\GenericModel;
24
use \Pasteque\Server\Model\Option;
25

26
class VersionAPI implements \Pasteque\Server\API\API {
Karamel's avatar
Karamel committed
27

Karamel's avatar
Karamel committed
28
29
    const VERSION = '8.5';
    const REVISION = 4;
Karamel's avatar
Karamel committed
30
31
    const LEVEL = 8;

32
33
34
35
36
37
    protected $dao;

    public function __construct($dao) {
        $this->dao = $dao;
    }

Karamel's avatar
Karamel committed
38
    public static function fromApp($app) {
39
        return new VersionAPI($app->getDao());
Karamel's avatar
Karamel committed
40
41
42
    }

    public function get() {
43
44
45
46
47
48
49
50
51
        $version = $this->dao->read(Option::class, 'dblevel');
        if ($version === null) {
            // Database is not initialized.
            throw new \UnexpectedValueException('dblevel option does not exist. The database is probably not initialized.');
        }
        $dbLevel = intVal($version->getContent());
        if ($dbLevel === 0) {
            throw new \UnexpectedValueException(sprintf('dblevel %s option is invalid.', $version->getContent()));
        }
52
53
        $version = new GenericModel();
        $version->set('version', static::VERSION);
54
        $version->set('level', $dbLevel);
Karamel's avatar
Karamel committed
55
        $version->set('revision', static::REVISION);
Karamel's avatar
Karamel committed
56
        return $version;
Karamel's avatar
Karamel committed
57
58
    }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    /** Update the database level. It should be used only by update scripts
     * and not available outside the server.
     * If the dblevel option does not exists, it is created. */
    public function setLevel($newLevel) {
        if (is_int($newLevel)) {
            $newLevel = strval($newLevel);
        }
        if (empty($newLevel)) {
            throw new \InvalidArgumentException('Invalid level value.');
        }
        $version = $this->dao->read(Option::class, 'dblevel');
        if ($version === null) {
            $version = new Option();
            $version->setName('dblevel');
            $version->setSystem(true);
        }
        $version->setContent($newLevel);
        $this->dao->write($version);
        $this->dao->commit();
    }
Karamel's avatar
Karamel committed
79
}