ContentRatingChange::getRating()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Api\Content;
4
5
use Apps\ActiveRecord\Content;
6
use Apps\ActiveRecord\ContentRating;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Interfaces\iUser;
10
11
class ContentRatingChange extends Model
12
{
13
    private $_content;
14
    private $_type;
15
    private $_user;
16
    
17
    /**
18
     * ContentRatingChange constructor. Pass inside record, type and user object
19
     * @param Content $record
20
     * @param string $type
21
     * @param iUser $user
22
     */
23
    public function __construct($record, $type, $user)
24
    {
25
        $this->_content = $record;
26
        $this->_type = $type;
27
        $this->_user = $user;
28
        parent::__construct();
29
    }
30
31
    /**
32
     * Check if content item is always rated by current user
33
     * @return boolean
34
     */
35
    public function isAlreadyRated()
36
    {
37
        return $this->_content->ratings->where('user_id', '=', $this->_user->getId())->count() > 0;
38
    }
39
    
40
    /**
41
     * Get content item rating
42
     * @return int
43
     */
44
    public function getRating()
45
    {
46
        return $this->_content->rating;
47
    }
48
    
49
    /**
50
     * Make content rating change - save results to db.
51
     */
52
    public function make()
53
    {
54
        // insert this rate to db logs
55
        $contentRating = new ContentRating();
56
        $contentRating->content_id = $this->_content->id;
57
        $contentRating->user_id = $this->_user->getId();
58
        $contentRating->type = $this->_type;
59
        $contentRating->save();
60
        
61
        // set ignored content id to rate in session
62
        $ignored = App::$Session->get('content.rate.ignore');
63
        $ignored[] = $this->_content->id;
64
        App::$Session->set('content.rate.ignore', $ignored);
65
        
66
        // save rating changes to database
67
        switch ($this->_type) {
68
            case 'plus':
69
                $this->_content->rating += 1;
70
                break;
71
            case 'minus':
72
                $this->_content->rating -= 1;
73
                break;
74
        }
75
        // save to db
76
        $this->_content->save();
77
        
78
        // update content author rating
79
        $authorId = (int)$this->_content->author_id;
80
        if ($authorId > 0 && App::$User->isExist($authorId)) {
81
            $authorObject = App::$User->identity($authorId);
82
            if ($authorObject !== null) {
83
                if ($this->_type === 'plus') {
84
                    $authorObject->profile->rating += 1;
85
                } else {
86
                    $authorObject->profile->rating -= 1;
87
                }
88
                $authorObject->profile->save();
89
            }
90
        }
91
        return true;
92
    }
93
}
94