Completed
Push — 1.x ( f4f2df...fa8652 )
by Akihito
11s
created

AppMeta::__construct()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.2363

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 21
ccs 14
cts 16
cp 0.875
rs 6.4715
cc 11
eloc 15
nc 13
nop 3
crap 11.2363

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
12
class AppMeta extends AbstractAppMeta
13
{
14
    /**
15
     * @param string $name    application name    (Vendor\Project)
16
     * @param string $context application context (prod-hal-app)
17
     * @param string $appDir  application directory
18
     */
19 4
    public function __construct($name, $context = 'app', $appDir = null)
20
    {
21 4
        $appModule = $name . '\Module\AppModule';
22 4
        if (! class_exists($appModule)) {
23 1
            throw new AppNameException($name);
24
        }
25 4
        $this->name = $name;
26 4
        $this->appDir = $appDir ? $appDir : dirname(dirname(dirname((new \ReflectionClass($appModule))->getFileName())));
27 4
        $this->tmpDir = $this->appDir . '/var/tmp/' . $context;
28 4
        if (! file_exists($this->tmpDir) && mkdir($this->tmpDir) && ! is_writable($this->tmpDir)) {
29
            throw new NotWritableException($this->tmpDir);
30
        }
31 4
        $this->logDir = $this->appDir . '/var/log/' . $context;
32 4
        if (! file_exists($this->logDir) && mkdir($this->logDir) && ! is_writable($this->logDir)) {
33
            throw new NotWritableException($this->logDir);
34
        }
35 4
        $isCacheable = is_int(strpos($context, 'prod-')) || is_int(strpos($context, 'stage-'));
36 4
        if (! $isCacheable) {
37 1
            $this->clearTmpDirectory($this->tmpDir);
38
        }
39 4
    }
40
41
    /**
42
     * @param string $dir
43
     */
44 1
    private function clearTmpDirectory($dir)
45
    {
46
        /**
47
         * A flag for not deleting tmp directories many times with single request
48
         */
49 1
        static $cleanUpFlg = [];
50
51 1
        if (in_array($dir, $cleanUpFlg, true) || file_exists($dir . '/.do_not_clear')) {
52
            return;
53
        }
54 1
        $unlink = function ($path) use (&$unlink) {
55 1
            foreach (glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
56 1
                is_dir($file) ? $unlink($file) : unlink($file);
57 1
                @rmdir($file);
58
            }
59 1
        };
60 1
        $unlink($dir);
61 1
        $cleanUpFlg[] = $dir;
62 1
    }
63
}
64