Completed
Push — app-factory ( 09c0dc )
by Akihito
06:00
created

App   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package;
8
9
use BEAR\AppMeta\Meta;
10
use BEAR\Sunday\Extension\Application\AbstractApp;
11
use BEAR\Sunday\Extension\Application\AppInterface;
12
use Doctrine\Common\Cache\Cache;
13
14
final class App
15
{
16
    /**
17
     * @param string $name     application name      'MyVendor\MyProject'
18
     * @param string $contexts application context   'prod-app'
19
     * @param string $envFile  .env file path        '/path/to/project/.env'
20
     * @param string $appDir   application directory '/path/to/project'
21
     */
22 8
    public function __invoke(
23
        string $name,
24
        string $contexts,
25
        string $envFile = '',
26
        string $appDir = ''
27
    ) : AbstractApp {
28 8
        $appMeta = new Meta($name, $contexts, $appDir);
29 8
        $cacheNs = (string) filemtime($appMeta->appDir . '/src');
30 8
        $injector = new AppInjector($appMeta->name, $contexts, $appMeta, $cacheNs, $envFile);
31 8
        $cache = $injector->getCachedInstance(Cache::class);
32 7
        $appId = $appMeta->name . $contexts . $cacheNs;
33 7
        $app = $cache->fetch($appId);
34 7
        if ($app instanceof AbstractApp) {
35 1
            return $app;
36
        }
37 7
        $injector->clear();
38 7
        $app = $injector->getCachedInstance(AppInterface::class);
39 7
        $cache->save($appId, $app);
40
41 7
        return $app;
42
    }
43
}
44