Completed
Push — master ( bd7133...e05a5e )
by BruceScrutinizer
07:36
created

NamespaceProtectorProcessorFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 1
A createCacheObject() 0 9 2
1
<?php
2
namespace NamespaceProtector;
3
4
use NamespaceProtector\Analyser;
5
use Psr\SimpleCache\CacheInterface;
6
use NamespaceProtector\Config\Config;
7
use NamespaceProtector\Cache\NullCache;
8
use NamespaceProtector\Parser\PhpFileParser;
9
use NamespaceProtector\Scanner\ComposerJson;
10
use NamespaceProtector\Cache\SimpleFileCache;
11
use NamespaceProtector\Common\FileSystemPath;
12
use NamespaceProtector\EnvironmentDataLoader;
13
use NamespaceProtector\Scanner\FileSystemScanner;
14
use NamespaceProtector\NamespaceProtectorProcessor;
15
16
class NamespaceProtectorProcessorFactory
17
{
18
    private const NAMESPACE_PROTECTOR_CACHE = 'namespace-protector-cache';
19
20
    public function create(Config $config): NamespaceProtectorProcessor
21
    {
22
        $composerJson = new ComposerJson($config->getPathComposerJson());
23
        $fileSystem = new FileSystemScanner([$config->getStartPath()]);
24
        $metaDataLoader = new EnvironmentDataLoader($composerJson);
25
        $cacheClass = $this->createCacheObject($config);
26
        $analyser = new Analyser(new PhpFileParser($config, $metaDataLoader, $cacheClass));
27
28
        $namespaceProtectorProcessor = new NamespaceProtectorProcessor(
29
            $composerJson,
30
            $fileSystem,
31
            $analyser,
32
            $metaDataLoader
33
        );
34
        
35
        return $namespaceProtectorProcessor;
36
    }
37
38
    private function createCacheObject(Config $config): CacheInterface
39
    {
40
        if ($config->enabledCache()) {
41
            $directory = \sys_get_temp_dir().self::NAMESPACE_PROTECTOR_CACHE;
42
43
            return new SimpleFileCache(new FileSystemPath($directory, true));
44
        }
45
        
46
        return new NullCache();
47
    }
48
}
49