Test Setup Failed
Push — master ( bbcf9b...73bf86 )
by Kirill
02:39
created

Filter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 14
cts 28
cp 0.5
rs 10
c 0
b 0
f 0
wmc 9
lcom 3
cbo 3
1
<?php
2
3
/**
4
 * This file is part of Stream package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Serafim\Stream\Filter;
13
14
use Psr\SimpleCache\CacheInterface;
15
use Serafim\Stream\Exception\StreamCreatingException;
16
use Serafim\Stream\Stream;
17
use Serafim\Stream\StreamInterface;
18
19
class Filter extends Conjunction
20
{
21
    /**
22
     * @var array<callable>
23
     */
24
    protected array $then = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    /**
27
     * @var StreamInterface
28
     */
29
    protected StreamInterface $stream;
30
31
    /**
32
     * @var bool
33
     */
34
    protected bool $vendor = false;
35
36
    /**
37
     * @param string $vendorDirectory
38
     * @throws StreamCreatingException
39
     */
40
    public function __construct(string $vendorDirectory)
41
    {
42 14
        $this->stream = Stream::new(32);
43
44 14
        $this->stream->onRead(function (string $sources) {
45
            foreach ($this->then as $handler) {
46
                $sources = $handler($sources);
47
            }
48
49
            return $sources;
50
        });
51
52 14
        $this->where(function (string $class, string $pathname) use ($vendorDirectory): bool {
53
            if ($this->vendor) {
54
                return true;
55 14
            }
56 1
57
            return \strpos($pathname, $vendorDirectory) !== 0;
58
        });
59 14
    }
60 14
61 14
    /**
62
     * @return BaseFilter|$this
63
     */
64
    public function exceptVendors(): self
65
    {
66 1
        $this->vendor = false;
67
68 1
        return $this;
69
    }
70 1
71
    /**
72
     * @internal Attention! Inclusion of all vendor files may cause undefined behavior.
73
     * @return BaseFilter|$this
74
     */
75
    public function withVendors(): self
76
    {
77 1
        $this->vendor = true;
78
79 1
        return $this;
80
    }
81 1
82
    /**
83
     * @param string $pathname
84
     * @return string
85
     */
86
    public function pathname(string $pathname): string
87
    {
88
        return $this->stream->pathname($pathname);
89
    }
90
91
    /**
92
     * @param callable $then
93
     * @return Filter
94
     */
95
    public function then(callable $then): self
96
    {
97
        $this->then[] = $then;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param CacheInterface $cache
104
     * @param callable $then
105
     * @return Filter
106
     */
107
    public function through(CacheInterface $cache, callable $then): self
108
    {
109
        return $this->then(function (string $sources) use ($cache, $then) {
110
            $key = \md5($sources);
111
112
            if (! $cache->has($key)) {
113
                $cache->set($key, $then($sources));
114
            }
115
116
            return $cache->get($key);
117
        });
118
    }
119
}
120