Passed
Push — master ( f46ee6...4aaf16 )
by Dominik
20:25 queued 16:26
created

Version20201228090100::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Application\Migrations;
4
5
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
6
use Azine\MailgunWebhooksBundle\Entity\MailgunMessageSummary;
7
use Doctrine\DBAL\Migrations\AbortMigrationException;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Migrations\AbortMigrationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\DBAL\Migrations\AbstractMigration;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Migrations\AbstractMigration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\DBAL\Schema\Schema;
10
use Doctrine\ORM\EntityManager;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Update your db-table structure before applying this migration (doctrine:migration:diff && doctrine:migration:migrate).
16
 *
17
 * Important Note: doctrine:migration: diff will suggest to add a foreign key constraint like this:
18
 *                 "ALTER TABLE mailgun_event ADD CONSTRAINT FK_1271933F537A1329 FOREIGN KEY (message_id) REFERENCES mailgun_message_summary (id) ON DELETE CASCADE".
19
 *                 !!! BUT this constraint can only be added after the migration below ran successfully. !!!
20
 *
21
 * Add MessageSummary Entity to keep track and summarize what happened with a sent email.
22
 * Rename table email_traffic_statistics to mailgun_email_traffic_statistics
23
 */
24
class Version20201228090100 extends AbstractMigration implements ContainerAwareInterface
25
{
26
    /** @var EntityManager */
27
    private $manager;
28
29
    public function setContainer(ContainerInterface $container = null)
30
    {
31
        $this->manager = $container->get('doctrine.orm.entity_manager');
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-call */ 
32
        $this->manager = $container->get('doctrine.orm.entity_manager');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
    }
33
34
    /**
35
     * @param Schema $schema
36
     * @throws AbortMigrationException
37
     */
38
    public function up(Schema $schema)
39
    {
40
        $this->abortIf('mysql' != $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
41
    }
42
43
    /**
44
     * @param Schema $schema
45
     * @throws \Doctrine\ORM\OptimisticLockException
46
     */
47
    public function postUp(Schema $schema)
48
    {
49
        $repository = $this->manager->getRepository(MailgunMessageSummary::class);
50
        $processing = true;
51
        $page=0;
52
        $pageSize=100;
53
        while($processing) {
54
            $query = $this->manager->createQuery("select e from " . MailgunEvent::class . " e");
55
            $query->setFirstResult($page*$pageSize);
56
            $query->setMaxResults($pageSize);
57
            $iterableResult = $query->iterate();
58
            $counter = 0;
59
            foreach ($iterableResult as $row) {
60
                $repository->createOrUpdateMessageSummary($row[0]);
61
                $this->manager->flush();
62
                $counter++;
63
            }
64
            $this->manager->commit();
65
            $this->manager->clear();
66
            $processing = $counter == $pageSize;
67
            $this->manager->beginTransaction();
68
            $page++;
69
        }
70
    }
71
72
    /**
73
     * @param Schema $schema
74
     * @throws AbortMigrationException
75
     */
76
    public function down(Schema $schema)
77
    {
78
        $this->abortIf('mysql' != $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
79
    }
80
}
81