Completed
Push — feature/player-elo ( 8ea10f...365309 )
by Vladimir
10:20
created

Page   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 262
Duplicated Lines 11.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 29
loc 262
ccs 44
cts 68
cp 0.6471
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A assignResult() 8 8 1
A getStatus() 0 4 1
A isHomePage() 0 4 1
A getRouteName() 0 4 1
A getActiveStatuses() 0 4 1
A assignLazyResult() 0 6 1
A getCreated() 0 6 1
A setStatus() 0 4 1
A updateEditTimestamp() 0 4 1
A addPage() 11 11 1
A getDisallowedAliases() 0 8 1
A getLazyColumns() 0 4 1
A getHomePage() 0 4 1
A getEagerColumns() 0 14 1
A getQueryBuilder() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file contains functionality relating to the custom pages that admins can great
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A custom page
11
 * @package    BZiON\Models
12
 */
13
class Page extends AliasModel
14
{
15
    /**
16
     * The content of the page
17
     * @var string
18
     */
19
    protected $content;
20
21
    /**
22
     * The creation date of the page
23
     * @var TimeDate
24
     */
25
    protected $created;
26
27
    /**
28
     * The date the page was last updated
29
     * @var TimeDate
30
     */
31
    protected $updated;
32
33
    /**
34
     * The ID of the author of the page
35
     * @var int
36
     */
37
    protected $author;
38
39
    /**
40
     * Whether the page is the home page
41
     * @var bool
42
     */
43
    protected $home;
44
45
    /**
46
     * The status of the page
47
     * @var string
48
     */
49
    protected $status;
50
51
    /**
52
     * The name of the database table used for queries
53
     */
54
    const TABLE = "pages";
55
56
    const CREATE_PERMISSION = Permission::CREATE_PAGE;
57
    const EDIT_PERMISSION = Permission::EDIT_PAGE;
58
    const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_PAGE;
59
    const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_PAGE;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1 View Code Duplication
    protected function assignResult($page)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66 1
        $this->name = $page['name'];
67 1
        $this->alias = $page['alias'];
68 1
        $this->author = $page['author'];
69 1
        $this->home = $page['home'];
70 1
        $this->status = $page['status'];
71 1
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function assignLazyResult($page)
77
    {
78
        $this->content = $page['content'];
79
        $this->created = TimeDate::fromMysql($page['created']);
80
        $this->updated = TimeDate::fromMysql($page['updated']);
81
    }
82
83
    /**
84
     * Get the raw content of the page
85
     * @return string
86
     */
87
    public function getContent()
88
    {
89
        $this->lazyLoad();
90
91
        return $this->content;
92
    }
93
94
    /**
95
     * Get the page's submission time
96
     * @return TimeDate
97
     */
98
    public function getCreated()
99
    {
100
        $this->lazyLoad();
101
102
        return $this->created->copy();
103
    }
104
105
    /**
106
     * Get the time when the page was last updated
107
     * @return TimeDate
108
     */
109
    public function getUpdated()
110
    {
111
        $this->lazyLoad();
112
113
        return $this->updated->copy();
114
    }
115
116
    /**
117
     * Get the user who created the page
118
     * @return Player The page's author
119
     */
120
    public function getAuthor()
121
    {
122
        return Player::get($this->author);
123
    }
124
125
    /**
126
     * Get the status of the page
127
     * @return string
128
     */
129 1
    public function getStatus()
130
    {
131 1
        return $this->status;
132
    }
133
134
    /**
135
     * Find out whether this is the homepage
136
     * @return bool
137
     */
138 1
    public function isHomePage()
139
    {
140 1
        return $this->home;
141
    }
142
143
    /**
144
     * Set the content of the page
145
     *
146
     * @param  string $content
147
     * @return self
148
     */
149
    public function setContent($content)
150
    {
151
        return $this->updateProperty($this->content, "content", $content);
152
    }
153
154
    /**
155
     * Set the status of the page
156
     *
157
     * @param  string $status One of "live", "revision" or "disabled"
158
     * @return self
159
     */
160
    public function setStatus($status)
161
    {
162
        return $this->updateProperty($this->status, "status", $status);
163
    }
164
165
    /**
166
     * Update the last edit timestamp
167
     * @return self
168
     */
169
    public function updateEditTimestamp()
170
    {
171
        return $this->updateProperty($this->updated, "updated", TimeDate::now());
172
    }
173
174
    /**
175
     * Create a new Page
176
     *
177
     * @param string $title    The title of the page
178
     * @param string $content  The content of page
179
     * @param int    $authorID The ID of the author
180
     * @param string $status   Page status: 'live','disabled',or 'deleted'
181
     *
182
     * @return Page An object representing the page that was just created
183
     */
184 1 View Code Duplication
    public static function addPage($title, $content, $authorID, $status = "live")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186 1
        return self::create(array(
187 1
            'name'    => $title,
188 1
            'alias'   => self::generateAlias($title),
189 1
            'content' => $content,
190 1
            'author'  => $authorID,
191 1
            'home'    => 0,
192 1
            'status'  => $status,
193 1
        ), array('created', 'updated'));
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public static function getRouteName($action = 'show')
200
    {
201 1
        return "custom_page_$action";
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1
    protected static function getDisallowedAliases()
208
    {
209
        return array(
210 1
            "admin", "bans", "index", "login", "logout", "maps", "matches",
211 1
            "messages", "news", "notifications", "pages", "players", "servers",
212 1
            "teams", "visits"
213 1
        );
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 1
    public static function getActiveStatuses()
220
    {
221 1
        return array('live', 'revision');
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 1
    public static function getEagerColumns($prefix = null)
228
    {
229
        $columns = [
230 1
            'id',
231 1
            'parent_id',
232 1
            'name',
233 1
            'alias',
234 1
            'author',
235 1
            'home',
236 1
            'status',
237 1
        ];
238
239 1
        return self::formatColumns($prefix, $columns);
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public static function getLazyColumns()
246
    {
247
        return 'content,created,updated';
248
    }
249
250
    /**
251
     * Get a query builder for pages
252
     * @return QueryBuilder
253
     */
254 1 View Code Duplication
    public static function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
    {
256 1
        return new QueryBuilder('Page', array(
257
            'columns' => array(
258 1
                'name'   => 'name',
259
                'status' => 'status'
260 1
            ),
261
            'name' => 'name'
262 1
        ));
263
    }
264
265
    /**
266
     * Get the home page
267
     * @deprecated
268
     * @return Page
269
     */
270
    public static function getHomePage()
271
    {
272
        return self::get(self::fetchIdFrom(1, "home"));
273
    }
274
}
275