Completed
Push — master ( 10b90e...3f5203 )
by David
09:22
created

Page::packagePage()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 25
c 2
b 0
f 2
rs 8.8571
cc 2
eloc 17
nc 2
nop 1
1
<?php namespace App\LaravelRestCms\Page;
2
3
use App\LaravelRestCms\BaseModel;
4
use App\LaravelRestCms\Page\PageDetail;
5
use App\LaravelRestCms\Template\Template;
6
7
class Page extends BaseModel {
8
9
	public static $searchCols = ['nav_name', 'url', 'title'];
10
11
	/**
12
	 * The database table used by the model.
13
	 *
14
	 * @var string
15
	 */
16
	protected $table = 'pages';
17
18
	/**
19
	 * The attributes that are mass assignable.
20
	 *
21
	 * @var array
22
	 */
23
	protected $fillable = ['parent_id', 'template_id', 'nav_name', 'url', 'title', 'created_by', 'updated_by'];
24
25
	/**
26
	 * The attributes excluded from the model's JSON form.
27
	 *
28
	 * @var array
29
	 */
30
	protected $hidden = [];
31
32
	/**
33
	 * Rules to validate when creating a model
34
	 * 
35
	* @var array
36
	 */
37
	protected static $createRules = [
38
		'parent_id' => 'integer',
39
		'template_id' => 'integer',
40
		'nav_name' => 'required',
41
		'url' => 'required|unique:pages',
42
		'title' => 'required',
43
		'created_by' => 'integer',
44
		'updated_by' => 'integer',
45
	];
46
47
	/**
48
	 * Indicates if the model should be attributed with created_by and updated_by
49
	 * 
50
	* @var bool
51
	 */
52
	public $attirbution = true;
53
54
	/**
55
	 * Joins the page_detail table
56
	 * 
57
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
58
	 */
59
	public function detail()
60
    {
61
        return $this->hasMany(PageDetail::class, 'page_id', 'id');
62
    }
63
64
	/**
65
	 * Joins the templates table
66
	 * 
67
	 * @return \Illuminate\Database\Eloquent\Relations\hasOne
68
	 */
69
	public function template()
70
    {
71
        return $this->hasMany(Template::class, 'id', 'template_id');
72
    }
73
74
	/**
75
	 * Joins the pages table
76
	 * 
77
	 * @return \Illuminate\Database\Eloquent\Relations\hasOne
78
	 */
79
	public function parent()
80
    {
81
        return $this->hasMany(Page::class, 'id', 'parent_id');
82
    }
83
84
    /**
85
     * Returns a page and associated detail and template data
86
     * 
87
     * @param  string $slug
88
     * @return array
89
     */
90
    public function showBySlug($slug)
91
    {        
92
        $data = Page::where(['url' => $slug])->with('template', 'detail.templateDetail')->firstOrFail();
93
        
94
        return $this->packagePage($data);
95
    }
96
97
    /**
98
     * Pacages a Page collection into an array for public consumption
99
     * 
100
     * @param  Page   $data 
101
     * @return array
102
     */
103
    protected function packagePage(Page $data)
104
    {
105
    	$template = $data->template->first();
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<App\LaravelRestCms\Page\Page>. 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...
106
    	
107
    	$page = [
108
        	'nav_name' => $data->nav_name,
0 ignored issues
show
Documentation introduced by
The property nav_name does not exist on object<App\LaravelRestCms\Page\Page>. 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...
109
        	'url' => $data->url,
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<App\LaravelRestCms\Page\Page>. 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...
110
        	'title' => $data->title,
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\LaravelRestCms\Page\Page>. 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...
111
        	'vars' => [],
112
        	'template' => [
113
        		'name' => $template->name,
114
	            'class' => $template->class,
115
	            'method' => $template->method,
116
	            'params' => $template->params,
117
	            'template_name' => $template->template_name,
118
	            'layout' => $template->layout,
119
        	]
120
        ];
121
122
        foreach ($data->detail as $detail) {
0 ignored issues
show
Documentation introduced by
The property detail does not exist on object<App\LaravelRestCms\Page\Page>. 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...
123
        	$page['vars'][$detail->templateDetail->first()->var] = $detail->data;
124
        }
125
126
        return $page;
127
    }
128
}
129