Passed
Push — logger ( fa6ca8...aacfbb )
by Arnaud
05:39 queued 02:50
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
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Generator;
12
13
use Cecil\Builder;
14
use Cecil\Collection\Page\Collection as PagesCollection;
15
use Cecil\Util;
16
17
class GeneratorManager extends \SplPriorityQueue
18
{
19
    /** @var Builder */
20
    protected $builder;
21
22
    /**
23
     * @param Builder $builder
24
     *
25
     * @return void
26
     */
27
    public function __construct(Builder $builder)
28
    {
29
        $this->builder = $builder;
30
    }
31
32
    /**
33
     * Adds a generator.
34
     *
35
     * @param GeneratorInterface $generator
36
     * @param int                $priority
37
     *
38
     * @return self
39
     */
40
    public function addGenerator(GeneratorInterface $generator, int $priority = 1): self
41
    {
42
        $this->insert($generator, $priority);
43
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function compare($priority1, $priority2)
51
    {
52
        if ($priority1 === $priority2) {
53
            return 0;
54
        }
55
56
        return $priority1 > $priority2 ? -1 : 1;
57
    }
58
59
    /**
60
     * Process each generators.
61
     *
62
     * @return PagesCollection
63
     */
64
    public function process(): PagesCollection
65
    {
66
        /** @var PagesCollection $pagesCollection */
67
        $pagesCollection = $this->builder->getPages();
68
        $max = $this->count();
69
70
        if ($max > 0) {
71
            $this->top();
72
            while ($this->valid()) {
73
                $count = $max - $this->key();
74
                /** @var AbstractGenerator $generator */
75
                $generator = $this->current();
76
                /** @var PagesCollection $generatedPages */
77
                $generatedPages = $generator->runGenerate();
78
                foreach ($generatedPages as $page) {
79
                    /** @var \Cecil\Collection\Page\Page $page */
80
                    if ($pagesCollection->has($page->getId())) {
81
                        $pagesCollection->replace($page->getId(), $page);
82
                    } else {
83
                        $pagesCollection->add($page);
84
                    }
85
                }
86
                $message = sprintf('%s: %s', Util::formatClassName($generator), count($generatedPages));
87
                $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
88
89
                $this->next();
90
            }
91
        }
92
93
        return $pagesCollection;
94
    }
95
}
96