Completed
Push — feature/test-php-7-2-in-travis ( 027341...a56bae )
by
unknown
05:31
created

Version20160719090052::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Surfnet\StepupMiddleware\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Rhumsaa\Uuid\Uuid;
8
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\InstitutionListing;
9
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\WhitelistEntry;
10
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\InstitutionListingRepository;
11
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\WhitelistEntryRepository;
12
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
13
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
17
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
22
class Version20160719090052 extends AbstractMigration implements ContainerAwareInterface
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    public function setContainer(ContainerInterface $container = null)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * @param Schema $schema
36
     */
37
    public function up(Schema $schema)
38
    {
39
        $tokenStorage = $this->getTokenStorage();
40
41
        // Authenticate as management user, so commands requiring the management role can be sent
42
        $tokenStorage->setToken(
43
            new UsernamePasswordToken(
44
                'management',
45
                $this->container->getParameter('management_password'),
46
                'in_memory',
47
                ['ROLE_MANAGEMENT']
48
            )
49
        );
50
51
        $whitelistEntryInstitutions = array_map(
52
            function (WhitelistEntry $whitelistEntry) {
53
                return $whitelistEntry->institution;
54
            },
55
            $this->getWhitelistEntryRepository()->findAll()
56
        );
57
        $institutionListingInstitutions = array_map(
58
            function (InstitutionListing $institutionListing) {
59
                return $institutionListing->institution;
60
            },
61
            $this->getInstitutionListingRepository()->findAll()
62
        );
63
64
        $allInstitutions = array_unique(array_merge($whitelistEntryInstitutions, $institutionListingInstitutions));
65
66
        $pipeline = $this->getPipeline();
67
68
        foreach ($allInstitutions as $institution) {
69
            $createInstitutionConfigurationCommand = new CreateInstitutionConfigurationCommand();
70
            $createInstitutionConfigurationCommand->UUID = (string) Uuid::uuid4();
71
            $createInstitutionConfigurationCommand->institution = $institution->getInstitution();
72
73
            $pipeline->process($createInstitutionConfigurationCommand);
74
        }
75
76
        $tokenStorage->setToken(null);
77
    }
78
79
    /**
80
     * @param Schema $schema
81
     */
82
    public function down(Schema $schema)
83
    {
84
        // No down-migration needed as the structure has not changed.
85
        $this->write('No down migration executed: this was a data only migration.');
86
    }
87
88
    /**
89
     * @return WhitelistEntryRepository
0 ignored issues
show
Documentation introduced by
Should the return type not be WhitelistEntryRepository|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    private function getWhitelistEntryRepository()
92
    {
93
        return $this->container->get('surfnet_stepup_middleware_api.repository.whitelist_entry');
94
    }
95
96
    /**
97
     * @return InstitutionListingRepository
0 ignored issues
show
Documentation introduced by
Should the return type not be InstitutionListingRepository|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    private function getInstitutionListingRepository()
100
    {
101
        return $this->container->get('surfnet_stepup_middleware_api.repository.institution_listing');
102
    }
103
104
    /**
105
     * @return TransactionAwarePipeline
0 ignored issues
show
Documentation introduced by
Should the return type not be TransactionAwarePipeline|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    private function getPipeline()
108
    {
109
        return $this->container->get('pipeline');
110
    }
111
112
    /**
113
     * @return TokenStorage
0 ignored issues
show
Documentation introduced by
Should the return type not be TokenStorage|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
114
     */
115
    private function getTokenStorage()
116
    {
117
        return $this->container->get('security.token_storage');
118
    }
119
}
120