Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

Version20170217154603::migrateWidgetAndViews()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 36
nop 0
1
<?php
2
3
namespace Victoire\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\ORM\EntityManager;
8
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty;
12
use Victoire\Bundle\BusinessEntityBundle\EventSubscriber\BusinessEntitySubscriber;
13
use Victoire\Bundle\CoreBundle\Annotations\BusinessEntity;
14
use Victoire\Bundle\CoreBundle\Annotations\BusinessProperty as BusinessPropertyAnnotation;
15
use Victoire\Bundle\CoreBundle\Entity\EntityProxy;
16
use Victoire\Bundle\CoreBundle\EventSubscriber\WidgetSubscriber;
17
use Victoire\Bundle\ORMBusinessEntityBundle\Entity\ORMBusinessEntity;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
22
class Version20170217154603 extends AbstractMigration implements ContainerAwareInterface
23
{
24
    private $container;
25
26
    public function setContainer(ContainerInterface $container = null)
27
    {
28
        $this->container = $container;
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getContainer()
35
    {
36
        return $this->container;
37
    }
38
39
    /**
40
     * @param Schema $schema
41
     */
42
    public function up(Schema $schema)
43
    {
44
        // this up() migration is auto-generated, please modify it to your needs
45
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
46
        $this->migrateBusinessEntities();
47
        $this->migrateEntityProxies();
48
        $this->migrateWidgetAndViews();
49
    }
50
51
    /**
52
     * @param Schema $schema
53
     */
54
    public function down(Schema $schema)
55
    {
56
        $this->write('Not implemented');
57
    }
58
59
    protected function migrateBusinessEntityName($entity)
60
    {
61
        /** @var EntityManager $entityManager */
62
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
63
        $businessEntity = $entityManager->getRepository('VictoireORMBusinessEntityBundle:ORMBusinessEntity')
64
            ->createQueryBuilder('proxy')
65
            ->where('proxy.name LIKE :prop')
66
            ->setParameter(':prop', $entity->getOldBusinessEntityName())
67
            ->getQuery()
68
            ->getOneOrNullResult();
69
70
        $entity->setBusinessEntity($businessEntity);
71
    }
72
73
    protected function migrateOldProxies($oldProxy)
74
    {
75
        /** @var EntityManager $entityManager */
76
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
77
78
        $proxy = new EntityProxy();
79
        foreach ($oldProxy as $oldProxyPropName => $oldProxyPropValue) {
80
            if ($oldProxyPropName === 'id') {
81
                $proxy->setId($oldProxyPropValue);
82
            } elseif ($oldProxyPropValue !== null) {
83
                $businessEntity = $entityManager->getRepository('VictoireORMBusinessEntityBundle:ORMBusinessEntity')
84
                    ->createQueryBuilder('proxy')
85
                    ->where('proxy.name LIKE :prop')
86
                    ->setParameter(':prop', str_replace('_id', '', $oldProxyPropName))
87
                    ->getQuery()
88
                    ->getOneOrNullResult();
89
                $proxy->setBusinessEntity($businessEntity);
90
                $proxy->setRessourceId($oldProxyPropValue);
91
            }
92
        }
93
94
        return $proxy;
95
    }
96
97
    public function migrateEntityProxies()
98
    {
99
        /** @var \Doctrine\DBAL\Connection $conn */
100
        $conn = $this->getContainer()->get('database_connection');
101
        /** @var EntityManager $entityManager */
102
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
103
104
        $sql = 'SELECT * FROM vic_entity_proxy';
105
        $oldProxies = $conn->fetchAll($sql);
106
107
        foreach ($oldProxies as $k => $oldProxy) {
108
            $proxy = $this->migrateOldProxies($oldProxy);
109
            $entityManager->persist($proxy);
110
            // Force the ID
111
            $metadata = $entityManager->getClassMetaData(get_class($proxy));
112
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
113
        }
114
115
        $entityManager->flush();
116
    }
117
118
    public function migrateWidgetAndViews()
119
    {
120
        /** @var \Doctrine\DBAL\Connection $conn */
121
        $conn = $this->getContainer()->get('database_connection');
0 ignored issues
show
Unused Code introduced by
$conn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
        /** @var EntityManager $entityManager */
123
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
124
125
        foreach ($entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findAll() as $bt) {
126
            $this->migrateBusinessEntityName($bt);
127
        }
128
        foreach ($entityManager->getRepository('VictoireWidgetBundle:Widget')->findAll() as $widget) {
129
            if ($widget->getOldBusinessEntityName()) {
130
                $this->migrateBusinessEntityName($widget);
131
            }
132
        }
133
134
        // Do not uselessly regenerate the viewCss threw the onFlush event. It led to an exception
135
        $evm = $entityManager->getEventManager();
136
        foreach ($entityManager->getEventManager()->getListeners() as $event => $listeners) {
137
            foreach ($listeners as $key => $listener) {
138
                if ($listener instanceof WidgetSubscriber) {
139
                    $evm->removeEventListener(['onFlush'], $listener);
140
                }
141
                if ($listener instanceof BusinessEntitySubscriber) {
142
                    $evm->removeEventListener(['postUpdate'], $listener);
143
                }
144
            }
145
        }
146
147
        $entityManager->flush();
148
    }
149
150
    /**
151
     * Parse the given Class to find some annotations related to BusinessEntities.
152
     */
153
    public function parse(\ReflectionClass $class)
154
    {
155
        $businessEntity = null;
156
        $classPath = dirname($class->getFileName());
157
        $inPaths = false;
158
        foreach ($this->getContainer()->getParameter('victoire_core.base_paths') as $key => $_path) {
159
            //Check the entity path is in watching paths
160
            if (strpos($classPath, realpath($_path)) === 0) {
161
                $inPaths = true;
162
            }
163
        }
164
        if ($inPaths) {
165
            $classAnnotations = $this->getContainer()->get('annotation_reader')->getClassAnnotations($class);
166 View Code Duplication
            if (!empty($classAnnotations)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
                foreach ($classAnnotations as $key => $annot) {
168
                    if (!is_numeric($key)) {
169
                        continue;
170
                    }
171
                    $classAnnotations[get_class($annot)] = $annot;
172
                }
173
            }
174
175
            // Evaluate Entity annotation
176
            if (isset($classAnnotations['Victoire\Bundle\CoreBundle\Annotations\BusinessEntity'])) {
177
                /** @var BusinessEntity $annotationObj */
178
                $annotationObj = $classAnnotations['Victoire\Bundle\CoreBundle\Annotations\BusinessEntity'];
179
                $businessEntity = $this->createBusinessEntity(
180
                    $class->getName(),
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
181
                    $annotationObj,
182
                    $this->loadBusinessProperties($class)
183
                );
184
            }
185
        }
186
187
        return $businessEntity;
188
    }
189
190
    /**
191
     * load business properties from ReflectionClass.
192
     *
193
     * @return array
194
     **/
195
    protected function loadBusinessProperties(\ReflectionClass $class)
196
    {
197
        $reader = $this->getContainer()->get('annotation_reader');
198
        $businessProperties = [];
199
        $properties = $class->getProperties();
200
        $traits = $class->getTraits();
201
        $className = $class->getName();
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
202
        // if the class is translatable, then parse annotations on it's translation class
203
        if (array_key_exists(Translatable::class, $traits)) {
204
            $translation = new \ReflectionClass($className::getTranslationEntityClass());
205
            $translationProperties = $translation->getProperties();
206
            $properties = array_merge($properties, $translationProperties);
207
        }
208
209
        foreach ($properties as $property) {
210
            $annotations = $reader->getPropertyAnnotations($property);
211
            foreach ($annotations as $key => $annotationObj) {
212
                if ($annotationObj instanceof BusinessPropertyAnnotation && !in_array($class, $businessProperties)) {
213
                    $businessProperties[$property->name] = $annotationObj;
214
                }
215
            }
216
        }
217
        // we load business properties of parents recursively
218
        // because they are defined by an annotation not by the property type(private, protected, public)
219
        $parentClass = $class->getParentClass();
220
        if ($parentClass) {
221
            //load parent properties recursively
222
            $parentProperties = $this->loadBusinessProperties(new \ReflectionClass($parentClass->getName()));
223
            foreach ($parentProperties as $propertyName => $parentProperty) {
224
                if (!array_key_exists($propertyName, $businessProperties)) {
225
                    $businessProperties[$propertyName] = $parentProperty;
226
                }
227
            }
228
        }
229
230
        return $businessProperties;
231
    }
232
233
    /**
234
     * @param $className
235
     * @param $annotationObj
236
     * @param $businessProperties
237
     *
238
     * @return ORMBusinessEntity
239
     */
240
    private function createBusinessEntity($className, $annotationObj, $businessProperties)
241
    {
242
        $businessEntity = new ORMBusinessEntity();
243
        $classNameArray = explode('\\', $className);
244
        $entityName = array_pop($classNameArray);
245
        $businessEntity->setName($entityName);
246
        $businessEntity->setClass($className);
247
        $businessEntity->setAvailableWidgets($annotationObj->getWidgets());
248
        //parse the array of the annotation reader
249
        foreach ($businessProperties as $propertyName => $property) {
250
            $businessProperty = new BusinessProperty();
251
            $businessProperty->setTypes($property->getTypes());
252
            $businessProperty->setBusinessEntity($businessEntity);
253
            $businessProperty->setName($propertyName);
254
        }
255
256
        return $businessEntity;
257
    }
258
259
    private function migrateBusinessEntities()
260
    {
261
        /** @var EntityManager $entityManager */
262
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
263
264
        foreach ($this->getContainer()->get('victoire_business_entity.annotation_driver')->getAllClassNames() as $className) {
265
            $businessEntity = $this->parse(new \ReflectionClass($className));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $businessEntity is correct as $this->parse(new \ReflectionClass($className)) (which targets Victoire\Migrations\Version20170217154603::parse()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
266
            if ($businessEntity) {
267
                $entityManager->persist($businessEntity);
268
            }
269
        }
270
271
        $entityManager->flush();
272
    }
273
}
274