Page   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
B initBySlug() 0 34 5
B updateSlugs() 0 42 6
A create() 0 10 2
A edit() 0 10 2
A move() 0 10 2
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Entitizer\Entity {
11
12
	use Modules\Auth, Modules\Entitizer, DB;
13
14
	class Page extends Entitizer\Utils\Entity {
15
16
		use Entitizer\Common\Page;
17
18
		/**
19
 		 * Initialize the page by a slug
20
		 *
21
		 * @return bool : true on success or false on failure
22
		 */
23
24
		public function initBySlug(string $slug) : bool {
25
26
			if (0 !== $this->id) return false;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Entity\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...
27
28
			# Process value
29
30
			$slug = $this->definition->getParam('slug')->cast($slug);
31
32
			# Process selection
33
34
			$selection = array_keys($this->definition->getParams());
35
36
			foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field);
37
38
			# Process query
39
40
			$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") .
41
42
			         ("FROM " . static::$table . " ent ") .
43
44
			         ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
45
46
			         ("WHERE ent.visibility = " . VISIBILITY_PUBLISHED . " AND ent.access <= " . Auth::get('rank') . " AND ") .
47
48
			         ("ent.locked = 0 AND ent.slug = '" . addslashes($slug) . "' LIMIT 1");
49
50
			# Select entity from DB
51
52
			if (!(DB::send($query) && (DB::getLast()->rows === 1))) return false;
53
54
			# ------------------------
55
56
			return $this->setData(DB::getLast()->getRow());
57
		}
58
59
		/**
60
 		 * Update slugs of the page descendants
61
		 *
62
		 * @return bool : true on success or false on failure
63
		 */
64
65
		public function updateSlugs() : bool {
66
67
			if (0 === $this->id) return false;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Entity\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...
68
69
			# Send lock/update request
70
71
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
72
73
			         ("SELECT rel.descendant as id, GROUP_CONCAT(ens.name ORDER BY rls.depth DESC SEPARATOR '/') slug ") .
74
75
			         ("FROM " . static::$table_relations . " rel ") .
76
77
			         ("JOIN " . static::$table_relations . " rls ON rls.descendant = rel.descendant ") .
78
79
			         ("JOIN " . static::$table . " ens ON ens.id = rls.ancestor ") .
80
81
			         ("WHERE rel.ancestor = " . $this->id . " GROUP BY rel.descendant") .
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Entity\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...
82
83
			         (") slg ON slg.id = ent.id SET ent.locked = 1, ent.slug = slg.slug");
84
85
			if (!(DB::send($query) && DB::getLast()->status)) return false;
86
87
			# Send unlock request
88
89
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
90
91
			         ("SELECT rel.descendant as id FROM " . static::$table_relations . " rel ") .
92
93
			         ("JOIN " . static::$table . " enc ON enc.id = rel.descendant AND enc.locked = 1 ") .
94
95
			         ("LEFT JOIN " . static::$table . " end ON end.id != enc.id AND end.slug = enc.slug ") .
96
97
			         ("WHERE end.id IS NULL GROUP BY rel.descendant") .
98
99
			         (") chk ON chk.id = ent.id SET ent.locked = 0");
100
101
			if (!(DB::send($query) && DB::getLast()->status)) return false;
102
103
			# ------------------------
104
105
			return true;
106
		}
107
108
		/**
109
		 * Create the page entry in DB
110
		 *
111
		 * @return bool : true on success or false on failure
112
		 */
113
114
		public function create(array $data) : bool {
115
116
			if (!parent::create($data)) return false;
117
118
			$this->updateSlugs();
119
120
			# ------------------------
121
122
			return true;
123
		}
124
125
		/**
126
		 * Edit the page entry in DB
127
		 *
128
		 * @return bool : true on success or false on failure
129
		 */
130
131
		public function edit(array $data) : bool {
132
133
			if (!parent::edit($data)) return false;
134
135
			$this->updateSlugs();
136
137
			# ------------------------
138
139
			return true;
140
		}
141
142
		/**
143
		 * Move the page to a new parent
144
		 *
145
		 * @return bool : true on success or false on failure
146
		 */
147
148
		public function move(int $parent_id) : bool {
149
150
			if (!parent::move($parent_id)) return false;
151
152
			$this->updateSlugs();
153
154
			# ------------------------
155
156
			return true;
157
		}
158
	}
159
}
160