Completed
Push — 1.x ( 17417a...1999aa )
by Akihito
27s queued 11s
created

Meta::getAppDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
10
class Meta extends AbstractAppMeta
11
{
12
    /**
13
     * @param string $name    application name      (Vendor\Project)
14
     * @param string $context application context   (prod-hal-app)
15
     * @param string $appDir  application directory
16
     */
17
    public function __construct(string $name, string $context = 'app', string $appDir = '')
18
    {
19
        $this->name = $name;
20
        $this->appDir = $appDir ?: $this->getAppDir($name);
21
        $this->tmpDir = $this->appDir . '/var/tmp/' . $context;
22
        if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, 0777, true) && ! is_dir($this->tmpDir)) {
23
            throw new NotWritableException($this->tmpDir);
24
        }
25
        $this->logDir = $this->appDir . '/var/log/' . $context;
26
        if (! file_exists($this->logDir) && ! @mkdir($this->logDir, 0777, true) && ! is_dir($this->logDir)) {
27
            throw new NotWritableException($this->logDir);
28
        }
29
    }
30
31
    private function getAppDir(string $name) : string
32
    {
33
        $module = $name . '\Module\AppModule';
34
        if (! class_exists($module)) {
35
            throw new AppNameException($name);
36
        }
37
38
        return dirname((string) (new \ReflectionClass($module))->getFileName(), 3);
39
    }
40
}
41