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

eliashaeussler / typo3-config-objects / 14489909490

16 Apr 2025 09:55AM UTC coverage: 62.195% (-37.8%) from 100.0%
14489909490

Pull #1

github

eliashaeussler
[FEATURE] Add configuration object for request middlewares
Pull Request #1: [FEATURE] Add configuration object for request middlewares

0 of 62 new or added lines in 3 files covered. (0.0%)

102 of 164 relevant lines covered (62.2%)

2.02 hits per line

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

0.0
/src/ValueObject/RequestMiddleware.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/typo3-config-objects".
7
 *
8
 * Copyright (C) 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\Typo3ConfigObjects\ValueObject;
25

26
use EliasHaeussler\Typo3ConfigObjects\Contracts;
27
use EliasHaeussler\Typo3ConfigObjects\Exception;
28
use Psr\Http\Server;
29

30
/**
31
 * RequestMiddleware.
32
 *
33
 * @author Elias Häußler <elias@haeussler.dev>
34
 * @license GPL-2.0-or-later
35
 *
36
 * @phpstan-type MiddlewareOptions array{
37
 *     target?: class-string<Server\MiddlewareInterface>,
38
 *     disabled?: bool,
39
 *     before?: list<non-empty-string>,
40
 *     after?: list<non-empty-string>,
41
 * }
42
 *
43
 * @implements Contracts\Arrayable<MiddlewareOptions>
44
 */
45
final class RequestMiddleware implements Contracts\Arrayable
46
{
47
    /**
48
     * @var class-string<Server\MiddlewareInterface>|null
49
     */
50
    private ?string $target = null;
51
    private ?bool $disabled = null;
52

53
    /**
54
     * @var array{before: list<non-empty-string>, after: list<non-empty-string>}
55
     */
56
    private array $dependencies = [
57
        'before' => [],
58
        'after' => [],
59
    ];
60

61
    /**
62
     * @param non-empty-string $name
63
     */
NEW
64
    public function __construct(
×
65
        public readonly string $name,
NEW
66
    ) {}
×
67

68
    /**
69
     * @param non-empty-string $name
70
     */
NEW
71
    public static function create(string $name): self
×
72
    {
NEW
73
        return new self($name);
×
74
    }
75

76
    /**
77
     * @param class-string<Server\MiddlewareInterface> $target
78
     */
NEW
79
    public function setTarget(string $target): self
×
80
    {
NEW
81
        $this->target = $target;
×
82

NEW
83
        return $this;
×
84
    }
85

NEW
86
    public function disable(): self
×
87
    {
NEW
88
        $this->disabled = true;
×
89

NEW
90
        return $this;
×
91
    }
92

NEW
93
    public function enable(): self
×
94
    {
NEW
95
        $this->disabled = false;
×
96

NEW
97
        return $this;
×
98
    }
99

100
    /**
101
     * @param non-empty-string ...$dependencies
102
     */
NEW
103
    public function before(string ...$dependencies): self
×
104
    {
NEW
105
        foreach ($dependencies as $dependency) {
×
NEW
106
            $this->dependencies['before'][] = $dependency;
×
107
        }
108

NEW
109
        return $this;
×
110
    }
111

112
    /**
113
     * @param non-empty-string ...$dependencies
114
     */
NEW
115
    public function after(string ...$dependencies): self
×
116
    {
NEW
117
        foreach ($dependencies as $dependency) {
×
NEW
118
            $this->dependencies['after'][] = $dependency;
×
119
        }
120

NEW
121
        return $this;
×
122
    }
123

124
    /**
125
     * @throws Exception\MiddlewareTargetIsMissing
126
     */
NEW
127
    public function toArray(): array
×
128
    {
NEW
129
        $this->validate();
×
130

NEW
131
        $middlewareArray = [];
×
132

NEW
133
        if (null !== $this->target) {
×
NEW
134
            $middlewareArray['target'] = $this->target;
×
135
        }
NEW
136
        if (null !== $this->disabled) {
×
NEW
137
            $middlewareArray['disabled'] = $this->disabled;
×
138
        }
NEW
139
        if ([] !== $this->dependencies['before']) {
×
NEW
140
            $middlewareArray['before'] = $this->dependencies['before'];
×
141
        }
NEW
142
        if ([] !== $this->dependencies['after']) {
×
NEW
143
            $middlewareArray['after'] = $this->dependencies['after'];
×
144
        }
145

NEW
146
        return $middlewareArray;
×
147
    }
148

149
    /**
150
     * @throws Exception\MiddlewareTargetIsMissing
151
     */
NEW
152
    private function validate(): void
×
153
    {
NEW
154
        if (null === $this->target && true !== $this->disabled) {
×
NEW
155
            throw new Exception\MiddlewareTargetIsMissing();
×
156
        }
157
    }
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc