Completed
Push — 1.x ( 49ef1e...c0b773 )
by Akihito
02:53
created

ProdModule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 4
A disableOptionsMethod() 0 4 1
A installApcuCache() 0 11 1
A installFileCache() 0 11 1
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\RenderInterface;
12
use BEAR\Resource\VoidOptionsRenderer;
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use Doctrine\Common\Annotations\CachedReader;
15
use Doctrine\Common\Annotations\Reader;
16
use Doctrine\Common\Cache\ApcuCache;
17
use Doctrine\Common\Cache\Cache;
18
use Doctrine\Common\Cache\CacheProvider;
19
use Ray\Di\AbstractModule;
20
use Ray\Di\Scope;
21
22
/**
23
 * @codeCoverageIgnore
24
 */
25
class ProdModule extends AbstractModule
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        $this->disableOptionsMethod();
33
        if (PHP_SAPI !== 'cli' && function_exists('apcu_fetch') && class_exists(ApcuCache::class)) {
34
            $this->installApcuCache(ApcuCache::class);
35
36
            return;
37
        }
38
        $this->installFileCache();
39
    }
40
41
    /**
42
     * Disable OPTIONS resource request method in production
43
     *
44
     * OPTIONS method return 405 Method Not Allowed error code. To enable OPTIONS in `prod` context,
45
     * Install BEAR\Resource\Module\OptionsMethodModule() in your ProdModule.
46
     */
47
    private function disableOptionsMethod()
48
    {
49
        $this->bind(RenderInterface::class)->annotatedWith('options')->to(VoidOptionsRenderer::class);
50
    }
51
52
    private function installApcuCache($apcClass)
53
    {
54
        $this->bind(Cache::class)->to($apcClass)->in(Scope::SINGLETON);
55
        $this->bind(Reader::class)->toConstructor(
56
            CachedReader::class,
57
            'reader=annotation_reader'
58
        );
59
        $this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
60
        $this->bind(CacheProvider::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
61
        $this->bind(Cache::class)->annotatedWith(Storage::class)->to($apcClass)->in(Scope::SINGLETON);
62
    }
63
64
    private function installFileCache()
65
    {
66
        $this->bind(Cache::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
67
        $this->bind(Reader::class)->toConstructor(
68
            CachedReader::class,
69
            'reader=annotation_reader'
70
        );
71
        $this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
72
        $this->bind(CacheProvider::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
73
        $this->bind(Cache::class)->annotatedWith(Storage::class)->toProvider(FilesystemCacheProvider::class)->in(Scope::SINGLETON);
74
    }
75
}
76