Passed
Push — master ( d3fd5d...81f334 )
by Mihail
03:51
created

ActionChangeRating   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 9

1 Method

Rating   Name   Duplication   Size   Complexity  
C changeRating() 0 64 11
1
<?php
2
3
4
namespace Apps\Controller\Api\Profile;
5
6
use Apps\ActiveRecord\ProfileRating;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Exception\ForbiddenException;
9
use Ffcms\Core\Exception\NativeException;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Helper\Date;
12
use Ffcms\Core\Helper\Type\Any;
13
use Ffcms\Core\Network\Request;
14
use Ffcms\Core\Network\Response;
15
16
/**
17
 * Trait ActionChangeRating
18
 * @package Apps\Controller\Api\Profile
19
 * @property Request $request
20
 * @property Response $response
21
 * @method void setJsonHeader
22
 */
23
trait ActionChangeRating
24
{
25
    /**
26
     * Change user rating action
27
     * @return string
28
     * @throws ForbiddenException
29
     * @throws NativeException
30
     * @throws NotFoundException
31
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33
    public function changeRating(): ?string
34
    {
35
        if (!App::$User->isAuth()) {
36
            throw new ForbiddenException('Auth required');
37
        }
38
39
        $this->setJsonHeader();
0 ignored issues
show
Bug introduced by
It seems like setJsonHeader() 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...
40
41
        // get operation type and target user id
42
        $targetId = (int)$this->request->get('target');
43
        $type = $this->request->get('type');
44
45
        // check type of query
46
        if ($type !== '+' && $type !== '-') {
47
            throw new NativeException('Wrong data');
48
        }
49
50
        // check if passed user id is exist
51
        if (!Any::isInt($targetId) || $targetId < 1 || !App::$User->isExist($targetId)) {
52
            throw new NotFoundException('Wrong user info');
53
        }
54
55
        $cfg = \Apps\ActiveRecord\App::getConfigs('app', 'Profile');
56
        // check if rating is enabled for website
57
        if (!(bool)$cfg['rating']) {
58
            throw new NativeException('Rating is disabled');
59
        }
60
61
        // get target and sender objects
62
        $target = App::$User->identity($targetId);
63
        $sender = App::$User->identity();
64
65
        // disable self-based changes ;)
66
        if ($target->getId() === $sender->getId()) {
67
            throw new ForbiddenException('Self change prevented');
68
        }
69
70
        // check delay
71
        $diff = Date::convertToDatetime(time() - $cfg['ratingDelay'], Date::FORMAT_SQL_TIMESTAMP);
72
73
        $query = ProfileRating::where('target_id', $target->getId())
74
            ->where('sender_id', $sender->getId())
75
            ->where('created_at', '>=', $diff)
76
            ->orderBy('id', 'DESC');
77
        if ($query->count() > 0) {
78
            throw new ForbiddenException('Delay required');
79
        }
80
81
        // delay is ok, lets insert a row
82
        $record = new ProfileRating();
83
        $record->target_id = $target->getId();
84
        $record->sender_id = $sender->getId();
85
        $record->type = $type;
86
        $record->save();
87
88
        if ($type === '+') {
89
            $target->profile->rating += 1;
90
        } else {
91
            $target->profile->rating -= 1;
92
        }
93
        $target->profile->save();
94
95
        return json_encode(['status' => 1, 'data' => 'ok']);
96
    }
97
}
98