SetPhpDirective   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 43
c 3
b 0
f 0
dl 0
loc 71
ccs 21
cts 27
cp 0.7778
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 0 56 6
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @see https://github.com/ecphp
8
 */
9
10
declare(strict_types=1);
11
12
namespace EcPhp\PhpDirectiveBundle\EventListener;
13
14
use InvalidArgumentException;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
17
use Symfony\Component\HttpKernel\Event\RequestEvent;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
use Throwable;
20
21
use const DIRECTORY_SEPARATOR;
22
23
final class SetPhpDirective
24
{
25
    private KernelInterface $kernel;
26
27
    private LoggerInterface $logger;
28
29
    private ParameterBagInterface $parameterBag;
30
31 3
    public function __construct(ParameterBagInterface $parameterBag, LoggerInterface $logger, KernelInterface $kernel)
32
    {
33 3
        $this->parameterBag = $parameterBag;
34 3
        $this->logger = $logger;
35 3
        $this->kernel = $kernel;
36
    }
37
38 3
    public function __invoke(RequestEvent $event): void
39
    {
40 3
        $iniFile = $this->kernel->getProjectDir() .
41
        DIRECTORY_SEPARATOR .
42 3
        $this->parameterBag->get('php_directive')['user_ini_file'];
43
44 3
        $realIniFile = realpath($iniFile);
45
46 3
        if (false === $realIniFile) {
47 1
            throw new InvalidArgumentException('Unable to find ini file: ' . $iniFile);
48
        }
49
50
        try {
51 2
            $parsedIniFile = parse_ini_file($realIniFile);
52 1
        } catch (Throwable $e) {
53 1
            throw new InvalidArgumentException(
54 1
                sprintf(
55
                    'Unable to parse ini file at %s.',
56
                    $realIniFile
57
                )
58
            );
59
        }
60
61 1
        if (false === $parsedIniFile) {
62
            throw new InvalidArgumentException(
63
                sprintf(
64
                    'Unable to parse ini file at %s.',
65
                    $realIniFile
66
                )
67
            );
68
        }
69
70 1
        foreach ($parsedIniFile as $key => $value) {
71 1
            $return = ini_set($key, $value);
72
73 1
            if (false === $return) {
74
                $this
75
                    ->logger
76
                    ->error(
77
                        sprintf(
78
                            'Unable to update PHP property "%s", skipping.',
79
                            $key
80
                        )
81
                    );
82
83
                continue;
84
            }
85
86
            $this
87 1
                ->logger
88 1
                ->debug(
89 1
                    sprintf(
90
                        'PHP property %s updated from %s to %s.',
91
                        $key,
92
                        $return,
93
                        $value
94
                    )
95
                );
96
        }
97
    }
98
}
99