Streaming::update()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 39
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 3
nop 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Plugin;
14
15
/**
16
 * TODO
17
 * @codeCoverageIgnore
18
 */
19
class Streaming extends PluginAbstract
20
{
21
22
    public static $hook = array('response', 'early');
23
24
    public $streamed = false;
25
    public $callback = null;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param array $options Array of options.
31
     */
32
    public function __construct(array $options=null)
33
    {
34
        // $this->setOptions($options);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
    }
36
37
    public static function ob()
38
    {
39
        apache_setenv('no-gzip', '1');
40
        // ini_set('zlib.output_compression', 0);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
42
        while (ob_get_level()) {
43
            ob_end_flush();
44
        }
45
        if (ob_get_length() === false) {
46
            ob_start();
47
        }
48
    }
49
50
    public function update(\SplSubject $response)
51
    {
52
        if ($this->streamed) {
53
            return;
54
        }
55
56
        $this->callback = function () use ($response) {
57
            header('Content-type: text/html; charset=utf-8');
58
            echo 'Begin ...<br>';
59
60
            for ($i = 0 ; $i < 10 ; $i++) {
61
                echo $i . '<hr>';
62
                ob_flush();
63
                flush();
64
                sleep(1);
65
            }
66
            sleep(1);
67
            echo 'End ...<br />';
68
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method update() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
69
        };
70
71
        if (!is_callable($this->callback)) {
72
            throw new \LogicException('The Response callback must be a valid PHP callable.');
73
        }
74
75
        // $this->headers->set('Cache-Control', 'no-cache');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
77
        $this->streamed = true;
78
        call_user_func($this->callback);
79
80
        // $response->setOutput(
81
        //     $this->tidy(
82
        //         $response->getOutput(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        //         isset($this->options[$format])
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
        //             ? $this->options[$format]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
        //             : array()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        //     )
87
        // );
88
    }
89
90
}
91