Passed
Push — master ( ae19d2...73e9f3 )
by
unknown
05:05
created

PolicyComparisonPeriod::lslug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
/**
4
 * Party Cohort Class
5
 *
6
 * @package TheyWorkForYou
7
 */
8
9
namespace MySociety\TheyWorkForYou;
10
11
/**
12
 * PolicyComparisonPeriod
13
 * This class handles the comparison period for a policy
14
 */
15
16
class PolicyComparisonPeriod {
17
    public int $id;
18
    public string $slug;
19
    public string $description;
20
    public string $start_date;
21
    public string $end_date;
22
    public int $chamber_id;
23
24
25
    public function lslug(): string {
26
        return strtolower($this->slug);
27
    }
28
29
    /**
30
     * Get periods we have information for a given house and person.
31
     *
32
     * @param int $house
33
     * @return PolicyComparisonPeriod[]
34
     */
35
    public static function getComparisonPeriodsForPerson(int $person_id, int $house): array {
36
        $db = new \ParlDB();
37
        $sql = "SELECT DISTINCT
38
                    pp.slug as slug
39
                FROM policyvotedistribution pd
40
                JOIN policycomparisonperiod pp ON pd.period_id = pp.id
41
                WHERE pd.person_id = :person_id
42
                AND pd.chamber_id = :house
43
        ";
44
        $params = ['house' => $house, 'person_id' => $person_id];
45
        $rows = $db->query($sql, $params);
46
        $periods = [];
47
        foreach ($rows as $row) {
48
            $periods[] = new PolicyComparisonPeriod($row['slug'], $house);
49
        }
50
        return $periods;
51
52
    }
53
54
    public function __construct(string $period_slug, int $house) {
55
56
        $db = new \ParlDB();
57
        // need to join to policyorganisationsto get the party_id
58
        // need to join to policycomparisonperiod to get slug to id
59
        $sql = "SELECT * FROM policycomparisonperiod where slug = :period_slug and chamber_id = :house";
60
61
        $params = ['period_slug' => $period_slug, 'house' => $house];
62
63
        $row = $db->query($sql, $params)->first();
64
        if (!$row) {
65
            throw new \Exception("PolicyComparisonPeriod not found for slug: $period_slug and house: $house");
66
        }
67
68
        $this->id = (int) $row['id'];
69
        $this->slug = $row['slug'];
70
        $this->description = $row['description'];
71
        $this->start_date = $row['start_date'];
72
        $this->end_date = $row['end_date'];
73
        $this->chamber_id = (int) $row['chamber_id'];
74
    }
75
}
76