Completed
Push — dev-master ( 6012ae...775d5b )
by Vijay
03:29
created

Page::page()   C

Complexity

Conditions 11
Paths 43

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nc 43
nop 2
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

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
3
namespace FFCMS\Controllers;
4
5
use FFCMS\{Mappers};
6
7
8
/**
9
 * Index Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright (c) Copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class Page extends Base
16
{
17
    protected $template_path = 'pages/';
18
19
    /**
20
     * Render a published page unless not published yet
21
     *
22
     * @param \Base $f3
23
     * @param array $params
24
     * @return void
25
     */
26
    public function page(\Base $f3, array $params = [])
27
    {
28
        if (array_key_exists('slug', $params)) {
29
            $slug = $params['slug'];
30
        } elseif (preg_match('/^\/(?P<lang>[^\/]+)\/(?P<slug>.+)/', $f3->get('PATH'), $matches)) {
31
            $slug = $matches['slug'];
32
        } else {
33
            // 404
34
            echo 'missing slug';
35
            die(404);
36
        }
37
38
        if (empty($slug)) {
39
            // 404
40
            echo 'page does not exist';
41
            die(404);
42
        }
43
44
        $page = new Mappers\Pages;
45
        $page->load(['slug = ?', $slug]);
46
47
        // conditions if page is viewable
48
        $publishTime = strtotime($page->published);
49
        $expireTime = strtotime($page->expires);
0 ignored issues
show
Documentation introduced by
The property expires does not exist on object<FFCMS\Mappers\Pages>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
        $showPage = $f3->get('REQUEST.preview') || (
51
            'published' == $page->status &&
52
            'public' == $page->scopes &&
53
            'page' == $page->category &&
54
            time() > $publishTime &&
55
            (0 >= $expireTime || $expireTime > time())
56
        );
57
58
        if (!$showPage) {
59
            // 404
60
            echo 'page unavailable';
61
            die(404);
62
        }
63
64
        $f3->set('pagesMapper', $page);
65
66
        echo \View::instance()->render($this->template_path . '/page.phtml');
67
    }
68
}
69