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

eliashaeussler / typo3-badges / 9728026790

30 Jun 2024 12:37AM UTC coverage: 98.947% (-0.2%) from 99.165%
9728026790

push

github

web-flow
[TASK] Update all dependencies

470 of 475 relevant lines covered (98.95%)

7.47 hits per line

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

97.37
/src/Badge/Provider/BadgenBadgeProvider.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Symfony project "eliashaeussler/typo3-badges".
7
 *
8
 * Copyright (C) 2021-2024 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 3 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 App\Badge\Provider;
25

26
use App\Entity\Badge;
27
use App\Enums\Color;
28
use Nyholm\Psr7\Uri;
29
use Override;
30
use Symfony\Component\HttpFoundation\JsonResponse;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
33
use Symfony\Component\Routing\Route;
34
use Symfony\Component\Routing\RouterInterface;
35

36
use function array_map;
37

38
/**
39
 * BadgenBadgeProvider.
40
 *
41
 * @author Elias Häußler <elias@haeussler.dev>
42
 * @license GPL-3.0-or-later
43
 */
44
final class BadgenBadgeProvider implements BadgeProvider
45
{
46
    use RoutingTrait;
47

48
    public const string IDENTIFIER = 'badgen';
49

50
    private const string BADGE_URL_PATTERN = 'https://badgen.net/badge/{subject}/{status}/{color}';
51
    private const string ENDPOINT_URL_PATTERN = 'https://badgen.net/https/{host}/{path}';
52

53
    public function __construct(
10✔
54
        RouterInterface $router,
55
    ) {
56
        $this->router = $router;
10✔
57
    }
58

59
    #[Override]
55✔
60
    public static function getIdentifier(): string
61
    {
62
        return self::IDENTIFIER;
55✔
63
    }
64

65
    #[Override]
1✔
66
    public static function getName(): string
67
    {
68
        return 'Badgen';
1✔
69
    }
70

71
    #[Override]
4✔
72
    public function createResponse(Badge $badge): Response
73
    {
74
        return new JsonResponse([
4✔
75
            'subject' => $badge->getLabel(),
4✔
76
            'status' => $badge->getMessage(),
4✔
77
            'color' => $this->getColorValue($badge->getColor()),
4✔
78
        ]);
4✔
79
    }
80

81
    #[Override]
2✔
82
    public function generateUriForRoute(Route|string $route, array $routeParameters = []): string
83
    {
84
        // Enforce provider parameter
85
        $routeParameters['provider'] = self::IDENTIFIER;
2✔
86

87
        if ($route instanceof Route) {
2✔
88
            $routeName = $this->identifyRoute($route);
1✔
89
        } else {
90
            $routeName = $route;
1✔
91
        }
92

93
        $appUrl = new Uri($this->router->generate($routeName, $routeParameters, UrlGeneratorInterface::ABSOLUTE_URL));
2✔
94

95
        return strtr(self::ENDPOINT_URL_PATTERN, [
2✔
96
            '{host}' => $appUrl->getHost().(null !== $appUrl->getPort() ? ':'.$appUrl->getPort() : ''),
2✔
97
            '{path}' => ltrim($appUrl->getPath(), '/'),
2✔
98
        ]);
2✔
99
    }
100

101
    #[Override]
1✔
102
    public function generateUriForBadge(Badge $badge): string
103
    {
104
        $urlParameters = [
1✔
105
            '{subject}' => $badge->getLabel(),
1✔
106
            '{status}' => $badge->getMessage(),
1✔
107
            '{color}' => $this->getColorValue($badge->getColor()),
1✔
108
        ];
1✔
109

110
        return strtr(self::BADGE_URL_PATTERN, array_map('rawurlencode', $urlParameters));
1✔
111
    }
112

113
    /**
114
     * @codeCoverageIgnore
115
     */
116
    #[Override]
117
    public function getUrlPattern(): string
118
    {
119
        return self::ENDPOINT_URL_PATTERN;
120
    }
121

122
    #[Override]
1✔
123
    public function getProviderUrl(): string
124
    {
125
        return 'https://badgen.net';
1✔
126
    }
127

128
    private function getColorValue(Color $color): string
5✔
129
    {
130
        return match ($color) {
131
            Color::Blue => 'blue',
5✔
132
            Color::Gray => 'grey',
4✔
133
            Color::Green => 'green',
4✔
134
            Color::Orange => 'orange',
4✔
135
            Color::Red => 'red',
×
136
            Color::Yellow => 'yellow',
5✔
137
        };
138
    }
139
}
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