Completed
Branch #338-Save_posting_before_movin... (2366dc)
by Schlaefer
02:58
created

UserPostingTrait::_isEditingAllowed()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 6
nop 2
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
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 Saito\Posting\UserPosting;
14
15
use Cake\Core\Configure;
16
use Saito\Posting\Basic\BasicPostingInterface;
17
use Saito\User\CurrentUser\CurrentUserInterface;
18
19
/**
20
 * Implements UserPostingInterface
21
 */
22
trait UserPostingTrait
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $_cache = [];
28
29
    /**
30
     * @var CurrentUserInterface
31
     */
32
    protected $_CurrentUser;
33
34
    /**
35
     * Get current-user.
36
     *
37
     * @return CurrentUserInterface
38
     */
39
    public function getCurrentUser()
40
    {
41
        return $this->_CurrentUser;
42
    }
43
44
    /**
45
     * Set current user.
46
     *
47
     * @param CurrentUserInterface $CurrentUser  current user
48
     * @return void
49
     */
50
    public function setCurrentUser($CurrentUser)
51
    {
52
        $this->_CurrentUser = $CurrentUser;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function isAnsweringForbidden()
59
    {
60
        if ($this->isLocked()) {
0 ignored issues
show
Bug introduced by
It seems like isLocked() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
            return 'locked';
62
        }
63
        $permission = $this->_CurrentUser->getCategories()->permission('answer', $this->get('category'));
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
65
        return !$permission;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function isBookmarked(): bool
72
    {
73
        return $this->_CurrentUser->hasBookmarked($this->get('id'));
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function isEditingAllowed(): bool
80
    {
81
        return $this->_isEditingAllowed($this, $this->_CurrentUser);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function isEditingAsUserAllowed(): bool
88
    {
89
        $MockedUser = clone $this->_CurrentUser;
90
        $MockedUser->set('user_type', 'user');
91
92
        return $this->_isEditingAllowed($this, $MockedUser);
93
    }
94
95
    /**
96
     * Check if editing on the posting is forbidden.
97
     *
98
     * @param BasicPostingInterface $posting The posting.
99
     * @param CurrentUserInterface $User The user.
100
     * @return bool|string string if a reason is available.
101
     */
102
    protected function _isEditingAllowed(BasicPostingInterface $posting, CurrentUserInterface $User)
103
    {
104
        if ($User->isLoggedIn() !== true) {
105
            return false;
106
        }
107
108
        if ($User->permission('saito.core.posting.edit.unrestricted')) {
109
            return true;
110
        }
111
112
        $editPeriod = Configure::read('Saito.Settings.edit_period') * 60;
113
        $timeLimit = $editPeriod + ($posting->get('time')->format('U'));
114
        $isOverTime = time() > $timeLimit;
115
116
        $isOwn = $User->getId() === $posting->get('user_id');
117
118
        if (!$isOverTime && $isOwn && !$this->isLocked()) {
0 ignored issues
show
Bug introduced by
It seems like isLocked() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
            return true;
120
        }
121
122
        if ($User->permission('saito.core.posting.edit.restricted')) {
123
            if ($posting->isPinned()) {
124
                return true;
125
            }
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function isIgnored(): bool
135
    {
136
        return $this->_CurrentUser->ignores($this->get('user_id'));
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function isUnread(): bool
143
    {
144
        if (!isset($this->_cache['isUnread'])) {
145
            $id = $this->get('id');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
146
            $time = $this->get('time');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
147
            $this->_cache['isUnread'] = !$this->getCurrentUser()
148
                ->getReadPostings()->isRead($id, $time);
149
        }
150
151
        return $this->_cache['isUnread'];
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157
    public function hasNewAnswers(): bool
158
    {
159
        if (!$this->isRoot()) {
0 ignored issues
show
Bug introduced by
It seems like isRoot() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
160
            throw new \RuntimeException(
161
                'Posting with id ' . $this->get('id') . ' is no root posting.'
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
162
            );
163
        }
164
        if (!$this->_CurrentUser->get('last_refresh')) {
165
            return false;
166
        }
167
168
        return $this->_CurrentUser->get('last_refresh_unix') < strtotime(
169
            $this->get('last_answer')
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
170
        );
171
    }
172
}
173