LangModule::mergeOrAddEntity()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 8.439
cc 6
eloc 22
nc 7
nop 4
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     LangModule
7
 *
8
 * @property string Name
9
 *
10
 * @method DataList Entities
11
 */
12
class LangModule 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...
13
14
    /**
15
     * @var array
16
     * @config
17
     */
18
    private static $db = [
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
        'Name' => 'Varchar',
20
    ];
21
22
    /**
23
     * @var array
24
     * @config
25
     */
26
    private static $has_many = [
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 $has_many 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...
27
        'Entities' => 'LangEntity',
28
    ];
29
30
    /**
31
     * Find module by name or create a new one if not existing.
32
     *
33
     * @param string $name
34
     *
35
     * @return LangModule
36
     */
37
    public static function findOrCreate($name) {
38
        $module = static::get()->filter('Name', strtolower($name));
39
40
        if ($module->exists()) {
41
            return $module->first();
42
        }
43
44
        $module = static::create(['Name' => $name]);
45
        $module->write();
46
47
        return $module;
48
    }
49
50
    protected function onBeforeWrite() {
51
        $this->Name = strtolower($this->Name);
52
53
        parent::onBeforeWrite();
54
    }
55
56
    public static function getByName($name) {
57
        if(($module = static::get()->filter('Name', $name)->first()) instanceof LangModule) {
58
            return $module;
59
        }
60
61
        return false;
62
    }
63
64
    public function mergeOrAddEntity($namespace, $value, $title = '', $merge = false) {
65
        // try to check is existing entity already
66
        $entity = LangEntity::get()->filter('Namespace', $namespace)->first();
67
68
        if ($entity instanceof LangEntity) {
69
            if ($merge) { // if we can overwrite value
70
                $entity->Value = $value;
71
72
                if (! empty($title)) {
73
                    $entity->Title = $title;
74
                }
75
76
                $entity->write();
77
78
                if (! ($this->Entities()->byID($entity->ID) instanceof LangEntity)) {
0 ignored issues
show
Bug introduced by
The method Entities() does not exist on LangModule. Did you maybe mean provideI18nEntities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
79
                    $entity->ModuleID = $this->ID;
80
                    $entity->write();
81
82
                    $this->Entities()->add($entity);
0 ignored issues
show
Bug introduced by
The method Entities() does not exist on LangModule. Did you maybe mean provideI18nEntities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83
84
                    return $entity;
85
                }
86
            }
87
        } else { // entity not existing
88
            $entity = LangEntity::create([
89
                'Namespace' => $namespace,
90
                'Value'     => $value,
91
                'Title'     => $title,
92
                'ModuleID'  => $this->ID,
93
            ]);
94
95
            if ($entity->write()) {
96
                $this->Entities()->add($entity);
0 ignored issues
show
Bug introduced by
The method Entities() does not exist on LangModule. Did you maybe mean provideI18nEntities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
98
                return $entity;
99
            }
100
        }
101
    }
102
}