CookieType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 32
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A fromConfiguration() 0 3 1
A isPersistent() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2022 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\StepupGateway\GatewayBundle\Sso2fa\ValueObject;
20
21
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidCookieTypeException;
22
23
final class CookieType
24
{
25
    // A session cookie has no set expiration date. Once the browser window closes, the cookie is gone.
26
    private const TYPE_SESSION = 'session';
27
    // Persistent cookies have an expiration date, are stored at the client side and are usable until
28
    // the expiration date is reached.
29
    private const TYPE_PERSISTENT = 'persistent';
30
31
    private $type;
32
33
    private function __construct(string $type)
34
    {
35
        $allowedTypes = [self::TYPE_PERSISTENT, self::TYPE_SESSION];
36
        if (!in_array($type, $allowedTypes)) {
37
            throw new InvalidCookieTypeException(
38
                sprintf(
39
                    'The SSO on second factor authentication cookie type must be one of: "%s"',
40
                    implode(', ', $allowedTypes)
41
                )
42
            );
43
        }
44
        $this->type = $type;
45
    }
46
47
    public static function fromConfiguration(string $type): self
48
    {
49
        return new self($type);
50
    }
51
52
    public function isPersistent(): bool
53
    {
54
        return $this->type === self::TYPE_PERSISTENT;
55
    }
56
}
57