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

Generate::process()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 1
nop 0
dl 0
loc 22
ccs 12
cts 15
cp 0.8
crap 3.072
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Step\Pages;
15
16
use Cecil\Builder;
17
use Cecil\Config;
18
use Cecil\Generator\GeneratorManager;
19
use Cecil\Step\AbstractStep;
20
use Psr\Log\LoggerInterface;
21
22
/**
23
 * Generate pages step.
24
 *
25
 * This step is responsible for generating pages based on the configured generators.
26
 * It initializes the generator manager with the generators defined in the configuration
27
 * and processes them to create the pages. The generated pages are then set in the builder.
28
 */
29
class Generate extends AbstractStep
30
{
31
    private GeneratorManager $generatorManager;
32
33 1
    public function __construct(
34
        Builder $builder,
35
        Config $config,
36
        LoggerInterface $logger,
37
        GeneratorManager $generatorManager
38
    ) {
39 1
        parent::__construct($builder, $config, $logger);
40 1
        $this->generatorManager = $generatorManager;
41
    }
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function getName(): string
46
    {
47 1
        return 'Generating pages';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function init(array $options): void
54
    {
55 1
        if (\count((array) $this->config->get('pages.generators')) > 0) {
56 1
            $this->canProcess = true;
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function process(): void
64
    {
65 1
        $generators = (array) $this->config->get('pages.generators');
66 1
        array_walk($generators, function ($generator, $priority) {
67 1
            if (!class_exists($generator)) {
68 1
                $message = \sprintf('Unable to load generator "%s" (priority: %s).', $generator, $priority);
69 1
                $this->logger->error($message);
70
71 1
                return;
72
            }
73
            // Use DI container to create the generator if possible
74
            try {
75 1
                $generatorInstance = $this->builder->get($generator);
76
            } catch (\Exception $e) {
77
                // Fallback: create manually if not in container
78
                $this->logger->debug(\sprintf('Using fallback for generator "%s": %s', $generator, $e->getMessage()));
79
                $generatorInstance = new $generator($this->builder);
80
            }
81 1
            $this->generatorManager->addGenerator($generatorInstance, $priority);
82 1
        });
83 1
        $pages = $this->generatorManager->process();
84 1
        $this->builder->setPages($pages);
85
    }
86
}
87