Completed
Push — 1.x ( 534b12...2b6feb )
by Akihito
02:37
created

AppMeta::__construct()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.1953

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 21
ccs 14
cts 16
cp 0.875
rs 6.6746
cc 10
eloc 15
nc 9
nop 3
crap 10.1953

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the BEAR.AppMeta package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\AppMeta;
8
9
use BEAR\AppMeta\Exception\AppNameException;
10
use BEAR\AppMeta\Exception\NotWritableException;
11
use Koriym\Psr4List\Psr4List;
12
13
class AppMeta extends AbstractAppMeta
14
{
15
    /**
16
     * @param string $name    application name    (Vendor\Project)
17
     * @param string $context application context (prod-hal-app)
18
     * @param string $appDir  application directory
19
     */
20 4
    public function __construct($name, $context = 'app', $appDir = null)
21
    {
22 4
        $appModule = $name . '\Module\AppModule';
23 4
        if (! class_exists($appModule)) {
24 1
            throw new AppNameException($name);
25
        }
26 4
        $this->name = $name;
27 4
        $this->appDir = $appDir ? $appDir : dirname(dirname(dirname((new \ReflectionClass($appModule))->getFileName())));
28 4
        $this->tmpDir = $this->appDir . '/var/tmp/' . $context;
29 4
        if (! file_exists($this->tmpDir) && mkdir($this->tmpDir) && ! is_writable($this->tmpDir)) {
30
            throw new NotWritableException($this->tmpDir);
31
        }
32 4
        $this->logDir = $this->appDir . '/var/log/' . $context;
33 4
        if (! file_exists($this->logDir) && mkdir($this->logDir) && ! is_writable($this->logDir)) {
34
            throw new NotWritableException($this->logDir);
35
        }
36 4
        $isDevelop = strpos($context, 'prod') === false;
37 4
        if ($isDevelop) {
38 2
            $this->clearTmpDirectory($this->tmpDir);
39
        }
40 4
    }
41
42
    /**
43
     * @return \Generator
44
     */
45 1
    public function getResourceListGenerator()
46
    {
47 1
        $list = new Psr4List;
48 1
        $resourceListGenerator = $list($this->name . '\Resource', $this->appDir . '/src/Resource');
49
50 1
        return $resourceListGenerator;
51
    }
52
53
    /**
54
     * @param string $dir
55
     */
56 2
    private function clearTmpDirectory($dir)
57
    {
58
        /**
59
         *  A flag for clear once because called many times during the unit testing
60
         */
61 2
        static $done = false;
62
63 2
        if ($done || file_exists($dir . '/.do_not_clear')) {
64 1
            return;
65
        }
66 1
        $unlink = function ($path) use (&$unlink) {
67 1
            foreach (glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
68 1
                is_dir($file) ? $unlink($file) : unlink($file);
69 1
                @rmdir($file);
70
            }
71 1
        };
72 1
        $unlink($dir);
73 1
        $done = true;
74 1
    }
75
}
76