Passed
Push — php-di ( 14dc40...53e65d )
by Arnaud
21:01 queued 17:21
created

ContainerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 43
ccs 10
cts 14
cp 0.7143
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 33 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cecil\Container;
6
7
use Cecil\Config;
8
use DI\Container;
9
use DI\ContainerBuilder;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * Factory to create and configure the dependency injection container.
14
 *
15
 * Uses PHP-DI for automatic autowiring and simple configuration.
16
 *
17
 * @see https://php-di.org/
18
 */
19
class ContainerFactory
20
{
21
    /**
22
     * Creates and configures the DI container with Cecil dependencies.
23
     *
24
     * @param Config          $config Application configuration
25
     * @param LoggerInterface $logger Application logger
26
     *
27
     * @return Container The configured and ready-to-use container
28
     */
29 1
    public static function create(
30
        Config $config,
31
        LoggerInterface $logger
32
    ): Container {
33 1
        $builder = new ContainerBuilder();
34
35
        // Load dependencies configuration
36 1
        $definitionsFile = __DIR__ . '/../../config/dependencies.php';
37 1
        if (file_exists($definitionsFile)) {
38 1
            $builder->addDefinitions($definitionsFile);
39
        }
40
41
        // Enable compilation cache in production
42 1
        if (!$config->get('debug')) {
43
            $cacheDir = $config->getCachePath() . '/di';
44
            if (!is_dir($cacheDir)) {
45
                mkdir($cacheDir, 0755, true);
46
            }
47
            $builder->enableCompilation($cacheDir);
48
        }
49
50
        // Build the container
51 1
        $container = $builder->build();
52
53
        // Inject Config and Logger instances from Builder
54
        // These objects are already instantiated and configured
55 1
        $container->set(Config::class, $config);
56 1
        $container->set(LoggerInterface::class, $logger);
57
58
        // Note: Builder cannot be injected here because it creates the container itself
59
        // Services that need Builder receive it as a constructor parameter
60
61 1
        return $container;
62
    }
63
}
64