Passed
Pull Request — master (#1837)
by Struan
32:17 queued 03:52
created

Alert   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 16.28%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 68
c 2
b 0
f 1
dl 0
loc 104
ccs 7
cts 43
cp 0.1628
rs 10
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
B prettifyCriteria() 0 40 11
A detailsToCriteria() 0 13 3
A sectionToTitle() 0 8 2
A forUser() 0 36 4
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Utility;
4
5
/**
6
 * Alert Utilities
7
 *
8
 * Utility functions related to alerts
9
 */
10
11
class Alert {
12
    #XXX don't calculate this every time
13
    private static function sectionToTitle($section) {
14 3
        global $hansardmajors;
15 3
        $section_map = [];
16
        foreach ($hansardmajors as $major => $details) {
17 3
            $section_map[$details["page_all"]] = $details["title"];
18 3
        }
19
20
        return $section_map[$section];
21 3
    }
22
    public static function detailsToCriteria($details) {
23
        $criteria = [];
24
25 3
        if (!empty($details['keyword'])) {
26 3
            $criteria[] = $details['keyword'];
27
        }
28
29
        if (!empty($details['pid'])) {
30
            $criteria[] = 'speaker:' . $details['pid'];
31
        }
32
33
        $criteria = join(' ', $criteria);
34
        return $criteria;
35
    }
36
37
    public static function forUser($email) {
38
        $db = new \ParlDB();
39
        $q = $db->query('SELECT * FROM alerts WHERE email = :email
40
            AND deleted != 1 ORDER BY created', [
41
            ':email' => $email,
42
        ]);
43
44
        $alerts = [];
45
        foreach ($q as $row) {
46
            $criteria = self::prettifyCriteria($row['criteria']);
47
            $parts = self::prettifyCriteria($row['criteria'], true);
48
            $token = $row['alert_id'] . '-' . $row['registrationtoken'];
49
50
            $status = 'confirmed';
51
            if (!$row['confirmed']) {
52
                $status = 'unconfirmed';
53
            } elseif ($row['deleted'] == 2) {
54
                $status = 'suspended';
55
            }
56
57
            $alert = [
58
                'token' => $token,
59
                'status' => $status,
60
                'criteria' => $criteria,
61
                'raw' => $row['criteria'],
62
                'keywords' => [],
63
                'exclusions' => [],
64
                'sections' => [],
65
            ];
66
67
            $alert = array_merge($alert, $parts);
68
69
            $alerts[] = $alert;
70
        }
71
72
        return $alerts;
73
    }
74
75
    public static function prettifyCriteria($alert_criteria, $as_parts = false) {
76
        $text = '';
77
        if ($alert_criteria) {
78
            $criteria = explode(' ', $alert_criteria);
79
            $parts = [];
80
            $words = [];
81
            $sections = [];
82
            $sections_verbose = [];
83
            $spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria));
84
85
            foreach ($criteria as $c) {
86
                if (preg_match('#^section:(\w+)#', $c, $m)) {
87
                    $sections[] = $m[1];
88
                    $sections_verbose[] = self::sectionToTitle($m[1]);
89
                } elseif (!preg_match('#^speaker:(\d+)#', $c, $m)) {
90
                    $words[] = $c;
91
                }
92
            }
93
            if ($spokenby && count($words)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $spokenby of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
94
                $text = implode(' or ', $spokenby) . ' mentions [' . implode(' ', $words) . ']';
95
                $parts['spokenby'] = $spokenby;
96
                $parts['words'] = $words;
97
            } elseif (count($words)) {
98
                $text = '[' . implode(' ', $words) . ']' . ' is mentioned';
99
                $parts['words'] = $words;
100
            } elseif ($spokenby) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $spokenby of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
                $text = implode(' or ', $spokenby) . " speaks";
102
                $parts['spokenby'] = $spokenby;
103
            }
104
105
            if ($sections) {
106
                $text = $text . " in " . implode(' or ', $sections_verbose);
107
                $parts['sections'] = $sections;
108
                $parts['sections_verbose'] = $sections_verbose;
109
            }
110
        }
111
        if ($as_parts) {
112
            return $parts;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parts does not seem to be defined for all execution paths leading up to this point.
Loading history...
113
        }
114
        return $text;
115
    }
116
117
}
118