Failed Conditions
Branch master (215e8c)
by Johannes
04:26
created

Topics::getTopic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Topics
4
 *
5
 * @package TheyWorkForYou
6
 */
7
8
namespace MySociety\TheyWorkForYou;
9
10
class Topics {
11
12
    /**
13
     * DB handle
14
     */
15
    private $db;
16
17
    /**
18
     * Constructor
19
     *
20
     */
21
22
    public function __construct()
23
    {
24
        $this->db = new \ParlDB;
25
    }
26
27 View Code Duplication
    public function getTopics() {
28
      $q = $this->db->query("SELECT id, slug, title, description, search_string, front_page, image FROM topics");
29
30
      $topics = array();
31
      $count = $q->rows();
32
33
      for ($i = 0; $i < $count; $i++ ) {
34
          $topic = $q->row($i);
35
          $topics[$topic['slug']] = new Topic($topic);
36
      }
37
      return $topics;
38
    }
39
40
    public function getTopic($topic_name) {
41
      $q = $this->db->query(
42
          "SELECT id, slug, title, description, search_string, front_page, image FROM topics WHERE slug = :slug",
43
          array(':slug' => $topic_name)
44
      );
45
      if ($q->rows) {
46
          return new Topic($q->row(0));
47
      }
48
49
      return NULL;
50
    }
51
52 View Code Duplication
    public function getFrontPageTopics() {
53
      $q = $this->db->query(
54
          "SELECT id, slug, title, description, search_string, front_page, image FROM topics WHERE front_page = TRUE"
55
      );
56
57
      $topics = array();
58
      $count = $q->rows();
59
60
      for ($i = 0; $i < $count; $i++ ) {
61
          $topic = $q->row($i);
62
          $topics[$topic['slug']] = new Topic($topic);
63
      }
64
      return $topics;
65
    }
66
67
    public function updateFrontPageTopics($topics) {
68
        // PDO doesn't cope with arrays so we have to do this by hand :|
69
        $quoted = array();
70
        foreach ($topics as $topic) {
71
            $quoted[] = $this->db->quote($topic);
72
        }
73
        $topics_str = implode(',', $quoted);
74
75
        $q = $this->db->query("UPDATE topics SET front_page = TRUE WHERE slug IN ($topics_str)");
0 ignored issues
show
Unused Code introduced by
$q is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
        $q = $this->db->query("UPDATE topics SET front_page = FALSE WHERE slug NOT IN ($topics_str)");
0 ignored issues
show
Unused Code introduced by
$q is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
79
        return true;
80
    }
81
}
82