Failed Conditions
Branch master (3ce7e2)
by Nick
14:43
created

Topic::addContent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 9.0856
1
<?php
2
/**
3
 * Topic
4
 *
5
 * @package TheyWorkForYou
6
 */
7
8
namespace MySociety\TheyWorkForYou;
9
10
class Topic {
11
12
    /**
13
     * DB handle
14
     */
15
    private $db;
16
17
    private $raw;
0 ignored issues
show
introduced by
The private property $raw is not used, and could be removed.
Loading history...
18
    private $id;
19
    private $title;
20
    private $slug;
21
    private $description;
22
    private $image;
23
    private $search_string;
24
    private $front_page;
25
26
    /**
27
     * Constructor
28
     *
29
     */
30
31
    public function __construct($data = NULL)
32
    {
33
        $this->db = new \ParlDB;
34
35
        if (is_null($data)) {
36
          return;
37
        }
38
39
        $this->id = $data['id'];
40
        $this->title = $data['title'];
41
        $this->slug = $data['slug'];
42
        $this->description = $data['description'];
43
        $this->search_string = $data['search_string'];
44
        $this->front_page = $data['front_page'];
45
        $this->image = $data['image'];
46
47
    }
48
49
50
    function title() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
        return $this->title;
52
    }
53
54
    function sctitle() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
        $title = $this->title;
56
        if (strpos($title, 'The ') === 0 ) {
57
            $title = lcfirst($title);
58
        }
59
60
        return $title;
61
    }
62
63
    function set_title($title) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
        $this->title = $title;
65
    }
66
67
    function slug() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
        return $this->slug;
69
    }
70
71
    function set_slug($slug) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
72
        $this->slug = $slug;
73
    }
74
75
    function url() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
        $url = new Url('topic');
77
        return $url->generate() . $this->slug;
78
    }
79
80
    function image() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
        return $this->image;
82
    }
83
84
    function image_url() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
        return "/topic/image.php?id=" . $this->slug();
86
    }
87
88
    function image_path() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
        if ($this->image) {
90
            return sprintf('%s%s%s', TOPICIMAGEPATH, DIRECTORY_SEPARATOR, $this->image);
1 ignored issue
show
Bug introduced by
The constant MySociety\TheyWorkForYou\TOPICIMAGEPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
        }
92
93
        return false;
94
    }
95
96
    function set_image($image) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
        $this->image = $image;
98
    }
99
100
101
    function description() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
        return $this->description;
103
    }
104
105
    function set_description($description) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
        $this->description = $description;
107
    }
108
109
    function search_string() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
        return $this->search_string;
111
    }
112
113
    function set_search_string($search_string) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
114
        $this->search_string = $search_string;
115
    }
116
117
    function set_front_page($on) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
        $this->front_page = $on;
119
    }
120
121
    function onFrontPage() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
        return $this->front_page == 1;
123
    }
124
125
    private function _getContentIDs() {
126
        $q = $this->db->query(
127
          "SELECT body, gid, ep.epobject_id FROM epobject ep
128
           JOIN hansard h on ep.epobject_id = h.epobject_id
129
           JOIN topic_epobjects te on te.epobject_id = ep.epobject_id
130
           WHERE topic_key = :topic_key",
131
            array(
132
                ':topic_key' => $this->id
133
            )
134
        );
135
136
        return $q;
137
    }
138
139
    function getContent() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
        $q = $this->_getContentIDs();
141
142
        $content = array();
143
        $rows = $q->rows;
144
        for ($i = 0; $i < $rows; $i++) {
145
            $content[] = array(
146
                'title' => $q->field($i, 'body'),
147
                'href'  => Utility\Hansard::gid_to_url($q->field($i, 'gid')),
148
                'id'    => $q->field($i, 'epobject_id'),
149
            );
150
        }
151
152
        return $content;
153
    }
154
155
    function getFullContent() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
156
        $q = $this->_getContentIDs();
157
158
        $content = array();
159
        $rows = $q->rows;
160
        for ($i = 0; $i < $rows; $i++) {
161
            $gid = $q->field($i, 'gid');
162
            if (strpos($gid, 'lords') !== false) {
163
                $debatelist = new \LORDSDEBATELIST;
164
            } elseif (strpos($gid, 'westminhall') !== false) {
165
                $debatelist = new \WHALLLIST;
166
            } else {
167
                $debatelist = new \DEBATELIST;
168
            }
169
            $data = $debatelist->display('featured_gid', array('gid' => $gid), 'none');
170
171
            $item = $data['data'];
172
            if (isset($item['parent']) && $item['body'] == $item['parent']['body']) {
173
                unset($item['parent']);
174
            }
175
            $content[] = $item;
176
        }
177
178
        return $content;
179
    }
180
181
    function addContent($gid) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
182
        $q = $this->db->query(
183
          "SELECT epobject_id FROM hansard WHERE gid = :gid",
184
          array(
185
            ":gid" => $gid
186
          )
187
        );
188
189
        if (!$q->success() || $q->rows == 0) {
190
          return false;
191
        }
192
193
        $epobject_id = $q->field(0, 'epobject_id');
194
195
        $q = $this->db->query(
196
          "INSERT INTO topic_epobjects (topic_key, epobject_id) VALUES (:topic, :ep_id)",
197
          array(
198
            ":topic" => $this->id,
199
            ":ep_id" => $epobject_id
200
          )
201
        );
202
203
        return $q->success();
204
    }
205
206
    function deleteContent($id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
        $q = $this->db->query(
208
          "DELETE FROM topic_epobjects WHERE topic_key = :topic AND epobject_id = :ep_id",
209
          array(
210
            ":topic" => $this->id,
211
            ":ep_id" => $id
212
          )
213
        );
214
215
        return $q->success();
216
    }
217
218
    function getPolicySets() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
219
      $q = $this->db->query(
220
        "SELECT policyset FROM topic_policysets WHERE topic_key = :key",
221
        array(
222
          ':key' => $this->id
223
        )
224
      );
225
226
      $sets = array();
227
      $count = $q->rows;
228
      for ($i = 0; $i < $count; $i++) {
229
        $sets[] = $q->field($i, 'policyset');
230
      }
231
232
      return $sets;
233
    }
234
235
    function addPolicySets($sets) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
236
        if ($sets === '' or count($sets) == 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
237
            $q = $this->db->query(
238
                "DELETE FROM topic_policysets WHERE topic_key = :topic_key",
239
                array(
240
                    ":topic_key" => $this->id
241
                )
242
            );
243
        } else {
244
            foreach ($sets as $set) {
245
                if ($set == '' ) {
246
                    continue;
247
                }
248
                $q = $this->db->query(
249
                    "REPLACE INTO topic_policysets (policyset, topic_key) VALUES (:policyset, :topic_key)",
250
                    array(
251
                        ':topic_key' => $this->id,
252
                        ':policyset' => $set
253
                    )
254
                );
255
            }
256
        }
257
258
        return $q->success();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $q does not seem to be defined for all execution paths leading up to this point.
Loading history...
259
    }
260
261
    function getPolicies() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
262
      $q = $this->db->query(
263
        'SELECT policy_id FROM topic_policies WHERE topic_key = :key',
264
        array(
265
          ':key' => $this->id
266
        )
267
      );
268
269
      $policies = array();
270
      $count = $q->rows;
271
      for ($i = 0; $i < $count; $i++) {
272
        $policies[] = $q->field($i, 'policy_id');
273
      }
274
275
      return $policies;
276
    }
277
278
    function addPolicies($policies) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
279
        if ($policies === '' or count($policies) == 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
280
            $q = $this->db->query(
281
                "DELETE FROM topic_policies WHERE topic_key = :topic_key",
282
                array(
283
                    ":topic_key" => $this->id
284
                )
285
            );
286
        } else {
287
            foreach ($policies as $policy) {
288
                if ($policy == '' ) {
289
                    continue;
290
                }
291
                $q = $this->db->query(
292
                    "REPLACE INTO topic_policies (policy_id, topic_key) VALUES (:policy, :topic_key)",
293
                    array(
294
                        ':topic_key' => $this->id,
295
                        ':policy' => $policy
296
                    )
297
                );
298
            }
299
        }
300
301
        return $q->success();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $q does not seem to be defined for all execution paths leading up to this point.
Loading history...
302
    }
303
304
    function getAllPolicies() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
305
        $policy_sets = $this->getPolicySets();
306
        $all_policies = array();
307
        $policies = new Policies();
308
        foreach ($policy_sets as $set) {
309
            $all_policies = array_merge($all_policies, array_keys($policies->limitToSet($set)->getPolicies()));
310
        }
311
        $topic_policies = $this->getPolicies();
312
        $all_policies = array_merge($all_policies, $topic_policies);
313
314
        return array_unique($all_policies);
315
    }
316
317
    function save() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
318
        $q = $this->db->query(
319
          "REPLACE INTO topics
320
          (id, title, slug, description, search_string, front_page, image)
321
          VALUES
322
          (:id, :title, :slug, :description, :search_string, :front_page, :image)",
323
            array(
324
                ':id' => $this->id,
325
                ':slug' => $this->slug(),
326
                ':title' => $this->title(),
327
                ':description' => $this->description(),
328
                ':search_string' => $this->search_string(),
329
                ':front_page' => $this->onFrontPage(),
330
                ':image' => $this->image()
331
            )
332
        );
333
334
        return $q->success();
335
    }
336
}
337