Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
48 / 50
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreConfiguration
96.00% covered (success)
96.00%
48 / 50
60.00% covered (warning)
60.00%
3 / 5
12
0.00% covered (danger)
0.00%
0 / 1
 mergeCookie
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getCookie
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
4.00
 getTemporary
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
2.00
 getSession
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 build
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models;
6
7use DateInterval;
8use PeServer\App\Models\AppConfiguration;
9use PeServer\App\Models\Configuration\CookieStoreSetting;
10use PeServer\App\Models\Configuration\SessionStoreSetting;
11use PeServer\App\Models\Configuration\StoreSetting;
12use PeServer\App\Models\Configuration\TemporaryStoreSetting;
13use PeServer\Core\Collection\Arr;
14use PeServer\Core\Store\CookieOptions;
15use PeServer\Core\Store\SessionOptions;
16use PeServer\Core\Store\SpecialStore;
17use PeServer\Core\Store\StoreOptions;
18use PeServer\Core\Store\TemporaryOptions;
19use PeServer\Core\Text;
20use PeServer\Core\Time;
21
22abstract class StoreConfiguration
23{
24    #region function
25
26    /**
27     * Undocumented function
28     *
29     * @param CookieOptions $base
30     * @param CookieStoreSetting $setting
31     * @return CookieOptions
32     */
33    private static function mergeCookie(CookieStoreSetting $setting, CookieOptions $base): CookieOptions
34    {
35        // //get_object_vars($base);
36        // $baseSetting = new CookieStoreSetting($base);
37
38        return new CookieOptions(
39            Text::requireNotNullOrWhiteSpace($setting->path, $base->path),
40            Text::isNullOrWhiteSpace($setting->span) ? $base->span : Time::create($setting->span),
41            $setting->secure === null ? $base->secure : $setting->secure,
42            $setting->httpOnly === null ? $base->httpOnly : $setting->httpOnly,
43            Text::requireNotNullOrWhiteSpace($setting->sameSite, $base->sameSite) //@phpstan-ignore-line not null
44        );
45    }
46
47    /**
48     * クッキー設定を取得。
49     *
50     * @param CookieStoreSetting $setting
51     * @return CookieOptions
52     */
53    public static function getCookie(CookieStoreSetting $setting): CookieOptions
54    {
55        /** @var DateInterval|null */
56        $span = null;
57        if (!Text::isNullOrWhiteSpace($setting->span)) {
58            $span = Time::create($setting->span);
59        }
60
61        $path = Text::requireNotNullOrWhiteSpace($setting->path, '/');
62        $secure = $setting->secure === null ? false : $setting->secure;
63        $httpOnly = $setting->httpOnly === null ? true : $setting->httpOnly;
64        /** @phpstan-var CookieSameSiteAlias */
65        $sameSite = Text::requireNotNullOrWhiteSpace($setting->sameSite, 'None');
66
67        $options = new CookieOptions(
68            $path,
69            $span,
70            $secure,
71            $httpOnly,
72            (string)$sameSite
73        );
74
75        return $options;
76    }
77
78    /**
79     * 一時データ設定を取得。
80     *
81     * @param TemporaryStoreSetting $setting
82     * @param CookieOptions $cookie
83     * @return TemporaryOptions
84     */
85    public static function getTemporary(TemporaryStoreSetting $setting, CookieOptions $cookie): TemporaryOptions
86    {
87        $overwriteCookie = self::mergeCookie($setting->cookie, $cookie);
88        if ($overwriteCookie->span === null) {
89            $overwriteCookie->span = new DateInterval('PT30M');
90        }
91
92        $name = Text::requireNotNullOrWhiteSpace($setting->name, 'TEMP');
93        $save = Text::requireNotNullOrWhiteSpace($setting->save, './temp');
94        $options = new TemporaryOptions(
95            $name,
96            $save,
97            $overwriteCookie
98        );
99
100        return $options;
101    }
102
103    /**
104     * セッション設定を取得。
105     *
106     * @param SessionStoreSetting $setting
107     * @param CookieOptions $cookie
108     * @return SessionOptions
109     */
110    public static function getSession(SessionStoreSetting $setting, CookieOptions $cookie): SessionOptions
111    {
112        $overwriteCookie = self::mergeCookie($setting->cookie, $cookie);
113
114        /** @var non-empty-string $name */
115        $name = Text::requireNotNullOrWhiteSpace($setting->name, SessionOptions::DEFAULT_NAME);
116        $save = Text::requireNotNullOrWhiteSpace($setting->save, Text::EMPTY);
117
118        $options = new SessionOptions(
119            $name,
120            $save,
121            $overwriteCookie
122        );
123
124        return $options;
125    }
126
127    /**
128     * ストア情報取得。
129     *
130     * @param StoreSetting $setting;
131     * @return StoreOptions
132     */
133    public static function build(StoreSetting $setting): StoreOptions
134    {
135        $cookie = self::getCookie($setting->cookie);
136        $temporary = self::getTemporary($setting->temporary, $cookie);
137        $session = self::getSession($setting->session, $cookie);
138
139        return new StoreOptions(
140            $cookie,
141            $temporary,
142            $session
143        );
144    }
145
146    #endregion
147}