Configuration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A applyConfigurationUpdatedEvent() 0 3 1
A getAggregateRootId() 0 3 1
A update() 0 26 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
    public const CONFIGURATION_ID = '12345678-abcd-4321-abcd-123456789012';
37
38
    /**
39
     * @var array
40
     */
41
    private array|null $configuration = null;
42
43
    public static function create(): self
44
    {
45
        $configuration = new self();
46
        $configuration->apply(new NewConfigurationCreatedEvent(self::CONFIGURATION_ID));
47
48
        return $configuration;
49
    }
50
51
    public function update(string $newConfiguration): void
52
    {
53
        $decodedConfiguration = JsonHelper::decode($newConfiguration);
54
55
        $this->apply(
56
            new ConfigurationUpdatedEvent(
57
                self::CONFIGURATION_ID,
58
                $decodedConfiguration,
59
                $this->configuration,
60
            ),
61
        );
62
63
        $this->apply(
64
            new ServiceProvidersUpdatedEvent(
65
                self::CONFIGURATION_ID,
66
                $decodedConfiguration['gateway']['service_providers'],
67
            ),
68
        );
69
        $this->apply(
70
            new IdentityProvidersUpdatedEvent(
71
                self::CONFIGURATION_ID,
72
                $decodedConfiguration['gateway']['identity_providers'],
73
            ),
74
        );
75
        $this->apply(new SraaUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['sraa']));
76
        $this->apply(new EmailTemplatesUpdatedEvent(self::CONFIGURATION_ID, $decodedConfiguration['email_templates']));
77
    }
78
79
    public function getAggregateRootId(): string
80
    {
81
        return self::CONFIGURATION_ID;
82
    }
83
84
    public function applyConfigurationUpdatedEvent(ConfigurationUpdatedEvent $event): void
85
    {
86
        $this->configuration = $event->newConfiguration;
87
    }
88
}
89