Test Failed
Push — master ( aacb55...0d48f6 )
by Andreas
19:39
created

name_is_clean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper.reflector
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Helper class for object name handling
11
 *
12
 * @package midcom.helper.reflector
13
 */
14
class midcom_helper_reflector_nameresolver
15
{
16
    /**
17
     * The object we're working with
18
     *
19
     * @var midcom_core_dbaobject
20
     */
21
    private $_object;
22
23 140
    public function __construct($object)
24
    {
25 140
        $this->_object = $object;
26 140
    }
27
28
    /**
29
     * Resolves the "name" of given object
30
     *
31
     * @param string $name_property property to use as "name", if left to default (null), will be reflected
32
     * @return string value of name property or null on failure
33
     */
34 140
    public function get_object_name(string $name_property = null) : ?string
35
    {
36 140
        if ($name_property === null) {
37 140
            $name_property = midcom_helper_reflector::get_name_property($this->_object);
38
        }
39 140
        if (    empty($name_property)
40 140
            || !midcom_helper_reflector::get($this->_object)->property_exists($name_property)) {
41
            // Could not resolve valid property
42 1
            return null;
43
        }
44 140
        return $this->_object->{$name_property};
45
    }
46
47
    /**
48
     * Checks for "clean" URL name
49
     *
50
     * @see http://trac.midgard-project.org/ticket/809
51
     * @param string $name_property property to use as "name", if left to default (null), will be reflected
52
     */
53 1
    public function name_is_clean(string $name_property = null) : bool
54
    {
55 1
        if ($name_copy = $this->get_object_name($name_property)) {
56 1
            return $name_copy === midcom_helper_misc::urlize($name_copy);
57
        }
58
        // empty name is not "clean"
59
        return false;
60
    }
61
62
    /**
63
     * Checks for URL-safe name
64
     *
65
     * @see http://trac.midgard-project.org/ticket/809
66
     * @param string $name_property property to use as "name", if left to default (null), will be reflected
67
     */
68 90
    public function name_is_safe(string $name_property = null) : bool
69
    {
70 90
        if ($name_copy = $this->get_object_name($name_property)) {
71 90
            return $name_copy === rawurlencode($name_copy);
72
        }
73
        // empty name is not url-safe
74
        return false;
75
    }
76
77
    /**
78
     * Check that none of given object's siblings have same name.
79
     */
80 89
    public function name_is_unique() : bool
81
    {
82
        // Get current name and sanity-check
83 89
        $name = $this->get_object_name();
84 89
        if (empty($name)) {
85
            // We do not check for empty names, and do not consider them to be unique
86 4
            return false;
87
        }
88
89
        // Start the magic
90 89
        midcom::get()->auth->request_sudo('midcom.helper.reflector');
91 89
        $parent = midcom_helper_reflector_tree::get_parent($this->_object);
0 ignored issues
show
Bug introduced by
$this->_object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject expected by parameter $object of midcom_helper_reflector_tree::get_parent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $parent = midcom_helper_reflector_tree::get_parent(/** @scrutinizer ignore-type */ $this->_object);
Loading history...
92 89
        $sibling_classes = $this->get_sibling_classes($parent);
93
        if ($sibling_classes === null) {
94 84
            // This should not happen, logging error and returning true (even though it's potentially dangerous)
95 84
            debug_add("Object " . get_class($this->_object) . " #" . $this->_object->id . " has no valid parent but is not listed in the root classes, don't know what to do, returning true and supposing user knows what he is doing", MIDCOM_LOG_ERROR);
96 84
            return true;
97 84
        }
98
        $stat = $this->check_sibling_classes($name, $sibling_classes, $parent);
99
100 84
        midcom::get()->auth->drop_sudo();
101
        return $stat;
102
    }
103 10
104 10
    private function get_sibling_classes($parent = null) : ?array
105 10
    {
106 10
        if (!empty($parent->guid)) {
107 6
            // We have parent, check siblings
108 6
            $parent_resolver = new midcom_helper_reflector_tree($parent);
109
            $sibling_classes = $parent_resolver->get_child_classes();
110
            if (!in_array('midgard_attachment', $sibling_classes)) {
111 10
                $sibling_classes[] = 'midgard_attachment';
112
            }
113 4
114 4
            return $sibling_classes;
115
        }
116 6
        // No parent, we might be a root level class
117
        $is_root_class = false;
118
        $root_classes = midcom_helper_reflector_tree::get_root_classes();
119
        foreach ($root_classes as $classname) {
120 89
            if (midcom::get()->dbfactory->is_a($this->_object, $classname)) {
121 89
                $is_root_class = true;
122
                break;
123
            }
124 86
        }
125
        if (!$is_root_class) {
126 86
            return null;
127 86
        }
128 86
        return $root_classes;
129 86
    }
130
131 86
    private function check_sibling_classes(string $name, array $schema_types, $parent = null) : bool
132
    {
133 86
        foreach ($schema_types as $schema_type) {
134 86
            $qb = $this->get_sibling_qb($schema_type, $parent);
135 1
            if (!$qb) {
136 1
                continue;
137
            }
138
            $child_name_property = midcom_helper_reflector::get_name_property(new $schema_type);
139 86
140
            $qb->add_constraint($child_name_property, '=', $name);
141
            if ($qb->count()) {
142
                debug_add("Name clash in sibling class {$schema_type} for " . get_class($this->_object) . " #{$this->_object->id} (path '" . midcom_helper_reflector_tree::resolve_path($this->_object, '/') . "')" );
0 ignored issues
show
Bug introduced by
$this->_object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject expected by parameter $object of midcom_helper_reflector_tree::resolve_path(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
                debug_add("Name clash in sibling class {$schema_type} for " . get_class($this->_object) . " #{$this->_object->id} (path '" . midcom_helper_reflector_tree::resolve_path(/** @scrutinizer ignore-type */ $this->_object, '/') . "')" );
Loading history...
143
                return false;
144
            }
145
        }
146
        return true;
147
    }
148
149
    /**
150
     * Generates an unique name for the given object.
151
     *
152
     * 1st IF name is empty, we generate one from title (if title is empty too, we return false)
153
     * Then we check if it's unique, if not we add an incrementing
154 69
     * number to it (before this we make some educated guesses about a
155
     * good starting value)
156
     *
157 69
     * @param string $title_property Property of the object to use at title, if null will be reflected (see midcom_helper_reflector::get_object_title())
158 69
     * @param string $extension The file extension, when working with attachments
159
     * @return string string usable as name or boolean false on critical failures
160
     */
161
    public function generate_unique_name($title_property = null, $extension = '')
162
    {
163
        // Get current name and sanity-check
164
        $original_name = $this->get_object_name();
165 69
        if ($original_name === null) {
166
            // Fatal error with name resolution
167 69
            debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} returned critical failure for name resolution, aborting", MIDCOM_LOG_WARN);
168
            return false;
169
        }
170
171 69
        // We need the name of the "name" property later
172 69
        $name_prop = midcom_helper_reflector::get_name_property($this->_object);
173
174
        if (!empty($original_name)) {
175
            $current_name = $original_name;
176
        } else {
177 69
            // Empty name, try to generate from title
178 65
            $title_copy = midcom_helper_reflector::get_object_title($this->_object, $title_property);
179 65
            if ($title_copy === false) {
0 ignored issues
show
introduced by
The condition $title_copy === false is always false.
Loading history...
180
                // Fatal error with title resolution
181 8
                debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} returned critical failure for title resolution when name was empty, aborting", MIDCOM_LOG_WARN);
182 8
                return false;
183
            }
184
            if (empty($title_copy)) {
185
                debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} has empty name and title, aborting", MIDCOM_LOG_WARN);
186 8
                return false;
187
            }
188 8
            $current_name = midcom_helper_misc::urlize($title_copy);
189
            unset($title_copy);
190 8
        }
191
192
        // incrementer, the number to add as suffix and the base name. see _generate_unique_name_resolve_i()
193
        list($i, $base_name) = $this->_generate_unique_name_resolve_i($current_name, $extension);
194 8
195
        $this->_object->name = $base_name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
196 4
        // decrementer, do not try more than this many times (the incrementer can raise above this if we start high enough.
197
        $d = 100;
198
199
        // The loop, usually we *should* hit gold in first try
200 8
        do {
201 8
            if ($i > 1) {
202
                // Start suffixes from -002
203
                $this->_object->{$name_prop} = $base_name . sprintf('-%03d', $i) . $extension;
204
            }
205
206
            // Handle the decrementer
207
            --$d;
208 8
            if ($d < 1) {
209 8
                // Decrementer underflowed
210
                debug_add("Maximum number of tries exceeded, current name was: " . $this->_object->{$name_prop}, MIDCOM_LOG_ERROR);
211
                $this->_object->{$name_prop} = $original_name;
212 8
                return false;
213
            }
214 8
            // and the incrementer
215 8
            ++$i;
216
        } while (!$this->name_is_unique());
217
218 86
        // Get a copy of the current, usable name
219
        $ret = (string)$this->_object->{$name_prop};
220 86
        // Restore the original name
221 86
        $this->_object->{$name_prop} = $original_name;
222 86
        return $ret;
223
    }
224 86
225
    private function get_sibling_qb(string $schema_type, $parent = null)
226 86
    {
227 6
        $dummy = new $schema_type();
228
        $child_name_property = midcom_helper_reflector::get_name_property($dummy);
229 84
        if (empty($child_name_property)) {
230 84
            // This sibling class does not use names
231
            return false;
232 86
        }
233
        if ($parent === null) {
234
            $qb = midcom_helper_reflector_tree::get($schema_type)->_root_objects_qb(false);
235
        } else {
236
            $resolver = midcom_helper_reflector_tree::get($schema_type);
237 86
            $qb = $resolver->_child_objects_type_qb($schema_type, $parent, false);
238 17
        }
239
        if (!$qb) {
240 86
            return false;
241
        }
242 86
243 86
        // Do not include current object in results, this is the easiest way
244
        if (!empty($this->_object->guid)) {
245
            $qb->add_constraint('guid', '<>', $this->_object->guid);
246
        }
247 8
        $qb->add_order($child_name_property, 'DESC');
248
        // One result should be enough
249 8
        $qb->set_limit(1);
250
        return $qb;
251
252
    }
253
254 8
    private function _parse_filename(string $name, string $extension, $default = 0) : array
255
    {
256
        if (preg_match('/(.*?)-([0-9]{3,})' . $extension . '$/', $name, $name_matches)) {
257
            // Name already has i and base parts, split them.
258
            return [(int) $name_matches[2], (string) $name_matches[1]];
259
        }
260
        // Defaults
261
        return [$default, $name];
262
    }
263
264
    /**
265 8
     * Resolve the base value for the incrementing suffix and for the name.
266
     *
267 8
     * @see midcom_helper_reflector_nameresolver::generate_unique_name()
268
     * @param string $current_name the "current name" of the object (might not be the actual name value see the title logic in generate_unique_name())
269
     * @param string $extension The file extension, when working with attachments
270 8
     * @return array first key is the resolved $i second is the $base_name, which is $current_name without numeric suffix
271 8
     */
272 8
    private function _generate_unique_name_resolve_i(string $current_name, string $extension) : array
273
    {
274 6
        list($i, $base_name) = $this->_parse_filename($current_name, $extension, 1);
275 6
276 6
        // Look for siblings with similar names and see if they have higher i.
277 6
        midcom::get()->auth->request_sudo('midcom.helper.reflector');
278
        $parent = midcom_helper_reflector_tree::get_parent($this->_object);
0 ignored issues
show
Bug introduced by
$this->_object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject expected by parameter $object of midcom_helper_reflector_tree::get_parent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

278
        $parent = midcom_helper_reflector_tree::get_parent(/** @scrutinizer ignore-type */ $this->_object);
Loading history...
279 6
        $sibling_classes = $this->get_sibling_classes($parent);
280 6
        if ($sibling_classes === null) {
281
            // This should not happen, logging error and returning true (even though it's potentially dangerous)
282
            midcom::get()->auth->drop_sudo();
283
            debug_add("Object " . get_class($this->_object) . " #" . $this->_object->id . " has no valid parent but is not listed in the root classes, don't know what to do, letting higher level decide", MIDCOM_LOG_ERROR);
284 2
            return [$i, $base_name];
285 2
        }
286 2
        foreach ($sibling_classes as $schema_type) {
287 2
            $i = $this->process_schema_type($this->get_sibling_qb($schema_type, $parent), $i, $schema_type, $base_name, $extension);
288 1
        }
289 1
        midcom::get()->auth->drop_sudo();
290
291
        return [$i, $base_name];
292 2
    }
293
294 1
    private function process_schema_type($qb, $i, string $schema_type, string $base_name, string $extension) : int
295 1
    {
296 1
        if (!$qb) {
297
            return $i;
298 1
        }
299 1
        $child_name_property = midcom_helper_reflector::get_name_property(new $schema_type);
300
301
        $qb->add_constraint($child_name_property, 'LIKE', "{$base_name}-%" . $extension);
302 7
        $siblings = $qb->execute();
303
        if (!empty($siblings)) {
304 7
            $sibling = $siblings[0];
305
            $sibling_name = $sibling->{$child_name_property};
306
307 7
            $sibling_i = $this->_parse_filename($sibling_name, $extension)[0];
308
            if ($sibling_i >= $i) {
309 7
                $i = $sibling_i + 1;
310 7
            }
311
        }
312 7
        return $i;
313
    }
314
}
315