CommentModel::updateComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Mafd16\Comment;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\Di\InjectionAwareTrait;
9
use \Mafd16\Comment\Comments;
10
11
/**
12
 * Comment system.
13
 */
14
class CommentModel implements
15
    ConfigureInterface,
16
    InjectionAwareInterface
17
{
18
    use ConfigureTrait,
19
        InjectionAwareTrait;
20
21
    /**
22
     * @var array $session inject a reference to the session.
23
     */
24
    //private $session;
25
26
27
28
    /**
29
     * @var string $key to use when storing in session.
30
     */
31
    const KEY = "commentsystem";
32
33
34
35
    /**
36
     * Inject dependencies.
37
     *
38
     * @param array $dependency key/value array with dependencies.
39
     *
40
     * @return self
41
     */
42
    //public function inject($dependency)
43
    //{
44
    //    $this->session = $dependency;
45
    //    return $this;
46
    //}
47
48
49
50
51
52
    /**
53
     * Get ALL comments from session
54
     *
55
     * @param string $key for data subset.
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     *
57
     * @return object with the dataset
58
     */
59 5
    public function getComments()
60
    {
61
        // Using db as storage:
62
        // Get users from db
63 5
        $com = new Comments();
64 5
        $com->setDb($this->di->get("db"));
65 5
        $comments = $com->findAll();
66
67 5
        return $comments;
68
    }
69
70
71
    /**
72
     * Get ONE comment from session
73
     *
74
     * @param string $key for dataset.
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     * @param int    $id for comment.
76
     *
77
     * @return array with the comment, name, email, id, or null if not exists
78
     */
79 5
    public function getComment($id)
80
    {
81
        // Using db
82 5
        $comments = $this->getComments();
83
        // Get comment with id $id
84 5
        $comment = null;
85 5
        foreach ($comments as $val) {
86 5
            if ($id == $val->id) {
87 5
                $comment = $val;
88 5
                break;
89
            }
90 5
        }
91 5
        return $comment;
92
    }
93
94
95
    /**
96
     * Add a comment to a dataset.
97
     *
98
     * @param array     $post   variables from posted comment
99
     *                          (article, name, email, comment)
100
     *
101
     * @return void
102
     */
103 5
    public function addComment($post)
104
    {
105
        // Connect to db
106 5
        $com = new Comments();
107 5
        $com->setDb($this->di->get("db"));
108
109 5
        $com->UserId = $post["id"];
110 5
        $com->UserName = $post["name"];
111 5
        $com->UserEmail = $post["email"];
112 5
        $com->comment = $post["comment"];
113
114 5
        $com->save();
115 5
    }
116
117
118
    /**
119
     * Update old comment with new comment
120
     *
121
     * @param int       $id         id for comment
122
     * @param array     $comment    the comment-array (name, email, comment, id)
123
     *
124
     * @return void
125
     */
126 2 View Code Duplication
    public function updateComment($id, $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        // Connect to db
129 2
        $com = new Comments();
130 2
        $com->setDb($this->di->get("db"));
131
        // Get comment
132 2
        $com->find("id", $id);
133
        // Update comment
134 2
        $com->comment = $comment["comment"];
135
        // Save
136 2
        $com->save();
137 2
    }
138
139
140
    /**
141
     * Delete comment with key and id
142
     *
143
     * @param int    $id            to delete
144
     *
145
     * @return void
146
     */
147 1 View Code Duplication
    public function deleteComment($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        // Set default timezone
150 1
        date_default_timezone_set('Europe/Stockholm');
151
        // Connect to db
152 1
        $com = new Comments();
153 1
        $com->setDb($this->di->get("db"));
154
        // Get comment
155 1
        $com->find("id", $id);
156
        // Delete (Update) comment
157 1
        $com->deleted = date("Y-m-d H:i:s");
158
        // Save
159 1
        $com->save();
160 1
    }
161
}
162