Completed
Push — 1.x ( 145627...57c1df )
by Akihito
9s
created

AppMeta::__construct()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
rs 8.2222
cc 7
eloc 13
nc 7
nop 3
crap 7.0178
1
<?php
2
/**
3
 * This file is part of the BEAR.Sunday 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';
33 4
        $isDevelop = strpos($context, 'prod') === false;
34 4
        if ($isDevelop) {
35 2
            $this->clearTmpDirectory($this->tmpDir);
36
        }
37 4
    }
38
39
    /**
40
     * @return \Generator
41
     */
42 1
    public function getResourceListGenerator()
43
    {
44 1
        $list = new Psr4List;
45 1
        $resourceListGenerator = $list($this->name . '\Resource', $this->appDir . '/src/Resource');
46
47 1
        return $resourceListGenerator;
48
    }
49
50
    /**
51
     * @param string $dir
52
     */
53 2
    private function clearTmpDirectory($dir)
54
    {
55
        /**
56
         *  A flag for clear once because called many times during the unit testing
57
         */
58 2
        static $done = false;
59
60 2
        if ($done) {
61 1
            return;
62
        }
63 1
        $unlink = function ($path) use (&$unlink) {
64 1
            foreach (glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
65 1
                is_dir($file) ? $unlink($file) : unlink($file);
66 1
                @rmdir($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
67
            }
68 1
        };
69 1
        $unlink($dir);
70 1
        $done = true;
71 1
    }
72
}
73