Passed
Push — main ( 1e2d69...a54930 )
by Sílvio
02:38
created

EnvHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRootPath() 0 21 6
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Composer\InstalledVersions;
6
7
class EnvHelper {
8
9
    /**
10
     * @return string
11
     * @throws \RuntimeException se o arquivo .env não for encontrado
12
     */
13
    public static function getRootPath()
14
    {
15
        // Tenta obter a raiz do projeto via Composer, se disponível
16
        if (class_exists(InstalledVersions::class)) {
17
            $rootPackage = InstalledVersions::getRootPackage();
18
            if (!empty($rootPackage['install_path'])) {
19
                return rtrim($rootPackage['install_path'], DIRECTORY_SEPARATOR);
20
            }
21
        }
22
23
        // Fallback: sobe os diretórios a partir do __DIR__ procurando o .env
24
        $baseDir = __DIR__;
25
        while (!file_exists($baseDir . DIRECTORY_SEPARATOR . '.env') && $baseDir !== dirname($baseDir)) {
26
            $baseDir = dirname($baseDir);
27
        }
28
29
        if (!file_exists($baseDir . DIRECTORY_SEPARATOR . '.env')) {
30
            throw new \RuntimeException('<CacheerPHP>: Arquivo .env não encontrado na raiz do projeto.');
31
        }
32
33
        return rtrim($baseDir, DIRECTORY_SEPARATOR);
34
    }
35
}
36