Passed
Pull Request — master (#2285)
by Arnaud
10:02 queued 03:47
created

Generate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 48
ccs 17
cts 20
cp 0.85
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A init() 0 4 2
A process() 0 22 3
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 DI\Attribute\Inject;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Generate pages step.
25
 *
26
 * This step is responsible for generating pages based on the configured generators.
27
 * It initializes the generator manager with the generators defined in the configuration
28
 * and processes them to create the pages. The generated pages are then set in the builder.
29
 */
30
class Generate extends AbstractStep
31
{
32
    #[Inject]
33
    private GeneratorManager $generatorManager;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function getName(): string
39
    {
40 1
        return 'Generating pages';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function init(array $options): void
47
    {
48 1
        if (\count((array) $this->config->get('pages.generators')) > 0) {
49 1
            $this->canProcess = true;
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function process(): void
57
    {
58 1
        $generators = (array) $this->config->get('pages.generators');
59 1
        array_walk($generators, function ($generator, $priority) {
60 1
            if (!class_exists($generator)) {
61 1
                $message = \sprintf('Unable to load generator "%s" (priority: %s).', $generator, $priority);
62 1
                $this->logger->error($message);
63
64 1
                return;
65
            }
66
            // Use DI container to create the generator if possible
67
            try {
68 1
                $generatorInstance = $this->builder->get($generator);
69
            } catch (\Exception $e) {
70
                // Fallback: create manually if not in container
71
                $this->logger->debug(\sprintf('Using fallback for generator "%s": %s', $generator, $e->getMessage()));
72
                $generatorInstance = new $generator($this->builder);
73
            }
74 1
            $this->generatorManager->addGenerator($generatorInstance, $priority);
75 1
        });
76 1
        $pages = $this->generatorManager->process();
77 1
        $this->builder->setPages($pages);
78
    }
79
}
80