Completed
Push — feature/institutions-with-pers... ( 5416f5 )
by A.
04:32
created

Configuration::update()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Configuration;
20
21
use Broadway\EventSourcing\EventSourcedAggregateRoot;
22
use GuzzleHttp;
23
use Surfnet\Stepup\Configuration\Api\Configuration as ConfigurationInterface;
24
use Surfnet\Stepup\Configuration\Event\ConfigurationUpdatedEvent;
25
use Surfnet\Stepup\Configuration\Event\EmailTemplatesUpdatedEvent;
26
use Surfnet\Stepup\Configuration\Event\IdentityProvidersUpdatedEvent;
27
use Surfnet\Stepup\Configuration\Event\InstitutionsWithPersonalRaDetailsUpdatedEvent;
28
use Surfnet\Stepup\Configuration\Event\NewConfigurationCreatedEvent;
29
use Surfnet\Stepup\Configuration\Event\ServiceProvidersUpdatedEvent;
30
use Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent;
31
32
class Configuration extends EventSourcedAggregateRoot implements ConfigurationInterface
33
{
34
    /**
35
     * There can ever be only one configuration, so using a fixed UUIDv4
36
     */
37
    const CONFIGURATION_ID = '12345678-abcd-4321-abcd-123456789012';
38
39
    /**
40
     * @var array
41
     */
42
    private $configuration;
43
44
    public static function create()
45
    {
46
        $configuration = new self();
47
        $configuration->apply(new NewConfigurationCreatedEvent(self::CONFIGURATION_ID));
48
49
        return $configuration;
50
    }
51
52
    public function update($configurationAsJson)
53
    {
54
        $decodedConfiguration = GuzzleHttp\json_decode($configurationAsJson, true);
55
56
        $this->apply(new ConfigurationUpdatedEvent(
57
            self::CONFIGURATION_ID,
58
            $decodedConfiguration,
59
            $this->configuration
60
        ));
61
62
        $this->apply(new ServiceProvidersUpdatedEvent(
63
            self::CONFIGURATION_ID,
64
            $decodedConfiguration['gateway']['service_providers']
65
        ));
66
        $this->apply(new IdentityProvidersUpdatedEvent(
67
            self::CONFIGURATION_ID,
68
            $decodedConfiguration['gateway']['identity_providers']
69
        ));
70
        $this->apply(new SraaUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['sraa']));
71
        $this->apply(new EmailTemplatesUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['email_templates']));
72
        $this->apply(new InstitutionsWithPersonalRaDetailsUpdatedEvent(
73
            self::CONFIGURATION_ID,
74
            $decodedConfiguration['institutions_with_personal_ra_details']
75
        ));
76
    }
77
78
    public function getAggregateRootId()
79
    {
80
        return self::CONFIGURATION_ID;
81
    }
82
83
    public function applyConfigurationUpdatedEvent(ConfigurationUpdatedEvent $event)
84
    {
85
        $this->configuration = $event->newConfiguration;
86
    }
87
}
88