GeneratorManager::compare()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
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\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 1
    public function __construct(Builder $builder)
39
    {
40 1
        $this->builder = $builder;
41
    }
42
43
    /**
44
     * Adds a generator.
45
     */
46 1
    public function addGenerator(GeneratorInterface $generator, int $priority = 1): self
47
    {
48 1
        $this->insert($generator, $priority);
49
50 1
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function compare($priority1, $priority2): int
57
    {
58 1
        if ($priority1 === $priority2) {
59 1
            return 0;
60
        }
61
62 1
        return $priority1 > $priority2 ? -1 : 1;
63
    }
64
65
    /**
66
     * Process each generator.
67
     */
68 1
    public function process(): PagesCollection
69
    {
70
        /** @var PagesCollection $pagesCollection */
71 1
        $pagesCollection = $this->builder->getPages();
72 1
        $total = $this->count();
73
74 1
        if ($total > 0) {
75 1
            $this->top();
76 1
            while ($this->valid()) {
77 1
                $count = $total - $this->key();
78 1
                $countPagesAdded = $countPagesUpdated = 0;
79
                /** @var AbstractGenerator $generator */
80 1
                $generator = $this->current();
81
                /** @var PagesCollection $generatedPages */
82 1
                $generatedPages = $generator->runGenerate();
83 1
                foreach ($generatedPages as $page) {
84
                    /** @var \Cecil\Collection\Page\Page $page */
85
                    try {
86 1
                        $pagesCollection->add($page);
87 1
                        $countPagesAdded++;
88 1
                    } catch (\DomainException) {
89 1
                        $pagesCollection->replace($page->getId(), $page);
90 1
                        $countPagesUpdated++;
91
                    }
92
                }
93 1
                $message = \sprintf('%s "%s" pages generated and %s pages updated', $countPagesAdded, Util::formatClassName($generator), $countPagesUpdated);
94 1
                $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
95
96 1
                $this->next();
97
            }
98
        }
99
100 1
        return $pagesCollection;
101
    }
102
}
103