CacheableModule::installAopModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 9.552
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 Override;
14
use Ray\Di\AbstractModule;
15
16
/**
17
 *  Provides HttpCacheInterface and derived bindings
18
 *
19
 *  The following bindings are provided:
20
 *
21
 *  HttpCacheInterface
22
 *  -Commands
23
 *  RefreshInterceptor
24
 *  StorageExpiryModule
25
 */
26
final class CacheableModule extends AbstractModule
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    #[Override]
32
    protected function configure(): void
33
    {
34
        $this->bind(HttpCacheInterface::class)->to(HttpCache::class);
35
        $this->bind()->annotatedWith(Commands::class)->toProvider(CommandsProvider::class);
36
        $this->bind(RefreshInterceptor::class);
37
        $this->install(new StorageExpiryModule(60, 60 * 60, 60 * 60 * 24));
38
        $this->installAopModule();
39
    }
40
41
    protected function installAopModule(): void
42
    {
43
        $this->bindPriorityInterceptor(
44
            $this->matcher->annotatedWith(Cacheable::class),
45
            $this->matcher->startsWith('onGet'),
46
            [CacheInterceptor::class],
47
        );
48
        $this->bindInterceptor(
49
            $this->matcher->annotatedWith(Cacheable::class),
50
            $this->matcher->logicalOr(
51
                $this->matcher->startsWith('onPut'),
52
                $this->matcher->logicalOr(
53
                    $this->matcher->startsWith('onPatch'),
54
                    $this->matcher->startsWith('onDelete'),
55
                ),
56
            ),
57
            [CommandInterceptor::class],
58
        );
59
        $this->bindInterceptor(
60
            $this->matcher->logicalNot(
61
                $this->matcher->annotatedWith(Cacheable::class),
62
            ),
63
            $this->matcher->logicalOr(
64
                $this->matcher->annotatedWith(Purge::class),
65
                $this->matcher->annotatedWith(Refresh::class),
66
            ),
67
            [RefreshInterceptor::class],
68
        );
69
70
        $this->bindInterceptor(
71
            $this->matcher->annotatedWith(AbstractCacheControl::class),
72
            $this->matcher->startsWith('onGet'),
73
            [HttpCacheInterceptor::class],
74
        );
75
    }
76
}
77