ProdModule::installCacheModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 17
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Context;
6
7
use BEAR\Package\Provide\Cache\CacheDirProvider;
8
use BEAR\Package\Provide\Error\ErrorPageFactoryInterface;
9
use BEAR\Package\Provide\Error\ProdVndErrorPageFactory;
10
use BEAR\Package\Provide\Logger\ProdMonologProvider;
11
use BEAR\QueryRepository\ProdQueryRepositoryModule;
12
use BEAR\RepositoryModule\Annotation\EtagPool;
13
use BEAR\Resource\NullOptionsRenderer;
14
use BEAR\Resource\RenderInterface;
15
use Doctrine\Common\Annotations\AnnotationReader;
16
use Doctrine\Common\Annotations\PsrCachedReader;
17
use Doctrine\Common\Annotations\Reader;
18
use Koriym\Attributes\AttributeReader;
19
use Koriym\Attributes\DualReader;
20
use Psr\Cache\CacheItemInterface;
21
use Psr\Log\LoggerInterface;
22
use Ray\Compiler\DiCompileModule;
23
use Ray\Di\AbstractModule;
24
use Ray\Di\Scope;
25
use Ray\PsrCacheModule\Annotation\CacheDir;
26
use Ray\PsrCacheModule\Annotation\Local;
27
use Ray\PsrCacheModule\LocalCacheProvider;
28
use Ray\PsrCacheModule\Psr6LocalCacheModule;
29
30
/** @codeCoverageIgnore */
31
class ProdModule extends AbstractModule
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected function configure(): void
37
    {
38
        $this->bind(ErrorPageFactoryInterface::class)->to(ProdVndErrorPageFactory::class);
39
        $this->bind(LoggerInterface::class)->toProvider(ProdMonologProvider::class)->in(Scope::SINGLETON);
40
        $this->disableOptionsMethod();
41
        $this->installCacheModule();
42
        $this->install(new DiCompileModule(true));
43
    }
44
45
    private function installCacheModule(): void
46
    {
47
        $this->install(new ProdQueryRepositoryModule());
48
        $this->bind('')->annotatedWith(CacheDir::class)->toProvider(CacheDirProvider::class);
49
        $this->install(new Psr6LocalCacheModule());
50
        /** @psalm-suppress DeprecatedClass */
51
        $this->bind(CacheItemInterface::class)->annotatedWith(EtagPool::class)->toProvider(LocalCacheProvider::class);
52
        $this->bind(Reader::class)->toConstructor(
53
            PsrCachedReader::class,
54
            ['reader' => 'dual_reader', 'cache' => Local::class],
55
        )->in(Scope::SINGLETON);
56
        $this->bind(Reader::class)->annotatedWith('dual_reader')->toConstructor(
57
            DualReader::class,
58
            ['annotationReader' => 'annotation_reader', 'attributeReader' => 'attribute_reader'],
59
        );
60
        $this->bind(Reader::class)->annotatedWith('annotation_reader')->to(AnnotationReader::class);
61
        $this->bind(Reader::class)->annotatedWith('attribute_reader')->to(AttributeReader::class);
62
    }
63
64
    /**
65
     * Disable OPTIONS resource request method in production
66
     *
67
     * OPTIONS method return 405 Method Not Allowed error code. To enable OPTIONS in `prod` context,
68
     * Install BEAR\Resource\Module\OptionsMethodModule() in your ProdModule.
69
     */
70
    private function disableOptionsMethod(): void
71
    {
72
        $this->bind(RenderInterface::class)->annotatedWith('options')->to(NullOptionsRenderer::class);
73
    }
74
}
75