midgard_object_class::get_object_by_guid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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
use midgard\portable\api\dbobject;
14
15
class midgard_object_class
16
{
17 10
    private static function resolve_classname(string $guid, bool $include_deleted = false) : string
18
    {
19 10
        $qb = connection::get_em()->createQueryBuilder();
20 10
        $qb->from(connection::get_fqcn('midgard_repligard'), 'r')
21 10
            ->select('r.typename, r.object_action')
22 10
            ->where('r.guid = ?1')
23 10
            ->setParameter(1, $guid);
24
25
        try {
26 10
            $result = $qb->getQuery()->getSingleResult();
27 3
        } catch (\Doctrine\ORM\NoResultException) {
28 3
            throw exception::not_exists();
29
        }
30
31 10
        if ($result["object_action"] == subscriber::ACTION_PURGE) {
32 2
            throw exception::object_purged();
33
        }
34 9
        if (!$include_deleted && $result["object_action"] == subscriber::ACTION_DELETE) {
35 1
            throw exception::object_deleted();
36
        }
37 8
        if ($include_deleted && !self::has_metadata($result["typename"])) {
38 1
            throw exception::invalid_property_value();
39
        }
40
41 7
        return $result["typename"];
42
    }
43
44 5
    public static function factory(?string $classname, $id = null) : ?dbobject
45
    {
46 5
        if ($classname === null) {
47
            return null;
48
        }
49 5
        $classname = connection::get_fqcn($classname);
50 5
        if (!class_exists($classname)) {
51
            throw exception::invalid_object();
52
        }
53 5
        return new $classname($id);
54
    }
55
56 4
    public static function undelete(string $guid) : bool
57
    {
58
        try {
59 4
            $classname = self::resolve_classname($guid, true);
60 2
        } catch (exception $e) {
61 2
            return false;
62
        }
63 3
        $classname = connection::get_fqcn($classname);
64
65 3
        $qb = new \midgard_query_builder($classname);
66 3
        $qb->include_deleted();
67 3
        $qb->add_constraint('guid', '=', $guid);
68 3
        $results = $qb->execute();
69 3
        if (empty($results)) {
70
            exception::not_exists();
71
            return false;
72
        }
73 3
        $entity = $results[0];
74
75 3
        if (!$entity->metadata_deleted) {
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_deleted does not exist on midgard\portable\api\mgdobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
76 1
            exception::internal(new \Exception("Object is not deleted."));
77 1
            return false;
78
        }
79
80
        try {
81 3
            $om = new objectmanager(connection::get_em());
82 3
            $om->undelete($entity);
83
        } catch (\Exception $e) {
84
            exception::internal($e);
85
            return false;
86
        }
87
88 3
        return true;
89
    }
90
91 7
    public static function get_object_by_guid(string $guid) : dbobject
92
    {
93 7
        if (!mgd_is_guid($guid)) {
94 1
            throw exception::not_exists();
95
        }
96
97 6
        $type = self::resolve_classname($guid);
98 4
        return self::factory($type, $guid);
99
    }
100
101 2
    public static function get_property_up(string $classname) : ?string
102
    {
103 2
        $cm = connection::get_em()->getClassMetadata($classname);
104 2
        return $cm->midgard['upfield'];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
105
    }
106
107 2
    public static function get_property_parent(string $classname) : ?string
108
    {
109 2
        $cm = connection::get_em()->getClassMetadata($classname);
110 2
        return $cm->midgard['parentfield'];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
111
    }
112
113 7
    public static function has_metadata($classname) : bool
114
    {
115 7
        if (is_string($classname)) {
116 7
            return in_array(metadata::class, class_implements($classname));
117
        }
118 1
        if (is_object($classname)) {
119 1
            return $classname instanceof metadata;
120
        }
121
        return false;
122
    }
123
}
124