Generate::process()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
ccs 12
cts 20
cp 0.6
crap 3.576
rs 9.6666
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; fail loudly if it cannot be resolved
67
            try {
68 1
                $generatorInstance = $this->builder->get($generator);
69
            } catch (\Throwable $e) {
70
                $this->logger->error(\sprintf(
71
                    'Unable to instantiate generator "%s" (priority: %s): %s',
72
                    $generator,
73
                    $priority,
74
                    $e->getMessage()
75
                ));
76
77
                throw $e;
78
            }
79 1
            $this->generatorManager->addGenerator($generatorInstance, $priority);
80 1
        });
81 1
        $pages = $this->generatorManager->process();
82 1
        $this->builder->setPages($pages);
83
    }
84
}
85