TitleDataObject::find_or_create()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 7
nop 2
1
<?php
2
3
4
class TitleDataObject extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $indexes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        'Title' => 'unique("Title, ClassName")'
8
    );
9
10
    private static $searchable_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $searchable_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
        'Title' => 'PartialMatchFilter'
12
    );
13
14
    private static $default_sort = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
        'Title' => "ASC"
16
    );
17
18
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
        'Title' => 'Varchar(255)',
20
    );
21
22
    /**
23
     * to prevent racing conditions ...
24
     *
25
     * @var array
26
     */
27
    private static $_cache = array();
28
29
    /**
30
     * see README.md for usage ...
31
     *
32
     * @param string $title
33
     * @param bool $showDBAlterationMessage
34
     * @return DataObject
35
     */
36
    public static function find_or_create($title, $showDBAlterationMessage = false)
37
    {
38
        $title = trim($title);
39
        $titleToLower = strtolower(($title));
40
        $className = get_called_class();
41
        $key = $className.'_'.$titleToLower;
42
        if (isset(self::$_cache[$key])) {
43
            if ($showDBAlterationMessage) {
44
                DB::alteration_message('Found '.$className.' with Title = <strong>'.$title.'</strong>');
45
            }
46
            return self::$_cache[$key];
47
        }
48
49
        if (! $title) {
50
            return $className::create();
51
        }
52
        $obj = $className::get()->where('LOWER("Title") =\''.Convert::raw2sql($titleToLower).'\'');
53
54
        if ($obj->count() == 0) {
55
            if ($showDBAlterationMessage) {
56
                DB::alteration_message('Creating new '.$className.' with Title = <strong>'.$title.'</strong>', 'created');
57
            }
58
            $obj = $className::create();
59
        } else {
60
            if ($showDBAlterationMessage) {
61
                DB::alteration_message('Found '.$className.' with Title = <strong>'.$title.'</strong>');
62
            }
63
            $obj = $obj->first();
64
        }
65
        $obj->Title = $title;
66
        self::$_cache[$key] = $obj;
67
        $obj->write();
68
69
        return $obj;
70
    }
71
}
72