Meta   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 12
c 3
b 0
f 1
dl 0
loc 30
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAppDir() 0 8 2
B __construct() 0 12 8
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