PostWriter::write()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 20

Duplication

Lines 6
Ratio 22.22 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 6
loc 27
rs 8.8571
cc 2
eloc 20
nc 2
nop 1
1
<?php
2
3
namespace Fillet\Writer;
4
5
use Symfony\Component\Yaml\Dumper as YamlDumper;
6
7
/**
8
 * Generates a Post for Sculpin
9
 *
10
 * @package Fillet\Writer
11
 */
12
class PostWriter extends AbstractWriter
13
{
14
	/**
15
	 * Write out a set of data into a file
16
	 *
17
	 * @param array $data Data to use for constructing the page
18
	 */
19
    public function write($data)
20
    {
21
        $post_date_string = $data['post_date']->format('Y-m-d H:i:s');
22
        $slug = $this->generateSlug($data['title']);
23
        $filename = $data['post_date']->format('Y-m-d') . '-' . $slug;
24
25
		$headerData = [
26
			'title' => $data['title'],
27
			'date' => $post_date_string,
28
			'layout' => 'post',
29
			'slug' => $slug,
30
			'categories' => $data['categories'],
31
			'tags' => $data['tags'],
32
		];
33
34
		$dumper = new YamlDumper();
35
		$header = '---' . PHP_EOL . $dumper->dump($headerData, 2) . '---' . PHP_EOL;
36
37
        $filename = $this->destinationFolder . $filename;
38 View Code Duplication
        if ($this->isMarkdownEnabled()) {
39
            $filename .= '.md';
40
            $data['content'] = $this->toMarkdown($data['content']);
41
        } else {
42
            $filename .= '.html';
43
        }
44
        file_put_contents($filename, $header . PHP_EOL . $data['content']);
45
    }
46
}