1 | <?php |
||
26 | class Comment extends AbstractModel |
||
27 | { |
||
28 | /** |
||
29 | * Instance's parent. |
||
30 | * |
||
31 | * @access private |
||
32 | * @var Paste |
||
33 | */ |
||
34 | private $_paste; |
||
35 | |||
36 | /** |
||
37 | * Get comment data. |
||
38 | * |
||
39 | * @access public |
||
40 | * @throws Exception |
||
41 | * @return stdClass |
||
42 | */ |
||
43 | 5 | public function get() |
|
58 | |||
59 | /** |
||
60 | * Store the comment's data. |
||
61 | * |
||
62 | * @access public |
||
63 | * @throws Exception |
||
64 | */ |
||
65 | 14 | public function store() |
|
66 | { |
||
67 | // Make sure paste exists. |
||
68 | 14 | $pasteid = $this->getPaste()->getId(); |
|
69 | 14 | if (!$this->getPaste()->exists()) { |
|
70 | 1 | throw new Exception('Invalid data.', 67); |
|
71 | } |
||
72 | |||
73 | // Make sure the discussion is opened in this paste and in configuration. |
||
74 | 13 | if (!$this->getPaste()->isOpendiscussion() || !$this->_conf->getKey('discussion')) { |
|
75 | 3 | throw new Exception('Invalid data.', 68); |
|
76 | } |
||
77 | |||
78 | // Check for improbable collision. |
||
79 | 10 | if ($this->exists()) { |
|
80 | 3 | throw new Exception('You are unlucky. Try again.', 69); |
|
81 | } |
||
82 | |||
83 | 8 | $this->_data->meta->postdate = time(); |
|
84 | |||
85 | // store comment |
||
86 | if ( |
||
|
|||
87 | 8 | $this->_store->createComment( |
|
88 | 8 | $pasteid, |
|
89 | 8 | $this->getParentId(), |
|
90 | 8 | $this->getId(), |
|
91 | 8 | json_decode(json_encode($this->_data), true) |
|
92 | 8 | ) === false |
|
93 | ) { |
||
94 | throw new Exception('Error saving comment. Sorry.', 70); |
||
95 | } |
||
96 | 8 | } |
|
97 | |||
98 | /** |
||
99 | * Delete the comment. |
||
100 | * |
||
101 | * @access public |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | 1 | public function delete() |
|
108 | |||
109 | /** |
||
110 | * Test if comment exists in store. |
||
111 | * |
||
112 | * @access public |
||
113 | * @return bool |
||
114 | */ |
||
115 | 10 | public function exists() |
|
123 | |||
124 | /** |
||
125 | * Set paste. |
||
126 | * |
||
127 | * @access public |
||
128 | * @param Paste $paste |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | 19 | public function setPaste(Paste $paste) |
|
136 | |||
137 | /** |
||
138 | * Get paste. |
||
139 | * |
||
140 | * @access public |
||
141 | * @return Paste |
||
142 | */ |
||
143 | 14 | public function getPaste() |
|
147 | |||
148 | /** |
||
149 | * Set parent ID. |
||
150 | * |
||
151 | * @access public |
||
152 | * @param string $id |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | 19 | public function setParentId($id) |
|
162 | |||
163 | /** |
||
164 | * Get parent ID. |
||
165 | * |
||
166 | * @access public |
||
167 | * @return string |
||
168 | */ |
||
169 | 10 | public function getParentId() |
|
176 | |||
177 | /** |
||
178 | * Set nickname. |
||
179 | * |
||
180 | * @access public |
||
181 | * @param string $nickname |
||
182 | * @throws Exception |
||
183 | */ |
||
184 | 14 | public function setNickname($nickname) |
|
213 | } |
||
214 |
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.