Passed
Push — master ( 37a082...f46ee6 )
by Dominik
76:18 queued 51:18
created

Version20201123090099::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
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
 * Move & rename this migration to your doctrine:migrations directory and run it to upgrade the
16
 * db tables and generate the MessageSummary-Objects from the MailgunEvents in your database.
17
 *
18
 * Add MessageSummary Entity to keep track and summarize what happened with a sent email.
19
 * Rename table email_traffic_statistics to mailgun_email_traffic_statistics
20
 */
21
class Version20201123090099 extends AbstractMigration implements ContainerAwareInterface
22
{
23
    /** @var EntityManager */
24
    private $manager;
25
26
    public function setContainer(ContainerInterface $container = null)
27
    {
28
        $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

28
        /** @scrutinizer ignore-call */ 
29
        $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...
29
    }
30
31
    /**
32
     * @param Schema $schema
33
     * @throws AbortMigrationException
34
     */
35
    public function up(Schema $schema)
36
    {
37
        $this->abortIf('mysql' != $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
38
        $this->addSql('RENAME TABLE email_traffic_statistics TO mailgun_email_traffic_statistics');
39
        $this->addSql('CREATE TABLE mailgun_message_summary (id VARCHAR(255) NOT NULL, fromAddress VARCHAR(255) NOT NULL, toAddress LONGTEXT NOT NULL, firstOpened DATETIME DEFAULT NULL, lastOpened DATETIME DEFAULT NULL, openCount INT NOT NULL, sendDate DATETIME NOT NULL, deliveryStatus VARCHAR(95) NOT NULL, senderIp VARCHAR(15) NOT NULL, subject LONGTEXT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
40
        $this->addSql('ALTER TABLE mailgun_event ADD messageId VARCHAR(255) DEFAULT NULL, CHANGE message_id message_id VARCHAR(255) NOT NULL, ADD sender VARCHAR(255) NOT NULL');
41
        $this->addSql('ALTER TABLE mailgun_event ADD CONSTRAINT FK_1271933FA4C3A0DA FOREIGN KEY (messageId) REFERENCES mailgun_message_summary (id) ON DELETE CASCADE');
42
        $this->addSql('CREATE INDEX IDX_1271933FA4C3A0DA ON mailgun_event (messageId)');
43
    }
44
45
    /**
46
     * @param Schema $schema
47
     * @throws \Doctrine\ORM\OptimisticLockException
48
     */
49
    public function postUp(Schema $schema)
50
    {
51
        foreach($this->manager->getRepository(MailgunEvent::class)->findAll() as $event){
52
            $this->manager->getRepository(MailgunMessageSummary::class)->createOrUpdateMessageSummary($event);
53
            $this->manager->flush();
54
        }
55
    }
56
57
    /**
58
     * @param Schema $schema
59
     * @throws AbortMigrationException
60
     */
61
    public function down(Schema $schema)
62
    {
63
        $this->abortIf('mysql' != $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
64
        $this->addSql('RENAME TABLE mailgun_email_traffic_statistics TO email_traffic_statistics');
65
        $this->addSql('DROP INDEX IDX_1271933FA4C3A0DA ON mailgun_event');
66
        $this->addSql('ALTER TABLE mailgun_event DROP FOREIGN KEY FK_1271933FA4C3A0DA');
67
        $this->addSql('ALTER TABLE mailgun_event DROP sender, DROP messageId, CHANGE message_id message_id VARCHAR(255) DEFAULT NULL COLLATE utf8_unicode_ci');
68
        $this->addSql('DROP TABLE mailgun_message_summary');
69
    }
70
}
71