PageFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 93
ccs 29
cts 29
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 22 4
A parse() 0 7 1
A __construct() 0 3 2
A file() 0 9 1
A body() 0 5 1
1
<?php
2
3
namespace Staticka\Factories;
4
5
use Staticka\Contracts\LayoutContract;
6
use Staticka\Contracts\PageContract;
7
use Staticka\Layout;
8
use Staticka\Matter;
9
use Staticka\Page;
10
use Symfony\Component\Yaml\Yaml;
11
12
/**
13
 * Page Factory
14
 *
15
 * @package Staticka
16
 * @author  Rougin Gutib <[email protected]>
17
 */
18
class PageFactory
19
{
20
    /**
21
     * @var \Staticka\Contracts\LayoutContract
22
     */
23
    protected $layout;
24
25
    /**
26
     * @param \Staticka\Contracts\LayoutContract|null $layout
27
     */
28 21
    public function __construct(LayoutContract $layout = null)
29
    {
30 21
        $this->layout = $layout ? $layout : new Layout;
31 21
    }
32
33
    /**
34
     * TODO: To be removed on v1.0.0.
35
     * Should only be file-based.
36
     *
37
     * Creates a new page instance based on the content.
38
     *
39
     * @param  string $body
40
     * @param  array  $data
41
     * @return \Staticka\Contracts\PageContract
42
     */
43 21
    public function body($body, $data = array())
44
    {
45 21
        $data[PageContract::DATA_BODY] = $body;
46
47 21
        return $this->make((array) $data);
48
    }
49
50
    /**
51
     * Creates a new page instance based on the file.
52
     *
53
     * @param  string $file
54
     * @param  array  $data
55
     * @return \Staticka\Contracts\PageContract
56
     */
57 9
    public function file($file, $data = array())
58
    {
59 9
        $result = file_get_contents($file);
60
61 9
        $matter = $this->parse($result);
62
63 9
        $data = array_merge($data, $matter);
64
65 9
        return $this->make((array) $data);
66
    }
67
68
    /**
69
     * Returns a new page instance.
70
     *
71
     * @param  array $data
72
     * @return \Staticka\Contracts\PageContract
73
     */
74 21
    protected function make($data)
75
    {
76 21
        if (! isset($data[PageContract::DATA_LINK]))
77 7
        {
78 21
            $data[PageContract::DATA_LINK] = 'index';
79 7
        }
80
81
        // TODO: Remove this on v1.0.0.
82
        // Use DATA_PLATE instead of "layout".
83 21
        if (isset($data['layout']))
84 7
        {
85 3
            $data[PageContract::DATA_PLATE] = $data['layout'];
86 1
        }
87
88
        // TODO: Remove this on v1.0.0.
89
        // Use DATA_LINK instead of "permalink".
90 21
        if (isset($data['permalink']))
91 7
        {
92 3
            $data[PageContract::DATA_LINK] = $data['permalink'];
93 1
        }
94
95 21
        return new Page($this->layout, $data);
96
    }
97
98
    /**
99
     * Converts the Matter format into data.
100
     *
101
     * @param  string $content
102
     * @return array
103
     */
104 9
    protected function parse($content)
105
    {
106 9
        list($matter, $body) = Matter::parse($content);
107
108 9
        $matter[PageContract::DATA_BODY] = trim($body);
109
110 9
        return (array) $matter;
111
    }
112
}
113