OutputCompressionSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getSubscribedEvents() 0 4 1
A onKernelRequest() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeBundle\EventSubscriber;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\RequestEvent;
18
use Symfony\Component\HttpKernel\KernelEvents;
19
20
class OutputCompressionSubscriber implements EventSubscriberInterface
21
{
22
    public function __construct(private readonly bool $useCompression)
23
    {
24
    }
25
26
    public static function getSubscribedEvents(): array
27
    {
28
        return [
29
            KernelEvents::REQUEST => ['onKernelRequest', 1023],
30
        ];
31
    }
32
33
    public function onKernelRequest(RequestEvent $event): void
34
    {
35
        // check if compression is desired
36
        if (!$this->useCompression) {
37
            return;
38
        }
39
40
        if (!$event->isMainRequest()) {
41
            return;
42
        }
43
44
        // check if Zlib extension is available
45
        if (!extension_loaded('zlib')) {
46
            return;
47
        }
48
49
        // set compression on
50
        ini_set('zlib.output_handler', '');
51
        ini_set('zlib.output_compression', 'On');
52
        ini_set('zlib.output_compression_level', 6);
53
    }
54
}
55