Passed
Pull Request — master (#2285)
by Arnaud
10:02 queued 03:47
created

AbstractGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
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\Config;
18
use Cecil\Collection\Page\Collection as PagesCollection;
19
use Cecil\Util;
20
use Psr\Log\LoggerInterface;
21
22
/**
23
 * Generator abstract class.
24
 */
25
abstract class AbstractGenerator implements GeneratorInterface
26
{
27
    /** @var Builder */
28
    protected $builder;
29
30
    /** @var Config */
31
    protected $config;
32
33
    /** @var LoggerInterface */
34
    protected $logger;
35
36
    /** @var PagesCollection */
37
    protected $generatedPages;
38
39
    /**
40
     * Flexible constructor supporting dependency injection or legacy mode.
41
     */
42 1
    public function __construct(Builder $builder, ?Config $config = null, ?LoggerInterface $logger = null)
43
    {
44 1
        $this->builder = $builder;
45 1
        $this->config = $config ?? $builder->getConfig();
46 1
        $this->logger = $logger ?? $builder->getLogger();
47
        // Creates a new empty collection
48 1
        $this->generatedPages = new PagesCollection('generator-' . Util::formatClassName($this, ['lowercase' => true]));
49
    }
50
51
    /**
52
     * Run the `generate` method of the generator and returns pages.
53
     */
54 1
    public function runGenerate(): PagesCollection
55
    {
56 1
        $this->generate();
57
58
        // set default language (e.g.: "en") if necessary
59 1
        $this->generatedPages->map(function (\Cecil\Collection\Page\Page $page) {
60 1
            if ($page->getVariable('language') === null) {
61
                $page->setVariable('language', $this->config->getLanguageDefault());
62
            }
63 1
        });
64
65 1
        return $this->generatedPages;
66
    }
67
}
68