SamlEntity::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\GatewayBundle\Entity;
20
21
use Doctrine\ORM\Mapping as ORM;
22
use Ramsey\Uuid\Uuid;
23
use Surfnet\StepupMiddleware\GatewayBundle\Exception\RuntimeException;
24
use function is_string;
25
26
#[ORM\Table]
27
#[ORM\UniqueConstraint(name: 'unq_saml_entity_entity_id_type', columns: ['entity_id', 'type'])]
28
#[ORM\Entity(repositoryClass: SamlEntityRepository::class)]
29
class SamlEntity
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SamlEntity
Loading history...
30
{
31
    /**
32
     * Constants denoting the type of SamlEntity. Also used in the gateway to make that distinction
33
     */
34
    public const TYPE_IDP = 'idp';
35
    public const TYPE_SP = 'sp';
36
37
    /**
38
     * @var string
39
     */
40
    #[ORM\Id]
41
    #[ORM\Column(length: 36)]
42
    public string $id;
43
44
    private function __construct(
45
        #[ORM\Column]
46
        public string $entityId,
47
        #[ORM\Column]
48
        public string $type,
49
        #[ORM\Column(type: 'text')]
50
        public string $configuration,
51
    ) {
52
        $this->id = (string)Uuid::uuid4();
53
    }
54
55
    public static function createServiceProvider(string $entityId, array $configuration): self
56
    {
57
        $encodedConfiguration = json_encode($configuration);
58
        if (!is_string($encodedConfiguration)) {
0 ignored issues
show
introduced by
The condition is_string($encodedConfiguration) is always true.
Loading history...
59
            throw new RuntimeException('Unable to json_encode the configuration array in SamlEntity::createServiceProvider');
60
        }
61
        return new self($entityId, self::TYPE_SP, $encodedConfiguration);
62
    }
63
64
    public static function createIdentityProvider(string $entityId, array $configuration): self
65
    {
66
        $encodedConfiguration = json_encode($configuration);
67
        if (!is_string($encodedConfiguration)) {
0 ignored issues
show
introduced by
The condition is_string($encodedConfiguration) is always true.
Loading history...
68
            throw new RuntimeException('Unable to json_encode the configuration array in SamlEntity::createServiceProvider');
69
        }
70
        return new self($entityId, self::TYPE_IDP, $encodedConfiguration);
71
    }
72
}
73