Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetupVersionBase
94.12% covered (success)
94.12%
16 / 17
80.00% covered (warning)
80.00%
4 / 5
7.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVersion
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getCurrentVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 splitStatements
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 migrateIOSystem
n/a
0 / 0
n/a
0 / 0
0
 migrateDatabase
n/a
0 / 0
n/a
0 / 0
0
 migrate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Setup\Versions;
6
7use PeServer\App\Models\AppConfiguration;
8use PeServer\App\Models\Setup\DatabaseSetupArgument;
9use PeServer\App\Models\Setup\IOSetupArgument;
10use PeServer\App\Models\Setup\Versions\Version;
11use PeServer\Core\Database\IDatabaseContext;
12use PeServer\Core\Log\ILogger;
13use PeServer\Core\Log\ILoggerFactory;
14use PeServer\Core\Regex;
15use PeServer\Core\Text;
16use ReflectionClass;
17
18abstract class SetupVersionBase
19{
20    #region variable
21
22    protected ILogger $logger;
23
24    #endregion
25
26    public function __construct(protected AppConfiguration $appConfig, ILoggerFactory $loggerFactory)
27    {
28        $this->logger = $loggerFactory->createLogger($this);
29    }
30
31    #region variable
32
33    /**
34     * [汎用] バージョン取得
35     *
36     * @template T of object
37     * @param string|object $objectOrClassName
38     * @phpstan-param class-string<T>|T $objectOrClassName
39     * @return int
40     */
41    public static function getVersion(string|object $objectOrClassName): int
42    {
43        $rc = new ReflectionClass($objectOrClassName);
44        $attrs = $rc->getAttributes(Version::class);
45        $attr = $attrs[0];
46
47        /** @var Version */
48        $version = $attr->newInstance();
49
50        return $version->version;
51    }
52
53    public function getCurrentVersion(): int
54    {
55        return self::getVersion($this);
56    }
57
58    #endregion
59
60    #region function
61
62    /**
63     * DB問い合わせ文の分割。
64     *
65     * @param string $multiStatement
66     * @return string[]
67     * @phpstan-return literal-string[]
68     */
69    protected function splitStatements(string $multiStatement): array
70    {
71        $regex = new Regex();
72
73        $statements =  $regex->split($multiStatement, '/^\s*;\s*$/m');
74        /** @phpstan-var literal-string[] */
75        $result = [];
76        foreach ($statements as $statement) {
77            if (Text::isNullOrWhiteSpace($statement)) {
78                continue;
79            }
80
81            $result[] = $statement;
82        }
83
84        /** @phpstan-var literal-string[] $result */
85        return $result;
86    }
87
88    abstract protected function migrateIOSystem(IOSetupArgument $argument): void;
89
90    abstract protected function migrateDatabase(DatabaseSetupArgument $argument): void;
91
92    public function migrate(IOSetupArgument $ioSetup, DatabaseSetupArgument $database): void
93    {
94        $this->migrateIOSystem($ioSetup);
95        $this->migrateDatabase($database);
96    }
97
98    #endregion
99}