Passed
Branch master (31dbc0)
by Wilder
01:22
created

CookieTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ElePHPant\Cookie\Cookie;
4
5
6
use ElePHPant\Cookie\Storage\{CookieStorage, CookieStorageInterface};
7
use ElePHPant\Cookie\Strategies\Encryption\EncryptionStrategyInterface;
8
use ElePHPant\Cookie\ValueObjects\{Option, Expiration};
9
10
/**
11
 * Class CookieTrait
12
 *
13
 * Please report bugs on https://github.com/wilderamorim/cookie/issues
14
 *
15
 * @author Wilder Amorim <https://github.com/wilderamorim>
16
 * @link https://www.linkedin.com/in/wilderamorim/
17
 */
18
trait CookieTrait
19
{
20
    /** @var array Stores the option array. */
21
    protected static array $options = [];
22
23
    /** @var bool Indicates if encryption is enabled */
24
    protected static bool $isEncrypted = false;
25
26
    /** @var CookieStorageInterface Storage object for managing cookies. */
27
    protected static CookieStorageInterface $storage;
28
29
    /** @var EncryptionStrategyInterface|null Encryption strategy object. */
30
    protected static ?EncryptionStrategyInterface $encryptionStrategy;
31
32
    /** @var array|string[] Stores the default values for option array. */
33
    private static array $defaultValues = ['expiration' => 'minutes'];
34
35
    /**
36
     * Process the option array.
37
     *
38
     * @param array $options The option array.
39
     */
40
    public function __construct(array $options)
41
    {
42
        self::boot($options);
43
    }
44
45
    /**
46
     * Initialize the trait by bootstrapping the necessary components.
47
     *
48
     * @param array $options The option array.
49
     */
50
    private static function boot(array $options): void
51
    {
52
        $options = array_replace(self::$defaultValues, $options);
53
54
        self::$options = (new Option($options))->toArray();
55
        self::$storage = new CookieStorage(self::$options);
56
57
        if (!empty(self::$options['encryption'])) {
58
            self::$isEncrypted = true;
59
            self::$encryptionStrategy = new self::$options['encryption'](self::$options);
60
        }
61
    }
62
}