Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Page::initBySlug()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 34
Code Lines 12

Duplication

Lines 1
Ratio 2.94 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 1
loc 34
rs 5.6923
cc 7
eloc 12
nc 5
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Entity {
4
5
	use Modules\Auth, Modules\Entitizer, Modules\Settings, DB;
6
7
	class Page extends Entitizer\Utils\Entity {
8
9
		use Entitizer\Common\Page;
10
11
		# Check if active
12
13
		private function getActive() {
14
15
			return (($this->visibility === VISIBILITY_PUBLISHED) && !$this->locked);
0 ignored issues
show
Documentation introduced by
The property visibility 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...
Documentation introduced by
The property locked 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
18
		# Get link
19
20
		private function getLink() {
21
22
			if ('' === $this->data['slug']) return '';
23
24
			return (INSTALL_PATH . '/' . $this->data['slug']);
25
		}
26
27
		# Get canonical
28
29
		private function getCanonical() {
30
31
			if ('' === $this->data['slug']) return '';
32
33
			return (Settings::get('system_url') . (($this->id !== 1) ? ('/' . $this->data['slug']) : ''));
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...
34
		}
35
36
		# Implement entity
37
38
		protected function implement() {
39
40
			$this->data['active'] = $this->getActive();
41
42
			$this->data['link'] = $this->getLink();
43
44
			$this->data['canonical'] = $this->getCanonical();
45
		}
46
47
		# Init by slug
48
49
		public function initBySlug(string $slug) {
50
51
			if (!$this->modifiable || (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...
52
53
			# Process value
54
55
			$slug = $this->definition->param('slug')->cast($slug);
56
57
			# Process selection
58
59
			$selection = array_keys($this->definition->params());
60
61
			foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field);
62
63
			# Process query
64
65
			$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") .
66
67
			         ("FROM " . static::$table . " ent ") .
68
69
			         ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
70
71
			         ("WHERE ent.visibility = " . VISIBILITY_PUBLISHED . " AND ent.access <= " . Auth::user()->rank . " AND ") .
72
73
			         ("ent.locked = 0 AND ent.slug = '" . addslashes($slug) . "' LIMIT 1");
74
75
			# Select entity from DB
76
77 View Code Duplication
			if (($this->error = !(DB::send($query) && DB::last()->status)) || (DB::last()->rows !== 1)) return false;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
79
			# ------------------------
80
81
			return $this->setData(DB::last()->row());
82
		}
83
84
		# Update slugs
85
86
		public function updateSlugs() {
87
88
			if (!$this->modifiable || (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...
89
90
			# Send lock/update request
91
92
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
93
94
			         ("SELECT rel.descendant as id, GROUP_CONCAT(ens.name ORDER BY rls.depth DESC SEPARATOR '/') slug ") .
95
96
			         ("FROM " . static::$table_relations . " rel ") .
97
98
			         ("JOIN " . static::$table_relations . " rls ON rls.descendant = rel.descendant ") .
99
100
			         ("JOIN " . static::$table . " ens ON ens.id = rls.ancestor ") .
101
102
			         ("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...
103
104
			         (") slg ON slg.id = ent.id SET ent.locked = 1, ent.slug = slg.slug");
105
106
			if (!(DB::send($query) && DB::last()->status)) return false;
107
108
			# Send unlock request
109
110
			$query = ("UPDATE " . static::$table . " ent JOIN (") .
111
112
			         ("SELECT rel.descendant as id FROM " . static::$table_relations . " rel ") .
113
114
			         ("JOIN " . static::$table . " enc ON enc.id = rel.descendant AND enc.locked = 1 ") .
115
116
			         ("LEFT JOIN " . static::$table . " end ON end.id != enc.id AND end.slug = enc.slug ") .
117
118
			         ("WHERE end.id IS NULL GROUP BY rel.descendant") .
119
120
			         (") chk ON chk.id = ent.id SET ent.locked = 0");
121
122
			if (!(DB::send($query) && DB::last()->status)) return false;
123
124
			# ------------------------
125
126
			return true;
127
		}
128
129
		# Create page entry in DB
130
131
		public function create(array $data) {
132
133
			if (!parent::create($data)) return false;
134
135
			$this->updateSlugs();
136
137
			# ------------------------
138
139
			return true;
140
		}
141
142
		# Edit page entry in DB
143
144
		public function edit(array $data) {
145
146
			if (!parent::edit($data)) return false;
147
148
			$this->updateSlugs();
149
150
			# ------------------------
151
152
			return true;
153
		}
154
155
		# Move page to new parent
156
157
		public function move(int $parent_id) {
158
159
			if (!parent::move($parent_id)) return false;
160
161
			$this->updateSlugs();
162
163
			# ------------------------
164
165
			return true;
166
		}
167
	}
168
}
169