Skip to content

Commit deed44d

Browse files
committed
fixup! Use composition instead of inheritance in HttpCodeActivationStrategy
1 parent d013660 commit deed44d

File tree

4 files changed

+56
-167
lines changed

4 files changed

+56
-167
lines changed

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/HttpCodeActivationStrategy.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,36 @@
1111

1212
namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
1415
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1516
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\HttpKernel\Exception\HttpException;
1718

18-
trigger_deprecation('symfony/monolog-bridge', '5.1', 'The "%s" class is deprecated, use "%s" instead.', HttpCodeActivationStrategy::class, HttpCodeActivationStrategyDecorator::class);
19-
2019
/**
2120
* Activation strategy that ignores certain HTTP codes.
2221
*
2322
* @author Shaun Simmons <shaun@envysphere.com>
24-
*
25-
* @deprecated since Symfony 5.1, use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategyDecorator instead.
23+
* @author Pierrick Vignand <pierrick.vignand@gmail.com>
2624
*/
27-
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy
25+
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
2826
{
27+
private $inner;
2928
private $exclusions;
3029
private $requestStack;
3130

3231
/**
3332
* @param array $exclusions each exclusion must have a "code" and "urls" keys
33+
* @param ActivationStrategyInterface|int|string $inner an ActivationStrategyInterface to decorate
3434
*/
35-
public function __construct(RequestStack $requestStack, array $exclusions, $actionLevel)
35+
public function __construct(RequestStack $requestStack, array $exclusions, $inner)
3636
{
37+
if (!$inner instanceof ActivationStrategyInterface) {
38+
trigger_deprecation('symfony/monolog-bridge', '5.2', 'Passing an actionLevel (int|string) as constructor\'s 3rd argument of "%s" is deprecated, "%s" expected.', __CLASS__, ActivationStrategyInterface::class);
39+
40+
$actionLevel = $inner;
41+
$inner = new ErrorLevelActivationStrategy($actionLevel);
42+
}
43+
3744
foreach ($exclusions as $exclusion) {
3845
if (!\array_key_exists('code', $exclusion)) {
3946
throw new \LogicException(sprintf('An exclusion must have a "code" key.'));
@@ -43,15 +50,14 @@ public function __construct(RequestStack $requestStack, array $exclusions, $acti
4350
}
4451
}
4552

46-
parent::__construct($actionLevel);
47-
53+
$this->inner = $inner;
4854
$this->requestStack = $requestStack;
4955
$this->exclusions = $exclusions;
5056
}
5157

5258
public function isHandlerActivated(array $record): bool
5359
{
54-
$isActivated = parent::isHandlerActivated($record);
60+
$isActivated = $this->inner->isHandlerActivated($record);
5561

5662
if (
5763
$isActivated

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/HttpCodeActivationStrategyDecorator.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/Symfony/Bridge/Monolog/Tests/Handler/FingersCrossed/HttpCodeActivationStrategyDecoratorTest.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/Symfony/Bridge/Monolog/Tests/Handler/FingersCrossed/HttpCodeActivationStrategyTest.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;
1313

14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1415
use Monolog\Logger;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy;
@@ -23,7 +24,7 @@ class HttpCodeActivationStrategyTest extends TestCase
2324
/**
2425
* @group legacy
2526
*/
26-
public function testExclusionsWithoutCode()
27+
public function testExclusionsWithoutCodeLegacy(): void
2728
{
2829
$this->expectException('LogicException');
2930
new HttpCodeActivationStrategy(new RequestStack(), [['urls' => []]], Logger::WARNING);
@@ -32,7 +33,7 @@ public function testExclusionsWithoutCode()
3233
/**
3334
* @group legacy
3435
*/
35-
public function testExclusionsWithoutUrls()
36+
public function testExclusionsWithoutUrlsLegacy(): void
3637
{
3738
$this->expectException('LogicException');
3839
new HttpCodeActivationStrategy(new RequestStack(), [['code' => 404]], Logger::WARNING);
@@ -43,7 +44,7 @@ public function testExclusionsWithoutUrls()
4344
*
4445
* @group legacy
4546
*/
46-
public function testIsActivated($url, $record, $expected)
47+
public function testIsActivatedLegacy($url, $record, $expected): void
4748
{
4849
$requestStack = new RequestStack();
4950
$requestStack->push(Request::create($url));
@@ -59,10 +60,44 @@ public function testIsActivated($url, $record, $expected)
5960
Logger::WARNING
6061
);
6162

62-
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
63+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
64+
}
65+
66+
public function testExclusionsWithoutCode(): void
67+
{
68+
$this->expectException('LogicException');
69+
new HttpCodeActivationStrategy(new RequestStack(), [['urls' => []]], new ErrorLevelActivationStrategy(Logger::WARNING));
70+
}
71+
72+
public function testExclusionsWithoutUrls(): void
73+
{
74+
$this->expectException('LogicException');
75+
new HttpCodeActivationStrategy(new RequestStack(), [['code' => 404]], new ErrorLevelActivationStrategy(Logger::WARNING));
76+
}
77+
78+
/**
79+
* @dataProvider isActivatedProvider
80+
*/
81+
public function testIsActivated($url, $record, $expected)
82+
{
83+
$requestStack = new RequestStack();
84+
$requestStack->push(Request::create($url));
85+
86+
$strategy = new HttpCodeActivationStrategy(
87+
$requestStack,
88+
[
89+
['code' => 403, 'urls' => []],
90+
['code' => 404, 'urls' => []],
91+
['code' => 405, 'urls' => []],
92+
['code' => 400, 'urls' => ['^/400/a', '^/400/b']],
93+
],
94+
new ErrorLevelActivationStrategy(Logger::WARNING)
95+
);
96+
97+
self::assertEquals($expected, $strategy->isHandlerActivated($record));
6398
}
6499

65-
public function isActivatedProvider()
100+
public function isActivatedProvider(): array
66101
{
67102
return [
68103
['/test', ['level' => Logger::ERROR], true],
@@ -78,7 +113,7 @@ public function isActivatedProvider()
78113
];
79114
}
80115

81-
protected function getContextException($code)
116+
private function getContextException(int $code): array
82117
{
83118
return ['exception' => new HttpException($code)];
84119
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy