Completed
Push — master ( e2e30a...b18a5e )
by Konstantinos
04:00
created

Page::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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;
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;
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, 's');
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, 's');
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(), 's');
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
    public static function addPage($title, $content, $authorID, $status = "live")
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
        ), 'sssiis', 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
     * Get a list of enabled pages
206
     * @return Page[] A list of Page IDs
207
     */
208 1
    public static function getPages()
209
    {
210 1
        return self::arrayIdToModel(
211 1
            parent::fetchIdsFrom("status", array("live"), "s")
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchIdsFrom() instead of getPages()). Are you sure this is correct? If so, you might want to change this to $this->fetchIdsFrom().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
212
        );
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 1
    protected static function getDisallowedAliases()
219
    {
220 1
        return array("bans", "index", "login", "logout", "matches",
221
                     "messages", "news", "notifications", "pages",
222
                     "players", "servers", "teams");
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 1
    public static function getActiveStatuses()
229
    {
230 1
        return array('live', 'revision');
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 1
    public static function getEagerColumns()
237
    {
238 1
        return 'id,parent_id,name,alias,author,home,status';
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public static function getLazyColumns()
245
    {
246
        return 'content,created,updated';
247
    }
248
249
    /**
250
     * Get a query builder for pages
251
     * @return QueryBuilder
252
     */
253 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...
254
    {
255
        return new QueryBuilder('Page', array(
256
            'columns' => array(
257
                'name'   => 'name',
258
                'status' => 'status'
259
            ),
260
            'name' => 'name'
261
        ));
262
    }
263
264
    /**
265
     * Get the home page
266
     * @return Page
267
     */
268
    public static function getHomePage()
269
    {
270
        return self::get(parent::fetchIdFrom(1, "home"));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fetchIdFrom() instead of getHomePage()). Are you sure this is correct? If so, you might want to change this to $this->fetchIdFrom().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
271
    }
272
}
273