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