landrok/activitypub

View on GitHub
src/ActivityPhp/Version.php

Summary

Maintainability
A
0 mins
Test Coverage
D
68%
<?php

declare(strict_types=1);

/*
 * This file is part of the ActivityPhp package.
 *
 * Copyright (c) landrok at github.com/landrok
 *
 * For the full copyright and license information, please see
 * <https://github.com/landrok/activitypub/blob/master/LICENSE>.
 */

namespace ActivityPhp;

use Exception;

/**
 * \ActivityPhp\Version handles current version of this package
 */
abstract class Version
{
    /**
     * Get ActivityPhp version
     */
    public static function getVersion(): string
    {
        $filename = dirname(__DIR__, 2) . '/composer.json';

        self::checkFile($filename);

        $composer = json_decode(
            file_get_contents($filename)
        );

        if (json_last_error() === JSON_ERROR_NONE) {
            if (isset($composer->version) && is_string($composer->version)) {
                return $composer->version;
            }
        }

        return 'Undefined';
    }

    /**
     * get root namespace
     */
    public static function getRootNamespace(): string
    {
        return __NAMESPACE__;
    }

    /**
     * Check that given filename is a readable file
     *
     * @throws \Exception if filename is not a file
     *                 or if file is not readable
     */
    public static function checkFile(string $filename): void
    {
        // Must be readable
        if (! is_readable($filename)) {
            throw new Exception(
                "FILE_ERROR Filename '{$filename}' is not readable"
            );
        }

        // Must be a file
        if (! is_file($filename)) {
            throw new Exception(
                "FILE_ERROR Filename '{$filename}' must be a file"
            );
        }
    }
}