Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.61% covered (warning)
82.61%
19 / 23
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
RuntimeEnvironment
82.61% covered (warning)
82.61%
19 / 23
85.71% covered (warning)
85.71%
6 / 7
9.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 nextcloudVersion
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 phpVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 osFamily
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 machine
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 databasePlatform
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 databaseServerVersion
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\UsageStatistics\Source;
10
11use OCP\IDBConnection;
12use OCP\ServerVersion;
13
14/**
15 * Raw facts about the server, the PHP runtime and the database. Callers reduce
16 * them to coarse values before reporting.
17 */
18class RuntimeEnvironment {
19    public function __construct(
20        private IDBConnection $db,
21        private ServerVersion $serverVersion,
22    ) {
23    }
24
25    /** major.minor.patch, without build or channel. */
26    public function nextcloudVersion(): string {
27        return sprintf(
28            '%d.%d.%d',
29            $this->serverVersion->getMajorVersion(),
30            $this->serverVersion->getMinorVersion(),
31            $this->serverVersion->getPatchVersion(),
32        );
33    }
34
35    public function phpVersion(): string {
36        return PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
37    }
38
39    public function osFamily(): string {
40        return PHP_OS_FAMILY;
41    }
42
43    /** Machine hardware name only; never the hostname or the kernel string. */
44    public function machine(): string {
45        return php_uname('m');
46    }
47
48    /**
49     * @return IDBConnection::PLATFORM_*
50     */
51    public function databasePlatform(): string {
52        return $this->db->getDatabaseProvider(true);
53    }
54
55    /**
56     * The server version string as reported by the database, or null when the
57     * platform offers no portable way to read it.
58     */
59    public function databaseServerVersion(): ?string {
60        $query = match ($this->databasePlatform()) {
61            IDBConnection::PLATFORM_MYSQL, IDBConnection::PLATFORM_MARIADB => 'SELECT VERSION()',
62            IDBConnection::PLATFORM_POSTGRES => 'SHOW server_version',
63            IDBConnection::PLATFORM_SQLITE => 'SELECT sqlite_version()',
64            default => null,
65        };
66        if ($query === null) {
67            return null;
68        }
69        $result = $this->db->executeQuery($query);
70        $version = $result->fetchOne();
71        $result->closeCursor();
72        return is_string($version) ? $version : null;
73    }
74}