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

CacheableModule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 47
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A installAopModule() 0 33 1
A configure() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\AbstractCacheControl;
8
use BEAR\RepositoryModule\Annotation\Cacheable;
9
use BEAR\RepositoryModule\Annotation\Commands;
10
use BEAR\RepositoryModule\Annotation\Purge;
11
use BEAR\RepositoryModule\Annotation\Refresh;
12
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BEAR\QueryRepository\HttpCacheInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Ray\Di\AbstractModule;
14
15
final class CacheableModule extends AbstractModule
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure(): void
21
    {
22
        $this->bind(HttpCacheInterface::class)->to(HttpCache::class);
23
        $this->bind()->annotatedWith(Commands::class)->toProvider(CommandsProvider::class);
24
        $this->bind(RefreshInterceptor::class);
25
        $this->install(new StorageExpiryModule(60, 60 * 60, 60 * 60 * 24));
26
        $this->installAopModule();
27
    }
28
29
    protected function installAopModule(): void
30
    {
31
        $this->bindPriorityInterceptor(
32
            $this->matcher->annotatedWith(Cacheable::class),
33
            $this->matcher->startsWith('onGet'),
34
            [CacheInterceptor::class]
35
        );
36
        $this->bindInterceptor(
37
            $this->matcher->annotatedWith(Cacheable::class),
38
            $this->matcher->logicalOr(
39
                $this->matcher->startsWith('onPut'),
40
                $this->matcher->logicalOr(
41
                    $this->matcher->startsWith('onPatch'),
42
                    $this->matcher->startsWith('onDelete')
43
                )
44
            ),
45
            [CommandInterceptor::class]
46
        );
47
        $this->bindInterceptor(
48
            $this->matcher->logicalNot(
49
                $this->matcher->annotatedWith(Cacheable::class)
50
            ),
51
            $this->matcher->logicalOr(
52
                $this->matcher->annotatedWith(Purge::class),
53
                $this->matcher->annotatedWith(Refresh::class)
54
            ),
55
            [RefreshInterceptor::class]
56
        );
57
58
        $this->bindInterceptor(
59
            $this->matcher->annotatedWith(AbstractCacheControl::class),
60
            $this->matcher->startsWith('onGet'),
61
            [HttpCacheInterceptor::class]
62
        );
63
    }
64
}
65