Passed
Push — di ( 61d74f...f00c6d )
by Arnaud
15:03 queued 11:43
created

GeneratorManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Generator;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Collection as PagesCollection;
18
use Cecil\Util;
19
20
/**
21
 * GeneratorManager class.
22
 *
23
 * This class manages the generators used in the Cecil build process.
24
 * It extends \SplPriorityQueue to allow generators to be processed in order of priority.
25
 * Generators can be added with a specific priority, and the process method will run each generator
26
 * in order, collecting the pages they generate.
27
 */
28
class GeneratorManager extends \SplPriorityQueue
29
{
30
    /** @var Builder */
31
    protected $builder;
32
33
    /**
34
     * @param Builder $builder
35
     *
36
     * @return void
37
     */
38
    public function __construct(Builder $builder)
39
    {
40
        $this->builder = $builder;
41
    }
42
43
    /**
44
     * Adds a generator.
45
     */
46
    public function addGenerator(GeneratorInterface $generator, int $priority = 1): self
47
    {
48
        $this->insert($generator, $priority);
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function compare($priority1, $priority2): int
57
    {
58
        if ($priority1 === $priority2) {
59
            return 0;
60
        }
61
62
        return $priority1 > $priority2 ? -1 : 1;
63
    }
64
65
    /**
66
     * Process each generator.
67
     */
68
    public function process(): PagesCollection
69
    {
70
        /** @var PagesCollection $pagesCollection */
71
        $pagesCollection = $this->builder->getPages();
72
        if ($pagesCollection === null) {
73
            $pagesCollection = new PagesCollection('pages');
74
        }
75
        $total = $this->count();
76
77
        if ($total > 0) {
78
            $this->top();
79
            while ($this->valid()) {
80
                $count = $total - $this->key();
81
                $countPagesAdded = $countPagesUpdated = 0;
82
                /** @var AbstractGenerator $generator */
83
                $generator = $this->current();
84
                /** @var PagesCollection $generatedPages */
85
                $generatedPages = $generator->runGenerate();
86
                foreach ($generatedPages as $page) {
87
                    /** @var \Cecil\Collection\Page\Page $page */
88
                    try {
89
                        $pagesCollection->add($page);
90
                        $countPagesAdded++;
91
                    } catch (\DomainException) {
92
                        $pagesCollection->replace($page->getId(), $page);
93
                        $countPagesUpdated++;
94
                    }
95
                }
96
                $message = \sprintf('%s "%s" pages generated and %s pages updated', $countPagesAdded, Util::formatClassName($generator), $countPagesUpdated);
97
                $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
98
99
                $this->next();
100
            }
101
        }
102
103
        return $pagesCollection;
104
    }
105
}
106