Completed
Push — travis ( 254a42 )
by Akihito
02:20
created

ProdModule::configure()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 15
ccs 5
cts 9
cp 0.5556
rs 9.2
cc 4
eloc 8
nc 3
nop 0
crap 5.4042
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\Context;
8
9
use BEAR\Package\Context\Provider\FilesystemCacheProvider;
10
use BEAR\RepositoryModule\Annotation\Storage;
11
use BEAR\Resource\Annotation\LogicCache;
12
use Doctrine\Common\Annotations\AnnotationReader;
13
use Doctrine\Common\Annotations\CachedReader;
14
use Doctrine\Common\Annotations\Reader;
15
use Doctrine\Common\Cache\ApcCache;
16
use Doctrine\Common\Cache\ApcuCache;
17
use Doctrine\Common\Cache\Cache;
18
use Doctrine\Common\Cache\CacheProvider;
19
use Doctrine\Common\Cache\FileCache;
20
use Doctrine\Common\Cache\FilesystemCache;
21
use Ray\Di\AbstractModule;
22
use Ray\Di\Scope;
23
24
class ProdModule extends AbstractModule
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    protected function configure()
30
    {
31 1
        if (function_exists('apcu_fetch') && class_exists(ApcuCache::class)) {
32
            $this->installApcCache(ApcuCache::class);
33
34
            return;
35
        }
36 1
        if (function_exists('apc_fetch')) {
37
            $this->installApcCache(ApcCache::class);
38
39
            return;
40
        }
41 1
        $this->installFileCache();
42
43 1
    }
44
45
    private function installApcCache($apcClass)
46
    {
47
        $this->bind(Cache::class)->to($apcClass)->in(Scope::SINGLETON);
48
        $this->bind(Reader::class)->toConstructor(
49
            CachedReader::class,
50
            'reader=annotation_reader'
51
        );
52
        $this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
53
        $this->bind(CacheProvider::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
54
        $this->bind(Cache::class)->annotatedWith(LogicCache::class)->to($apcClass)->in(Scope::SINGLETON);
55
        $this->bind(Cache::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
56
    }
57
58 1
    private function installFileCache()
59
    {
60 1
        $this->bind(Cache::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
61 1
        $this->bind(Reader::class)->toConstructor(
62 1
            CachedReader::class,
63
            'reader=annotation_reader'
64 1
        );
65 1
        $this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
66 1
        $this->bind(CacheProvider::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
67 1
        $this->bind(Cache::class)->annotatedWith(LogicCache::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
68 1
        $this->bind(Cache::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
69 1
    }
70
}
71