Passed
Pull Request — 1.x (#99)
by Akihito
10:41
created

DonutCacheModule::installAopMethodModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\CacheableResponse;
8
use BEAR\RepositoryModule\Annotation\DonutCache;
9
use BEAR\RepositoryModule\Annotation\RefreshCache;
10
use Ray\Di\AbstractModule;
11
use Ray\Di\Scope;
12
13
final class DonutCacheModule extends AbstractModule
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected function configure(): void
19
    {
20
        $this->bind(HeaderSetter::class);
21
        $this->bind(CdnCacheControlHeaderSetterInterface::class)->to(CdnCacheControlHeaderSetter::class);
22
        $this->bind(DonutRepositoryInterface::class)->to(DonutRepository::class)->in(Scope::SINGLETON);
23
        $this->bind(RepositoryLoggerInterface::class)->to(RepositoryLogger::class)->in(Scope::SINGLETON);
24
        $this->bind(PurgerInterface::class)->to(NullPurger::class);
25
        $this->bind(UriTagInterface::class)->to(UriTag::class)->in(Scope::SINGLETON);
26
        $this->installAopClassModule();
27
        $this->installAopMethodModule();
28
    }
29
30
    private function installAopClassModule(): void
31
    {
32
        $this->bind(DonutRepository::class)->in(Scope::SINGLETON);
33
        $this->bindPriorityInterceptor(
34
            $this->matcher->annotatedWith(CacheableResponse::class),
35
            $this->matcher->startsWith('onGet'),
36
            [DonutCacheableResponseInterceptor::class]
37
        );
38
        $this->bindPriorityInterceptor(
39
            $this->matcher->annotatedWith(DonutCache::class),
40
            $this->matcher->startsWith('onGet'),
41
            [DonutCacheInterceptor::class]
42
        );
43
44
        $this->bindInterceptor(
45
            $this->matcher->annotatedWith(CacheableResponse::class),
46
            $this->matcher->logicalOr(
47
                $this->matcher->startsWith('onPut'),
48
                $this->matcher->logicalOr(
49
                    $this->matcher->startsWith('onPatch'),
50
                    $this->matcher->startsWith('onDelete')
51
                )
52
            ),
53
            [DonutCommandInterceptor::class]
54
        );
55
    }
56
57
    private function installAopMethodModule(): void
58
    {
59
        $this->bindInterceptor(
60
            $this->matcher->any(),
61
            $this->matcher->annotatedWith(CacheableResponse::class),
62
            [DonutCacheInterceptor::class]
63
        );
64
        $this->bindInterceptor(
65
            $this->matcher->any(),
66
            $this->matcher->annotatedWith(RefreshCache::class),
67
            [DonutCacheInterceptor::class]
68
        );
69
    }
70
}
71