ChangeLog::getShortLogMessage()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 92
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
eloc 88
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 92
rs 8.2617

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace AL\Common\Model\Database;
3
4
/**
5
 * Maps to the `change_log` table
6
 */
7
class ChangeLog {
8
    const ACTION_UPDATE = "Update";
9
    const ACTION_INSERT = "Insert";
10
    const ACTION_DELETE = "Delete";
11
    const ACTION_DELETE_SHOT = "Delete shot";
12
13
    /** Permitted DB actions */
14
    const ACTIONS = array(self::ACTION_UPDATE, self::ACTION_INSERT, self::ACTION_DELETE, self::ACTION_DELETE_SHOT);
15
16
    /** List of permitted sections and sub-sections */
17
    const SECTIONS = array(
18
        "Articles" => array("Article"),
19
        "Author type" => array("Author type"),
20
        "Bug type" => array("Bug type"),
21
        "Company" => array("Company", "Logo"),
22
        "Downloads" => array("Crew", "Details", "TOS"),
23
        "Format" => array("Format"),
24
        "Game series" => array("Game", "Series"),
25
        "Games" => array("AKA", "Box back", "Box front", "Comment","Creator",
26
            "Developer", "Fact", "File", "Mag score", "Music","Game", "Publisher",
27
            "Review", "Review comment", "Screenshot", "Similar", "Submission", "Year",
28
            "Release", "Sound hardware", "Video", "Vs"
29
        ),
30
        "Game Release" => array("Scan"),
31
        "Individuals" => array("Image", "Individual", "Nickname"),
32
        "Interviews" => array("Comment", "Interview", "Screenshots"),
33
        "Lingo" => array("Lingo"),
34
        "Links" => array("Category", "Link", "Link submit"),
35
        "Links cat" => array("Category"),
36
        "Menu disk" => array("Game", "Menu disk"),
37
        "Menu set" => array("Menu disk (multiple)", "Menu set", "Menu type"),
38
        "Menu type" => array("Menu type"),
39
        "News" => array("Image", "News item", "News submit"),
40
        "Reviews" => array("Comment"),
41
        "TOS" => array("TOS"),
42
        "Trivia" => array("DYK", "Quote", "Spotlight"),
43
        "Users" => array("Avatar", "User")
44
    );
45
46
    private $id;
47
    private $section;
48
    private $section_id;
49
    private $section_value;
50
    private $sub_section;
51
    private $sub_section_id;
52
    private $sub_section_value;
53
    private $user_id;
54
    private $action;
55
    private $timestamp;
56
    private $user;
57
58
    public function __construct(
59
        $id,
60
        $section,
61
        $section_id,
62
        $section_value,
63
        $sub_section,
64
        $sub_section_id,
65
        $sub_section_value,
66
        $user_id,
67
        $action,
68
        $timestamp = null
69
    ) {
70
        $this->id = $id;
71
72
        // Check if the section is valid
73
        if (! array_key_exists($section, self::SECTIONS)) {
74
            die("Unknown section '$section'. Only "
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
75
                .join(", ", array_keys(self::SECTIONS))." are supported");
76
        }
77
        $this->section = $section;
78
        $this->section_id = $section_id;
79
        $this->section_value = $section_value;
80
81
        // Check is the sub-section is valid for the section
82
        if (! in_array($sub_section, self::SECTIONS[$section])) {
83
            die("Unknown sub-section '$sub_section'. Only "
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
84
                .join(", ", self::SECTIONS[$section])." are supported for $section");
85
        }
86
        $this->sub_section = $sub_section;
87
        $this->sub_section_id = $sub_section_id;
88
        $this->sub_section_value = $sub_section_value;
89
90
        $this->user_id = $user_id;
91
92
        // Check if the action is valid
93
        if (! in_array($action, self::ACTIONS)) {
94
            die("Unknown action '$action'. Only ".self::ACTIONS." are supported");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug introduced by
Are you sure self::ACTIONS of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            die("Unknown action '$action'. Only "./** @scrutinizer ignore-type */ self::ACTIONS." are supported");
Loading history...
95
        }
96
        $this->action = $action;
97
98
        if ($timestamp == null) {
99
            $this->timestamp = time();
100
        } else {
101
            $this->timestamp = $timestamp;
102
        }
103
    }
104
105
    public function getId() {
106
        return $this->id;
107
    }
108
109
    public function getSection() {
110
        return $this->section;
111
    }
112
113
    public function getSectionId() {
114
        return $this->section_id;
115
    }
116
117
    public function getSectionValue() {
118
        return $this->section_value;
119
    }
120
121
    public function getSubSection() {
122
        return $this->sub_section;
123
    }
124
125
    public function getSubSectionId() {
126
        return $this->sub_section_id;
127
    }
128
129
    public function getSubSectionValue() {
130
        return $this->sub_section_value;
131
    }
132
133
    public function getUserId() {
134
        return $this->user_id;
135
    }
136
137
    public function getAction() {
138
        return $this->action;
139
    }
140
141
    public function getTimestamp() {
142
        return $this->timestamp;
143
    }
144
145
    public function getUser() {
146
        return $this->user;
147
    }
148
149
    public function setUser($user) {
150
        $this->user = $user;
151
    }
152
153
    public function getShortLogMessage() {
154
        $section_name = $this->section_value;
155
        $sub_section_name = $this->sub_section_value;
156
        $messages = array(
157
            "Games" => array(
158
                "AKA" => array(
159
                    "Update" => "Updated $section_name",
160
                    "Insert" => "Added $sub_section_name to $section_name",
161
                    "Delete" => "Removed $sub_section_name from $section_name"),
162
                "Box back" => array(
163
                    "Update" => "Updated the boxscan of $section_name",
164
                    "Insert" => "Added boxscan to $section_name",
165
                    "Delete" =>"Removed a boxscan from $section_name"),
166
                "Box front" => array(
167
                    "Update" => "Updated the boxscan of $section_name",
168
                    "Insert" => "Added boxscan to $section_name",
169
                    "Delete" => "Removed a boxscan from $section_name"),
170
                "Comment" => array(
171
                    "Update" => "Updated a comment on $section_name",
172
                    "Insert" => "Added a comment on $section_name",
173
                    "Delete" => "Removed a comment on $section_name"),
174
                "Creator" => array(
175
                    "Update" => "Updated $section_name",
176
                    "Insert" => "Added $sub_section_name to $section_name",
177
                    "Delete" => "Removed $sub_section_name from $section_name"),
178
                "Developer" => array(
179
                    "Update" => "Updated the developer of $section_name",
180
                    "Insert" => "Added $sub_section_name to $section_name",
181
                    "Delete" => "Removed $sub_section_name from $section_name"),
182
                "Fact" => array(
183
                    "Update" => "Updated $section_name",
184
                    "Insert" => "Added fact to $section_name",
185
                    "Delete" =>"Removed a fact from $section_name",
186
                    "Delete shot" => "Removed a fact shot from $section_name"),
187
                "File" => array(
188
                    "Update" => "Updated $section_name",
189
                    "Insert" => "Added a file to $section_name",
190
                    "Delete" =>"Removed a file from $section_name"),
191
                "Mag score" => array(
192
                    "Update" => "Updated $section_name",
193
                    "Insert" => "Added a mag score to $section_name",
194
                    "Delete" =>"Removed a mag score from $section_name"),
195
                "Music" => array(
196
                    "Update" => "Updated $section_name",
197
                    "Insert" => "Added music to $section_name",
198
                    "Delete" =>"Removed music from $section_name"),
199
                "Game" => array(
200
                    "Update" => "Updated $section_name",
201
                    "Insert" => "Added a new game: $section_name",
202
                    "Delete" => "Removed $section_name"),
203
                "Publisher" => array(
204
                    "Update" => "Updated the publisher of $section_name",
205
                    "Insert" => "Added $sub_section_name to $section_name",
206
                    "Delete" => "Removed $sub_section_name from $section_name"),
207
                "Review" => array(
208
                    "Update" => "Updated a review of $sub_section_name",
209
                    "Insert" => "Added a review to $section_name",
210
                    "Delete" => "Removed a review from $section_name"),
211
                "Release" => array(
212
                    "Update" => "Updated a release of $section_name",
213
                    "Insert" => "Added a release to $section_name",
214
                    "Delete" => "Removed a release from $section_name"),
215
                "Screenshot" => array(
216
                    "Update" => "Updated the screenshots of $section_name",
217
                    "Insert" => "Added a screenshot to $section_name",
218
                    "Delete" => "Removed a screenshot from $section_name"),
219
                "Similar" => array(
220
                    "Update" => "Updated $section_name",
221
                    "Insert" => "Added $sub_section_name as similar to $section_name",
222
                    "Delete" => "Updated $section_name"),
223
                "Sound hardware" => array(
224
                    "Update" => "Updated $section_name",
225
                    "Insert" => "Added sound hardware to $section_name",
226
                    "Delete" => "Updated $section_name"),
227
                "Submission" => array(
228
                    "Update" => "Updated info submission for $section_name",
229
                    "Insert" => "Submitted info for $section_name",
230
                    "Delete" => "Removed an info submission for $section_name"),
231
                "Vs" => array(
232
                    "Update" => "Updated versus info for $section_name",
233
                    "Insert" => "Submitted versus info for $section_name",
234
                    "Delete" => "Removed a versus info for $section_name"),
235
            )
236
        );
237
238
        if (array_key_exists($this->section, $messages)
239
            && array_key_exists($this->sub_section, $messages[$this->section])
240
            && array_key_exists($this->action, $messages[$this->section][$this->sub_section])) {
241
            return $messages[$this->section][$this->sub_section][$this->action];
242
        } else {
243
            return "ERROR: Message missing in ChangeLog.php for section {$this->section},
244
                sub-section {$this->sub_section}, action {$this->action}";
245
        }
246
    }
247
}
248