Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

GeneratorManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addGenerator() 0 6 1
A compare() 0 8 3
A process() 0 28 5
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Collection as PageCollection;
12
13
class GeneratorManager extends \SplPriorityQueue
14
{
15
    /**
16
     * Adds a generator.
17
     *
18
     * @param GeneratorInterface $generator
19
     * @param int                $priority
20
     *
21
     * @return self
22
     */
23
    public function addGenerator(GeneratorInterface $generator, $priority = 1)
24
    {
25
        $this->insert($generator, $priority);
26
27
        return $this;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function compare($priority1, $priority2)
34
    {
35
        if ($priority1 === $priority2) {
36
            return 0;
37
        }
38
39
        return $priority1 > $priority2 ? -1 : 1;
40
    }
41
42
    /**
43
     * Process each generators.
44
     *
45
     * @param PageCollection $pageCollection
46
     * @param \Closure       $messageCallback
47
     *
48
     * @return PageCollection
49
     */
50
    public function process(PageCollection $pageCollection, \Closure $messageCallback)
51
    {
52
        $max = $this->count();
53
54
        if ($max > 0) {
55
            $this->top();
56
            while ($this->valid()) {
57
                /* @var GeneratorInterface $generator */
58
                $generator = $this->current();
59
                /* @var $generatedPages PageCollection */
60
                $generatedPages = $generator->generate($pageCollection, $messageCallback);
61
                foreach ($generatedPages as $page) {
62
                    /* @var $page \Cecil\Collection\Page\Page */
63
                    if ($pageCollection->has($page->getId())) {
64
                        $pageCollection->replace($page->getId(), $page);
65
                    } else {
66
                        $pageCollection->add($page);
67
                    }
68
                }
69
                $message = substr(strrchr(get_class($generator), '\\'), 1).': '.count($generatedPages);
70
                $count = ($max - $this->key());
71
                call_user_func_array($messageCallback, ['GENERATE_PROGRESS', $message, $count, $max]);
72
                $this->next();
73
            }
74
        }
75
76
        return $pageCollection;
77
    }
78
}
79