Issues (326)

src/Controller/Component/MarkAsReadComponent.php (3 issues)

Severity
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\Component;
14
15
use App\Controller\AppController;
16
use Cake\Controller\Component;
17
use Saito\Posting\PostingInterface;
18
19
/**
20
 * Class MarkAsReadComponent
21
 *
22
 * @package App\Controller\Component
23
 */
24
class MarkAsReadComponent extends Component
25
{
26
    /**
27
     * @var array of Posting
28
     */
29
    protected $postings = [];
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function shutdown()
35
    {
36
        if (empty($this->postings)) {
37
            return;
38
        }
39
40
        /** @var AppController */
41
        $controller = $this->getController();
42
        $controller->CurrentUser->getReadPostings()->set($this->postings);
43
    }
44
45
    /**
46
     * On next reload
47
     *
48
     * @return void
49
     */
50
    public function next()
51
    {
52
        /** @var AppController */
53
        $controller = $this->getController();
54
        $CU = $controller->CurrentUser;
55
        if (!$CU->isLoggedIn() || !$CU->get('user_automaticaly_mark_as_read')) {
56
            return;
57
        }
58
        $this->_registry->getController()->set('markAsRead', true);
59
    }
60
61
    /**
62
     * automatic mark-as-read
63
     *
64
     * @param array $options options
65
     * - 'enabled' (bool) - Is enabled. Default: get from current-user preference.
66
     *
67
     * @return bool true on refresh
68
     */
69
    public function refresh(array $options = [])
70
    {
71
        /** @var AppController */
72
        $controller = $this->getController();
73
        $CU = $controller->CurrentUser;
74
        if ($this->request->is('preview') || !$CU->isLoggedIn()) {
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead. ( Ignorable by Annotation )

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

74
        if (/** @scrutinizer ignore-deprecated */ $this->request->is('preview') || !$CU->isLoggedIn()) {

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

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

Loading history...
75
            return false;
76
        }
77
78
        $options += [
79
            'enabled' => $CU->get('user_automaticaly_mark_as_read'),
80
        ];
81
82
        if (!$options['enabled']) {
83
            return false;
84
        }
85
86
        $session = $this->request->getSession();
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead. ( Ignorable by Annotation )

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

86
        $session = /** @scrutinizer ignore-deprecated */ $this->request->getSession();

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

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

Loading history...
87
        $lastRefreshTemp = $session->read('User.last_refresh_tmp');
88
        if (empty($lastRefreshTemp)) {
89
            // new session
90
            $lastRefreshTemp = time();
91
            $session->write('User.last_refresh_tmp', $lastRefreshTemp);
92
        }
93
94
        if ($this->request->getQuery('mar') !== null) {
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead. ( Ignorable by Annotation )

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

94
        if (/** @scrutinizer ignore-deprecated */ $this->request->getQuery('mar') !== null) {

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

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

Loading history...
95
            // a second session A shall not accidentally mark something as read that isn't read on session B
96
            if ($lastRefreshTemp > $CU->get('last_refresh_unix')) {
97
                $CU->getLastRefresh()->set();
98
            }
99
            $session->write('User.last_refresh_tmp', time());
100
101
            return true;
102
        } else {
103
            $CU->getLastRefresh()->setMarker();
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Mark single posting as read
111
     *
112
     * @param PostingInterface $posting posting
113
     *
114
     * @return void
115
     */
116
    public function posting(PostingInterface $posting)
117
    {
118
        $this->postings[] = $posting;
119
    }
120
121
    /**
122
     * Mark posting and all subpostings as read
123
     *
124
     * @param PostingInterface $posting posting
125
     * @return void
126
     */
127
    public function thread(PostingInterface $posting)
128
    {
129
        $postings = $posting->getAllChildren();
130
        $postings[$posting->get('id')] = $posting;
131
        $this->postings = array_merge($this->postings, $postings);
132
    }
133
}
134