Completed
Push — develop ( b0b139...6288f9 )
by A.
9s
created

Configuration::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 1
eloc 14
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 Surfnet\Stepup\Configuration\Api\Configuration as ConfigurationInterface;
23
use Surfnet\Stepup\Configuration\Event\ConfigurationUpdatedEvent;
24
use Surfnet\Stepup\Configuration\Event\EmailTemplatesUpdatedEvent;
25
use Surfnet\Stepup\Configuration\Event\IdentityProvidersUpdatedEvent;
26
use Surfnet\Stepup\Configuration\Event\NewConfigurationCreatedEvent;
27
use Surfnet\Stepup\Configuration\Event\ServiceProvidersUpdatedEvent;
28
use Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent;
29
use Surfnet\Stepup\Helper\JsonHelper;
30
31
class Configuration extends EventSourcedAggregateRoot implements ConfigurationInterface
32
{
33
    /**
34
     * There can ever be only one configuration, so using a fixed UUIDv4
35
     */
36
    const CONFIGURATION_ID = '12345678-abcd-4321-abcd-123456789012';
37
38
    /**
39
     * @var array
40
     */
41
    private $configuration;
42
43
    public static function create()
44
    {
45
        $configuration = new self();
46
        $configuration->apply(new NewConfigurationCreatedEvent(self::CONFIGURATION_ID));
47
48
        return $configuration;
49
    }
50
51
    public function update($configurationAsJson)
52
    {
53
        $decodedConfiguration = JsonHelper::decode($configurationAsJson);
54
55
        $this->apply(new ConfigurationUpdatedEvent(
56
            self::CONFIGURATION_ID,
57
            $decodedConfiguration,
58
            $this->configuration
59
        ));
60
61
        $this->apply(new ServiceProvidersUpdatedEvent(
62
            self::CONFIGURATION_ID,
63
            $decodedConfiguration['gateway']['service_providers']
64
        ));
65
        $this->apply(new IdentityProvidersUpdatedEvent(
66
            self::CONFIGURATION_ID,
67
            $decodedConfiguration['gateway']['identity_providers']
68
        ));
69
        $this->apply(new SraaUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['sraa']));
70
        $this->apply(new EmailTemplatesUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['email_templates']));
71
    }
72
73
    public function getAggregateRootId()
74
    {
75
        return self::CONFIGURATION_ID;
76
    }
77
78
    public function applyConfigurationUpdatedEvent(ConfigurationUpdatedEvent $event)
79
    {
80
        $this->configuration = $event->newConfiguration;
81
    }
82
}
83