Passed
Push — master ( a29a7e...12a432 )
by Mihail
08:06
created

ActionChangeRate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 55
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D changeRate() 0 44 9
1
<?php
2
3
namespace Apps\Controller\Api\Content;
4
5
use Apps\Model\Api\Content\ContentRatingChange;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Exception\ForbiddenException;
8
use Ffcms\Core\Exception\NativeException;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Helper\Type\Arr;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
use Apps\ActiveRecord\Content as ContentRecord;
15
16
/**
17
 * Trait ActionChangeRate
18
 * @package Apps\Controller\Api\Content
19
 * @property Request $request
20
 * @property Response $response
21
 * @method void setJsonHeader()
22
 */
23
trait ActionChangeRate
24
{
25
    /**
26
     * Change content item rating action
27
     * @param string $type
28
     * @param string $id
29
     * @throws NativeException
30
     * @throws ForbiddenException
31
     * @throws NotFoundException
32
     * @return string
33
     */
34
    public function changeRate(string $type, string $id)
35
    {
36
        $this->setJsonHeader();
37
38
        // check input params
39
        if (!Arr::in($type, ['plus', 'minus']) || !Any::isInt($id)) {
40
            throw new NativeException('Bad conditions');
41
        }
42
43
        // get current user and check is authed
44
        $user = App::$User->identity();
45
        if ($user === null || !App::$User->isAuth()) {
46
            throw new ForbiddenException(__('Authorization is required!'));
47
        }
48
49
        // set ignored content id to rate in session
50
        $ignored = App::$Session->get('content.rate.ignore');
51
        $ignored[] = $id;
52
        App::$Session->set('content.rate.ignore', $ignored);
53
54
        // find content record
55
        $record = ContentRecord::find($id);
56
        if ($record === null || $record->count() < 1) {
57
            throw new NotFoundException(__('Content item is not founded'));
58
        }
59
60
        // check if author rate him-self content
61
        if ($record->author_id === $user->getId()) {
0 ignored issues
show
Bug introduced by
The property author_id does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
62
            throw new ForbiddenException(__('You can not rate your own content'));
63
        }
64
65
        // initialize model
66
        $model = new ContentRatingChange($record, $type, $user);
67
        // check if content items is already rated by this user
68
        if ($model->isAlreadyRated()) {
69
            throw new ForbiddenException(__('You have already rate this!'));
70
        }
71
72
        // make rate - add +1 to content rating and author rating
73
        $model->make();
74
75
        return json_encode([
76
            'status' => 1,
77
            'rating' => $model->getRating()
78
        ]);
79
    }
80
}
81