Passed
Branch master (056094)
by Andreas
04:47
created

midgard_object_class::undelete()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.1314

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 6
nop 1
dl 0
loc 35
ccs 19
cts 23
cp 0.8261
crap 5.1314
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
use midgard\portable\storage\connection;
9
use midgard\portable\storage\subscriber;
10
use midgard\portable\api\error\exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use midgard\portable\storage\objectmanager;
12
use midgard\portable\storage\interfaces\metadata;
13
14
class midgard_object_class
15
{
16 10
    private static function resolve_classname($guid, $include_deleted = false)
17
    {
18 10
        $qb = connection::get_em()->createQueryBuilder();
19 10
        $qb->from('midgard:midgard_repligard', 'r')
20 10
            ->select('r.typename, r.object_action')
21 10
            ->where('r.guid = ?1')
22 10
            ->setParameter(1, $guid);
23
24
        try {
25 10
            $result = $qb->getQuery()->getSingleResult();
26 10
        } catch (\Doctrine\ORM\NoResultException $e) {
27 3
            throw exception::not_exists();
28
        }
29
30 10
        if ($result["object_action"] == subscriber::ACTION_PURGE) {
31 2
            throw exception::object_purged();
32
        }
33 9
        if (!$include_deleted && $result["object_action"] == subscriber::ACTION_DELETE) {
34 1
            throw exception::object_deleted();
35
        }
36 8
        if ($include_deleted && !self::has_metadata($result["typename"])) {
37 1
            throw exception::invalid_property_value();
38
        }
39
40 7
        return $result["typename"];
41
    }
42
43 8
    public static function resolve_fqn($classname)
44
    {
45 8
        $cm = connection::get_em()->getClassMetadata('midgard:' . $classname);
46 8
        return $cm->name;
47
    }
48
49 5
    public static function factory($classname, $id = null)
50
    {
51 5
        if ($classname === null) {
52
            return null;
53
        }
54 5
        $classname = self::resolve_fqn($classname);
55 5
        return new $classname($id);
56
    }
57
58
    /**
59
     * @return boolean
60
     */
61 4
    public static function undelete($guid)
62
    {
63
        try {
64 4
            $classname = self::resolve_classname($guid, true);
65 4
        } catch (exception $e) {
66 2
            return false;
67
        }
68 3
        $classname = self::resolve_fqn($classname);
69
70 3
        $qb = new \midgard_query_builder($classname);
71 3
        $qb->include_deleted();
72 3
        $qb->add_constraint('guid', '=', $guid);
73 3
        $results = $qb->execute();
74 3
        if (count($results) === 0) {
75
            exception::not_exists();
76
            return false;
77
        }
78 3
        $entity = array_shift($results);
79
80 3
        if (!$entity->metadata_deleted) {
81 1
            exception::internal(new \Exception("Object is not deleted."));
82 1
            return false;
83
        }
84
85
        try {
86 3
            $om = new objectmanager(connection::get_em());
87 3
            $om->undelete($entity);
88 3
        } catch (\Exception $e) {
89
            exception::internal($e);
90
            return false;
91
        }
92
93 3
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
94 3
        return true;
95
    }
96
97 7
    public static function get_object_by_guid($guid)
98
    {
99 7
        if (!mgd_is_guid($guid)) {
100 1
            throw exception::not_exists();
101
        }
102
103 6
        $type = self::resolve_classname($guid);
104 4
        return self::factory($type, $guid);
105
    }
106
107 2
    public static function get_property_up($classname)
108
    {
109 2
        if (is_object($classname)) {
110 1
            $classname = get_class($classname);
111 1
        }
112 2
        $cm = connection::get_em()->getClassMetadata($classname);
113 2
        return $cm->midgard['upfield'];
114
    }
115
116 3
    public static function get_property_parent($classname)
117
    {
118 2
        if (is_object($classname)) {
119 1
            $classname = get_class($classname);
120 1
        }
121 2
        $cm = connection::get_em()->getClassMetadata($classname);
122 2
        return $cm->midgard['parentfield'];
123 3
    }
124
125
    public static function connect_default($classname, $signal, $callback, $userdata = null) // <== check!
0 ignored issues
show
Unused Code introduced by
The parameter $classname is not used and could be removed. ( Ignorable by Annotation )

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

125
    public static function connect_default(/** @scrutinizer ignore-unused */ $classname, $signal, $callback, $userdata = null) // <== check!

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $signal is not used and could be removed. ( Ignorable by Annotation )

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

125
    public static function connect_default($classname, /** @scrutinizer ignore-unused */ $signal, $callback, $userdata = null) // <== check!

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $userdata is not used and could be removed. ( Ignorable by Annotation )

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

125
    public static function connect_default($classname, $signal, $callback, /** @scrutinizer ignore-unused */ $userdata = null) // <== check!

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

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

125
    public static function connect_default($classname, $signal, /** @scrutinizer ignore-unused */ $callback, $userdata = null) // <== check!

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127
    }
128
129 7
    public static function has_metadata($classname)
130
    {
131 7
        if (is_string($classname)) {
132 7
            return in_array('midgard\\portable\\storage\\interfaces\\metadata', class_implements($classname));
133
        }
134 1
        if (is_object($classname)) {
135 1
            return ($classname instanceof metadata);
136
        }
137
        return false;
138
    }
139
140
    public static function get_schema_value($classname, $node_name)
0 ignored issues
show
Unused Code introduced by
The parameter $node_name is not used and could be removed. ( Ignorable by Annotation )

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

140
    public static function get_schema_value($classname, /** @scrutinizer ignore-unused */ $node_name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $classname is not used and could be removed. ( Ignorable by Annotation )

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

140
    public static function get_schema_value(/** @scrutinizer ignore-unused */ $classname, $node_name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
    }
143
}
144