TwigTemplateTask::bake()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 9.6
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 20
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of TwigView.
6
 *
7
 ** (c) 2014 Cees-Jan Kiewiet
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace WyriHaximus\TwigView\Shell\Task;
14
15
use Bake\Shell\Task\TemplateTask;
16
use Cake\Console\Shell;
17
use Cake\Utility\Inflector;
18
19
final class TwigTemplateTask extends TemplateTask
20
{
21
    /**
22
     * @return string Task name.
23
     */
24
    public function name(): string
25
    {
26
        return 'twig_template';
27
    }
28
29
    /**
30
     * Assembles and writes bakes the twig view file.
31
     *
32
     * @param  string $action     Action to bake.
33
     * @param  string $content    Content to write.
34
     * @param  string $outputFile The destination action name. If null, will fallback to $template.
35
     * @return string Generated file content.
36
     */
37
    public function bake($action, $content = '', $outputFile = null): string
38
    {
39
        if ($outputFile === null) {
40
            $outputFile = $action;
41
        }
42
        if ($content === true) {
43
            $content = $this->getContent($action);
44
        }
45
        if (empty($content)) {
46
            $this->err("<warning>No generated content for '{$action}.php', not generating template.</warning>");
47
48
            return false;
49
        }
50
        $this->out("\n" . sprintf('Baking `%s` view twig template file...', $outputFile), 1, Shell::QUIET);
51
        $path = $this->getPath();
52
        $filename = $path . Inflector::underscore($outputFile) . '.twig';
53
        $this->createFile($filename, $content);
54
55
        return $content;
56
    }
57
}
58