Comments   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 113
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A deleteComments() 0 4 1
A getHtml() 0 6 1
A new() 0 8 2
A update() 0 11 2
A render() 0 16 2
1
<?php
2
3
namespace litemerafrukt\Comments;
4
5
class Comments
6
{
7
    private $commentHandler;
8
    private $rootView;
9
10
    /**
11
     * Construct
12
     *
13
     * @param $db - database objekt. See source for interface.
14
     * @param $rootView - root view file for comments html rendering. See source for example.
15
     */
16 6
    public function __construct($commentHandler, $rootView = __DIR__.'/../../view/comments.php')
17
    {
18 6
        $this->commentHandler = $commentHandler;
19 6
        $this->rootView = $rootView;
20 6
    }
21
22
    /**
23
     * Create a comment
24
     *
25
     * @param $postId
26
     * @param $parentId - parent comment
27
     * @param $authorId - author/user id
28
     * @param $authorName - author/user name
29
     * @param $text - comment text
30
     *
31
     * @return void
32
     */
33 4
    public function new($postId, $parentId, $authorId, $authorName, $text)
34
    {
35 4
        if (\trim($text) === "") {
36 1
            return;
37
        }
38
39 3
        $this->commentHandler->new($postId, $parentId, $authorId, $authorName, $text);
40 3
    }
41
42
    /**
43
     * Update a comment
44
     *
45
     * @param $id - comment id
46
     * @param $text - comment text
47
     * @param $guard - function, comment only updates if guard returns true
48
     *
49
     * @return bool
50
     */
51 2
    public function update($id, $text, $guard)
52
    {
53 2
        $comment = $this->commentHandler->fetch($id);
54
55 2
        if (! $guard($comment)) {
56 1
            return false;
57
        }
58
59 1
        $this->commentHandler->upsert($id, $text);
60 1
        return true;
61
    }
62
63
    /**
64
     * Delete all comments assosiated with a post.
65
     *
66
     * @param $postId
67
     *
68
     * @return void
69
     */
70 3
    public function deleteComments($postId)
71
    {
72 3
        $this->commentHandler->deleteComments($postId);
73 3
    }
74
75
    /**
76
     * Get comment html for a post. All comments and save/edit forms.
77
     *
78
     * @param $postId
79
     * @param $user - true if there is user previlages.
80
     * @param $admin - true if admin previleges.
81
     * @param $username
82
     * @param $userId
83
     *
84
     * @return string
85
     */
86 5
    public function getHtml($postId, $user = false, $admin = false, $username = "unknown", $userId = null)
87
    {
88 5
        $commentGroups = $this->commentHandler->commentsForPost($postId);
89
90 5
        return $this->render($this->rootView, \compact('commentGroups', 'user', 'admin', 'username', 'userId'));
91
    }
92
93
    /**
94
     * Render view file with data.
95
     *
96
     * @param $file
97
     * @param $data
98
     * @return string
99
     * @throws \Exception
100
     */
101 5
    private function render($file, $data)
102
    {
103 5
        if (!file_exists($file)) {
104 1
            throw new \Exception("View template not found: $file.");
105
        }
106
107 4
        ob_start();
108
109 4
        extract($data);
110
111 4
        include $file;
112
113 4
        $output = ob_get_clean();
114
115 4
        return $output;
116
    }
117
}
118