Passed
Pull Request — master (#1676)
by Arnaud
05:15 queued 14s
created

AbstractGenerator::runGenerate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
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
 * Abstract class AbstractGenerator.
22
 */
23
abstract class AbstractGenerator implements GeneratorInterface
24
{
25
    /** @var Builder */
26
    protected $builder;
27
28
    /** @var \Cecil\Config */
29
    protected $config;
30
31
    /** @var PagesCollection */
32
    protected $generatedPages;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function __construct(Builder $builder)
38
    {
39 1
        $this->builder = $builder;
40 1
        $this->config = $builder->getConfig();
41
        // Creates a new empty collection
42 1
        $this->generatedPages = new PagesCollection('generator-' . Util::formatClassName($this, ['lowercase' => true]));
43
    }
44
45
    /**
46
     * Run the `generate` method of the generator and returns pages.
47
     */
48 1
    public function runGenerate(): PagesCollection
49
    {
50 1
        $this->generate();
51
52
        // set default language (e.g.: "en") if necessary
53 1
        $this->generatedPages->map(function (\Cecil\Collection\Page\Page $page) {
54 1
            if ($page->getVariable('language') === null) {
55 1
                $page->setVariable('language', $this->config->getLanguageDefault());
56
            }
57 1
        });
58
59 1
        return $this->generatedPages;
60
    }
61
}
62