• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-solver / 13271078584

11 Feb 2025 07:36PM UTC coverage: 88.153% (+0.4%) from 87.8%
13271078584

push

github

web-flow
Merge pull request #294 from eliashaeussler/feature/generic-solution

[!!!][FEATURE] Provide generic class for generated solutions

86 of 87 new or added lines in 7 files covered. (98.85%)

878 of 996 relevant lines covered (88.15%)

2.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/Classes/Cache/Serializer/SolutionSerializer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "solver".
7
 *
8
 * Copyright (C) 2023-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3Solver\Cache\Serializer;
25

26
use EliasHaeussler\Typo3Solver\Configuration;
27
use EliasHaeussler\Typo3Solver\ProblemSolving;
28

29
/**
30
 * SolutionSerializer
31
 *
32
 * @author Elias Häußler <elias@haeussler.dev>
33
 * @license GPL-2.0-or-later
34
 *
35
 * @internal
36
 *
37
 * @phpstan-import-type SolutionArray from ProblemSolving\Solution\Solution as SolutionSubArray
38
 * @phpstan-type SolutionArray array{
39
 *     solution: SolutionSubArray,
40
 *     createdAt: int,
41
 *     validUntil: int,
42
 * }
43
 */
44
final class SolutionSerializer
45
{
46
    private readonly Configuration\Configuration $configuration;
47

48
    public function __construct()
3✔
49
    {
50
        $this->configuration = new Configuration\Configuration();
3✔
51
    }
52

53
    /**
54
     * @phpstan-return SolutionArray
55
     */
56
    public function serialize(ProblemSolving\Solution\Solution $solution): array
1✔
57
    {
58
        $now = time();
1✔
59

60
        return [
1✔
61
            'solution' => $solution->toArray(),
1✔
62
            'createdAt' => $now,
1✔
63
            'validUntil' => $now + $this->configuration->getCacheLifetime(),
1✔
64
        ];
1✔
65
    }
66

67
    /**
68
     * @phpstan-param SolutionArray $solutionArray
69
     */
70
    public function deserialize(array $solutionArray): ?ProblemSolving\Solution\Solution
2✔
71
    {
72
        [
2✔
73
            'solution' => $solution,
2✔
74
            'createdAt' => $createdAt,
2✔
75
            'validUntil' => $validUntil,
2✔
76
        ] = $solutionArray;
2✔
77

78
        if ($validUntil < time()) {
2✔
79
            return null;
1✔
80
        }
81

82
        // BC layer: We cannot handle legacy cache entries, therefore return early
83
        // @todo Remove with next major release
84
        /* @phpstan-ignore function.alreadyNarrowedType, nullCoalesce.offset */
85
        if (!\is_array($solution['responses'] ?? null)) {
1✔
NEW
86
            return null;
×
87
        }
88

89
        return ProblemSolving\Solution\Solution::fromArray($solution)
1✔
90
            ->setCreateDate(new \DateTimeImmutable('@' . $createdAt))
1✔
91
        ;
1✔
92
    }
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc