VersionAPI.php 2.56 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
21
22
<?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/>.

namespace Pasteque\Server\API;

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

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

Karamel's avatar
Karamel committed
28
    const VERSION = '8.0-alpha5';
Karamel's avatar
Karamel committed
29
30
    const LEVEL = 8;

31
32
33
34
35
36
    protected $dao;

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

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

    public function get() {
42
43
44
45
46
47
48
49
50
        $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()));
        }
51
52
        $version = new GenericModel();
        $version->set('version', static::VERSION);
53
        $version->set('level', $dbLevel);
Karamel's avatar
Karamel committed
54
        return $version;
Karamel's avatar
Karamel committed
55
56
    }

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    /** 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
77
}