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

AbstractGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 37
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A runGenerate() 0 12 2
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