Completed
Branch master (1e7eb6)
by Andreas
02:50
created

midgard_storage::delete_class_storage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
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 Doctrine\Common\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
15
class midgard_storage
16
{
17 2
    public static function create_base_storage()
18
    {
19 2
        $em = connection::get_em();
20 2
        $ns = $em->getConfiguration()->getEntityNamespace("midgard");
21
22 2
        $cm_repligard = $em->getClassMetadata($ns . '\\midgard_repligard');
23 2
        if (!self::create_class_storage($cm_repligard->getName())) {
24
            return false;
25
        }
26 2
        $cm_person = $em->getClassMetadata($ns . '\\midgard_person');
27 2
        if (!self::create_class_storage($cm_person->getName())) {
28
            return false;
29
        }
30 2
        $cm_user = $em->getClassMetadata($ns . '\\midgard_user');
31 2
        if (!self::create_class_storage($cm_user->getName())) {
32
            return false;
33
        }
34
35 2
        $admin = $em->find('midgard:midgard_user', 1);
36
37 2
        if ($admin === null) {
38 2
            $fqcn = $cm_person->getName();
39 2
            $person = new $fqcn;
40 2
            $person->firstname = 'Midgard';
41 2
            $person->lastname = 'Administrator';
42 2
            $person->create();
43
44 2
            $fqcn = $cm_user->getName();
45 2
            $admin = new $fqcn;
46 2
            $admin->authtype = 'Legacy';
47 2
            $admin->authtypeid = 2;
48 2
            $admin->login = 'admin';
49 2
            $admin->password = password_hash('password', PASSWORD_DEFAULT);
50 2
            $admin->active = true;
51 2
            $admin->usertype = 2;
52 2
            $admin->set_person($person);
53 2
            $admin->create();
54 2
        }
55
56 2
        return true;
57
    }
58
59 4
    public static function create_class_storage($classname)
60 1
    {
61 4
        $em = connection::get_em();
62
63 4
        $cm = self::get_cm($em, $classname);
64 4
        if ($cm === false) {
65 1
            return false;
66
        }
67
68 4
        if (!$em->getConnection()->getSchemaManager()->tablesExist([$cm->getTableName()])) {
0 ignored issues
show
Bug introduced by
The method getTableName() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo. ( Ignorable by Annotation )

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

68
        if (!$em->getConnection()->getSchemaManager()->tablesExist([$cm->/** @scrutinizer ignore-call */ getTableName()])) {
Loading history...
69 4
            $tool = new SchemaTool($em);
70 4
            $tool->createSchema([$cm]);
71 4
        }
72
73 4
        self::generate_proxyfile($cm);
74
75 4
        return true;
76
    }
77
78 4
    private static function generate_proxyfile(ClassMetadata $cm)
79
    {
80 4
        $em = connection::get_em();
81 4
        $generator = new ProxyGenerator($em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Proxy\ProxyGenerator has been deprecated: The Doctrine\Common\Proxy component is deprecated, please use ocramius/proxy-manager instead. ( Ignorable by Annotation )

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

81
        $generator = /** @scrutinizer ignore-deprecated */ new ProxyGenerator($em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace());
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

81
        $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...
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

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