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

Meta   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

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