Completed
Push — master ( b3a060...a91f17 )
by Maik
02:01
created

CachedStreamInterceptor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 66
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 13 3
A getCache() 0 4 1
A getFilterName() 0 4 1
A reset() 0 4 1
1
<?php
2
/**
3
 * This file is part of the PHP Generics package.
4
 *
5
 * @package Generics
6
 */
7
namespace Generics\Streams\Interceptor;
8
9
/**
10
 * This class provides an caching stream interceptor
11
 *
12
 * @author Maik Greubel <[email protected]>
13
 */
14
class CachedStreamInterceptor extends AbstractStreamInterceptor
15
{
16
17
    /**
18
     *
19
     * @var string
20
     */
21
    private static $cache = "";
22
23
    /**
24
     * Create a new instance of CachedStreamInterceptor
25
     */
26 1
    public function __construct()
27
    {
28 1
        stream_filter_register($this->getFilterName(), CachedStreamInterceptor::class);
29 1
    }
30
31
    /**
32
     *
33
     * {@inheritdoc}
34
     * @see \Generics\Streams\Interceptor\StreamInterceptor::filter()
35
     */
36 1
    public function filter($in, $out, int &$consumed, bool $closing): int
37
    {
38 1
        if ($closing) {
39 1
            return PSFS_FEED_ME;
40
        }
41
        
42 1
        while ($bucket = stream_bucket_make_writeable($in)) {
43 1
            self::$cache .= $bucket->data;
44 1
            $consumed += $bucket->datalen;
45 1
            stream_bucket_append($out, $bucket);
46
        }
47 1
        return PSFS_PASS_ON;
48
    }
49
50
    /**
51
     * Retrieve the cache buffer
52
     * 
53
     * @return string
54
     */
55 1
    public function getCache(): string
56
    {
57 1
        return self::$cache;
58
    }
59
60
    /**
61
     *
62
     * {@inheritdoc}
63
     * @see \Generics\Streams\Interceptor\StreamInterceptor::getFilterName()
64
     */
65 1
    public function getFilterName(): string
66
    {
67 1
        return strtolower(CachedStreamInterceptor::class);
68
    }
69
70
    /**
71
     *
72
     * {@inheritdoc}
73
     * @see \Generics\Streams\Interceptor\StreamInterceptor::reset()
74
     */
75 1
    public function reset()
76
    {
77 1
        self::$cache = "";
78
    }
79
}