Passed
Push — 0.3.0 ( 0d73f1...9a02d7 )
by Anton
04:10
created

Page::updateSlugs()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 42
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
rs 8.439
cc 6
eloc 18
nc 4
nop 0
1
<?php
2
3
namespace Modules\Entitizer\Entity {
4
5
	use Modules\Auth, Modules\Entitizer, DB;
6
7
	class Page extends Entitizer\Utils\Entity {
8
9
		use Entitizer\Common\Page;
10
11
		# Init by slug
12
13
		public function initBySlug(string $slug) {
14
15
			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...
16
17
			# Process value
18
19
			$slug = $this->definition->param('slug')->cast($slug);
20
21
			# Process selection
22
23
			$selection = array_keys($this->definition->params());
24
25
			foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field);
26
27
			# Process query
28
29
			$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") .
30
31
			         ("FROM " . static::$table . " ent ") .
32
33
			         ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
34
35
			         ("WHERE ent.visibility = " . VISIBILITY_PUBLISHED . " AND ent.access <= " . Auth::user()->rank . " AND ") .
36
37
			         ("ent.locked = 0 AND ent.slug = '" . addslashes($slug) . "' LIMIT 1");
38
39
			# Select entity from DB
40
41
			if (!(DB::send($query) && (DB::last()->rows === 1))) return false;
42
43
			# ------------------------
44
45
			return $this->setData(DB::last()->row());
46
		}
47
48
		# Update slugs
49
50
		public function updateSlugs() {
51
52
			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...
53
54
			# Send lock/update request
55
56
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
57
58
			         ("SELECT rel.descendant as id, GROUP_CONCAT(ens.name ORDER BY rls.depth DESC SEPARATOR '/') slug ") .
59
60
			         ("FROM " . static::$table_relations . " rel ") .
61
62
			         ("JOIN " . static::$table_relations . " rls ON rls.descendant = rel.descendant ") .
63
64
			         ("JOIN " . static::$table . " ens ON ens.id = rls.ancestor ") .
65
66
			         ("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...
67
68
			         (") slg ON slg.id = ent.id SET ent.locked = 1, ent.slug = slg.slug");
69
70
			if (!(DB::send($query) && DB::last()->status)) return false;
71
72
			# Send unlock request
73
74
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
75
76
			         ("SELECT rel.descendant as id FROM " . static::$table_relations . " rel ") .
77
78
			         ("JOIN " . static::$table . " enc ON enc.id = rel.descendant AND enc.locked = 1 ") .
79
80
			         ("LEFT JOIN " . static::$table . " end ON end.id != enc.id AND end.slug = enc.slug ") .
81
82
			         ("WHERE end.id IS NULL GROUP BY rel.descendant") .
83
84
			         (") chk ON chk.id = ent.id SET ent.locked = 0");
85
86
			if (!(DB::send($query) && DB::last()->status)) return false;
87
88
			# ------------------------
89
90
			return true;
91
		}
92
93
		# Create page entry in DB
94
95
		public function create(array $data) {
96
97
			if (!parent::create($data)) return false;
98
99
			$this->updateSlugs();
100
101
			# ------------------------
102
103
			return true;
104
		}
105
106
		# Edit page entry in DB
107
108
		public function edit(array $data) {
109
110
			if (!parent::edit($data)) return false;
111
112
			$this->updateSlugs();
113
114
			# ------------------------
115
116
			return true;
117
		}
118
119
		# Move page to new parent
120
121
		public function move(int $parent_id) {
122
123
			if (!parent::move($parent_id)) return false;
124
125
			$this->updateSlugs();
126
127
			# ------------------------
128
129
			return true;
130
		}
131
	}
132
}
133