Completed
Push — master ( c7c6d5...eb76cf )
by Andreas
02:59
created

midgard_storage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 90.7%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 82
c 4
b 0
f 0
dl 0
loc 144
ccs 78
cts 86
cp 0.907
rs 10
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_cm() 0 19 4
A create_class_storage() 0 17 3
A generate_proxyfile() 0 10 2
A create_base_storage() 0 40 5
A class_storage_exists() 0 7 2
A update_class_storage() 0 31 5
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 Doctrine\Persistence\Mapping\ClassMetadata;
9
use Doctrine\Common\Proxy\ProxyGenerator;
10
use Doctrine\DBAL\Schema\Comparator;
11
use Doctrine\ORM\Tools\SchemaTool;
12
use Doctrine\ORM\Mapping\MappingException;
13
use midgard\portable\storage\connection;
14
use Doctrine\ORM\EntityManager;
15
16
class midgard_storage
17
{
18 2
    public static function create_base_storage() : bool
19
    {
20 2
        $em = connection::get_em();
21 2
        $ns = $em->getConfiguration()->getEntityNamespace("midgard");
22
23 2
        $cm_repligard = $em->getClassMetadata($ns . '\\midgard_repligard');
24 2
        if (!self::create_class_storage($cm_repligard->getName())) {
25
            return false;
26
        }
27 2
        $cm_person = $em->getClassMetadata($ns . '\\midgard_person');
28 2
        if (!self::create_class_storage($cm_person->getName())) {
29
            return false;
30
        }
31 2
        $cm_user = $em->getClassMetadata($ns . '\\midgard_user');
32 2
        if (!self::create_class_storage($cm_user->getName())) {
33
            return false;
34
        }
35
36 2
        $admin = $em->find('midgard:midgard_user', 1);
37
38 2
        if ($admin === null) {
39 2
            $fqcn = $cm_person->getName();
40 2
            $person = new $fqcn;
41 2
            $person->firstname = 'Midgard';
42 2
            $person->lastname = 'Administrator';
43 2
            $person->create();
44
45 2
            $fqcn = $cm_user->getName();
46 2
            $admin = new $fqcn;
47 2
            $admin->authtype = 'Legacy';
48 2
            $admin->authtypeid = 2;
49 2
            $admin->login = 'admin';
50 2
            $admin->password = password_hash('password', PASSWORD_DEFAULT);
51 2
            $admin->active = true;
52 2
            $admin->usertype = 2;
53 2
            $admin->set_person($person);
54 2
            $admin->create();
55
        }
56
57 2
        return true;
58
    }
59
60 4
    public static function create_class_storage(string $classname) : bool
61
    {
62 4
        $em = connection::get_em();
63
64 4
        $cm = self::get_cm($em, $classname);
65 4
        if ($cm === null) {
66 1
            return false;
67
        }
68
69 4
        if (!$em->getConnection()->getSchemaManager()->tablesExist([$cm->getTableName()])) {
70 4
            $tool = new SchemaTool($em);
71 4
            $tool->createSchema([$cm]);
72
        }
73
74 4
        self::generate_proxyfile($cm);
75
76 4
        return true;
77
    }
78
79 4
    private static function generate_proxyfile(ClassMetadata $cm)
80
    {
81 4
        $em = connection::get_em();
82 4
        $generator = new ProxyGenerator($em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\Configuration::getProxyDir() has been deprecated: 2.7 We're switch to `ocramius/proxy-manager` and this method isn't applicable any longer ( Ignorable by Annotation )

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

82
        $generator = new ProxyGenerator(/** @scrutinizer ignore-deprecated */ $em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\ORM\Configuration::getProxyNamespace() has been deprecated: 2.7 We're switch to `ocramius/proxy-manager` and this method isn't applicable any longer ( Ignorable by Annotation )

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

82
        $generator = new ProxyGenerator($em->getConfiguration()->getProxyDir(), /** @scrutinizer ignore-deprecated */ $em->getConfiguration()->getProxyNamespace());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83 4
        $generator->setPlaceholder('baseProxyInterface', 'Doctrine\ORM\Proxy\Proxy');
84 4
        $filename = $generator->getProxyFileName($cm->getName());
85 4
        if (file_exists($filename)) {
86 3
            unlink($filename);
87
        }
88 4
        $generator->generateProxyClass($cm, $filename);
89 4
    }
90
91 4
    private static function get_cm(EntityManager $em, string $classname) : ?\Doctrine\ORM\Mapping\ClassMetadata
92
    {
93 4
        if (!class_exists($classname)) {
94
            // if the class doesn't exist (e.g. for some_random_string), there is really nothing we could do
95 2
            return null;
96
        }
97
98 4
        $factory = $em->getMetadataFactory();
99
        try {
100 4
            return $factory->getMetadataFor($classname);
101 3
        } catch (MappingException $e) {
102
            // add namespace
103 3
            $classname = $em->getConfiguration()->getEntityNamespace("midgard") . '\\' . $classname;
104
            try {
105 3
                return $factory->getMetadataFor($classname);
106
            } catch (MappingException $e) {
107
                // check for merged classes (duplicate tablenames)
108
                $classname = get_class(new $classname);
109
                return $factory->getMetadataFor($classname);
110
            }
111
        }
112
    }
113
114
    /**
115
     * Update DB table according to MgdSchema information.
116
     *
117
     * this does not use SchemaTool's updateSchema, since this would delete columns that are no longer
118
     * in the MgdSchema definition
119
     */
120 1
    public static function update_class_storage(string $classname) : bool
121
    {
122 1
        $em = connection::get_em();
123 1
        $cm = self::get_cm($em, $classname);
124 1
        if ($cm === null) {
125
            return false;
126
        }
127 1
        $sm = $em->getConnection()->getSchemaManager();
128 1
        if ($sm->tablesExist([$cm->getTableName()])) {
129 1
            $tool = new SchemaTool($em);
130 1
            $conn = $em->getConnection();
131 1
            $from = $sm->createSchema();
132 1
            $to = $tool->getSchemaFromMetadata([$cm]);
133
134 1
            $comparator = new Comparator;
135 1
            $diff = $comparator->compare($from, $to);
136 1
            if (!empty($diff->changedTables[$cm->getTableName()]->removedColumns)) {
137 1
                $diff->changedTables[$cm->getTableName()]->removedColumns = [];
138
            }
139 1
            $sql = $diff->toSaveSql($conn->getDatabasePlatform());
140
141
142 1
            foreach ($sql as $sql_line) {
143 1
                $conn->executeQuery($sql_line);
144
            }
145
146 1
            self::generate_proxyfile($cm);
147
148 1
            return true;
149
        }
150
        return false;
151
    }
152
153 1
    public static function class_storage_exists(string $classname) : bool
154
    {
155 1
        $em = connection::get_em();
156 1
        if ($cm = self::get_cm($em, $classname)) {
157 1
            return $em->getConnection()->getSchemaManager()->tablesExist([$cm->getTableName()]);
158
        }
159 1
        return false;
160
    }
161
}
162