Meta::getAppDir()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\AppMeta;
6
7
use BEAR\AppMeta\Exception\AppNameException;
8
use BEAR\AppMeta\Exception\NotWritableException;
9
use ReflectionClass;
10
11
use function class_exists;
12
use function dirname;
13
use function file_exists;
14
use function is_dir;
15
use function mkdir;
16
17
use const DIRECTORY_SEPARATOR;
18
19
final class Meta extends AbstractAppMeta
20
{
21
    /**
22
     * @param string $name    application name      (Vendor\Project)
23
     * @param string $context application context   (prod-hal-app)
24
     * @param string $appDir  application directory
25
     */
26
    public function __construct(string $name, string $context = 'app', string $appDir = '')
27
    {
28
        $this->name = $name;
29
        $this->appDir = $appDir ?: $this->getAppDir($name);
30
        $this->tmpDir = $this->appDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $context;
31
        if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, 0777, true) && ! is_dir($this->tmpDir)) {
32
            throw new NotWritableException($this->tmpDir);
33
        }
34
35
        $this->logDir = $this->appDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $context;
36
        if (! file_exists($this->logDir) && ! @mkdir($this->logDir, 0777, true) && ! is_dir($this->logDir)) {
37
            throw new NotWritableException($this->logDir); // @codeCoverageIgnore
38
        }
39
    }
40
41
    private function getAppDir(string $name): string
42
    {
43
        $module = $name . '\Module\AppModule';
44
        if (! class_exists($module)) {
45
            throw new AppNameException($name);
46
        }
47
48
        return dirname((string) (new ReflectionClass($module))->getFileName(), 3);
49
    }
50
}
51