Completed
Push — develop ( 31679f...f95741 )
by Schlaefer
11:21
created

StatusController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 73
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A status() 0 12 2
A _statusAsJson() 0 8 2
A beforeFilter() 0 4 1
A _statusAsEventStream() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Controller;
14
15
use Cake\Event\Event;
16
use Cake\Http\Exception\BadRequestException;
17
use Cake\Http\Response;
18
19
class StatusController extends AppController
20
{
21
22
    public $autoRender = false;
23
24
    /**
25
     * Sends status data to the frontend
26
     *
27
     * Even if no data is send for other functionality the ping keeps the
28
     * current user online.
29
     *
30
     * @return Response
31
     */
32
    public function status()
33
    {
34
        $data = [];
35
        $data = json_encode($data);
36
        if ($this->request->accepts('text/event-streams')) {
37
            $body = $this->_statusAsEventStream($data);
38
        } else {
39
            $body = $this->_statusAsJson($data);
40
        }
41
        $this->response = $this->response->withStringBody($body);
42
43
        return $this->response;
44
    }
45
46
    /**
47
     * Get status as event-stream
48
     *
49
     * @param string $data json-encoded data
50
     * @return string
51
     */
52
    protected function _statusAsEventStream($data)
53
    {
54
        // time in ms to next request
55
        $retry = '10000';
56
        $this->response = $this->response->withType(['eventstream' => 'text/event-stream']);
0 ignored issues
show
Bug introduced by
array('eventstream' => 'text/event-stream') of type array<string,string> is incompatible with the type string expected by parameter $contentType of Cake\Http\Response::withType(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->response = $this->response->withType(/** @scrutinizer ignore-type */ ['eventstream' => 'text/event-stream']);
Loading history...
57
        $this->response = $this->response->withType('eventstream');
58
        $this->response->disableCache();
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::disableCache() has been deprecated: 3.4.0 Use withDisabledCache() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
        /** @scrutinizer ignore-deprecated */ $this->response->disableCache();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
59
        $out = '';
60
        $out .= "retry: $retry\n";
61
        $out .= 'data: ' . $data . "\n\n";
62
63
        return $out;
64
    }
65
66
    /**
67
     * Get status as JSON response
68
     *
69
     * @param string $data json-encoded data
70
     * @return mixed
71
     */
72
    protected function _statusAsJson($data)
73
    {
74
        if ($this->request->is('ajax') === false) {
75
            throw new BadRequestException();
76
        }
77
        $this->response = $this->response->withType('json');
78
79
        return $data;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @param Event $event An Event instance
86
     * @return void
87
     */
88
    public function beforeFilter(Event $event)
89
    {
90
        parent::beforeFilter($event);
91
        $this->components()->unload('Authentication');
92
    }
93
}
94