Completed
Push — master ( 6ddfd6...31b686 )
by Vladimir
02:08
created

DataItem::isDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\DataTransformer\DataTransformerInterface;
11
use allejo\stakx\DataTransformer\DataTransformerManager;
12
use allejo\stakx\Filesystem\File;
13
use allejo\stakx\FrontMatter\FrontMatterParser;
14
15
class DataItem extends ReadableDocument implements CollectableItem, TemplateReadyDocument, PermalinkDocument
16
{
17
    use CollectableItemTrait;
18
    use PermalinkDocumentTrait;
19
20
    /** @var DataTransformerInterface */
21
    protected $dataTransformer;
22
23
    /** @var array */
24
    protected $frontMatter;
25
26
    /** @var array */
27
    protected $data;
28
29
    /**
30
     * DataItem constructor.
31
     */
32 7
    public function __construct(File $file)
33
    {
34 7
        $this->noReadOnConstructor = true;
35
36 7
        parent::__construct($file);
37 7
    }
38
39
    /**
40
     * Set the transformer used to convert the file contents into an array,.
41
     */
42 7
    public function setDataTransformer(DataTransformerManager $manager)
43
    {
44 7
        $this->dataTransformer = $manager->getTransformer($this->getExtension());
45 6
        $this->readContent();
46 6
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function evaluateFrontMatter(array $variables = [], array $complexVariables = [])
52
    {
53
        $this->frontMatter = array_merge($this->data, $variables);
54
        $parser = new FrontMatterParser($this->frontMatter, [
55
            'filename' => $this->getFileName(),
56
            'basename' => $this->getBaseName(),
57
        ]);
58
        $parser->addComplexVariables($complexVariables);
59
        $parser->parse();
60
61
        if (!is_null($parser) && $parser->hasExpansion())
62
        {
63
            throw new \LogicException('The permalink for this item has not been set.');
64
        }
65
66
        $permalink = $this->frontMatter['permalink'];
67
68 View Code Duplication
        if (is_array($permalink))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
        {
70
            $this->permalink = $permalink[0];
71
            array_shift($permalink);
72
            $this->redirects = $permalink;
73
        }
74
        else
75
        {
76
            $this->permalink = $permalink;
77
            $this->redirects = [];
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function buildPermalink($force = false)
85
    {
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 6
    public function readContents($mixed)
92
    {
93 6
        $content = $this->file->getContents();
94 6
        $this->data = $this->dataTransformer->transformData($content);
95 6
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 4
    public function getContent()
101
    {
102 4
        return $this->data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->data; (array) is incompatible with the return type of the parent method allejo\stakx\Document\ReadableDocument::getContent of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function isDraft()
109
    {
110 2
        return false;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 4
    public function createJail()
117
    {
118 4
        $whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [
119 4
        ]);
120
121
        $jailedFunctions = [
122 4
            'getDataset' => 'getNamespace',
123
        ];
124
125 4
        return new JailedDocument($this, $whiteListedFunctions, $jailedFunctions);
126
    }
127
128
    ///
129
    // JsonSerializable implementation
130
    ///
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function jsonSerialize()
136
    {
137
        return $this->data;
138
    }
139
140
    ///
141
    // IteratorAggregate implementation
142
    ///
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function getIterator()
148
    {
149 1
        return new \ArrayIterator($this->data);
150
    }
151
152
    ///
153
    // ArrayAccess implementation
154
    ///
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function offsetExists($offset)
160
    {
161
        return isset($this->data[$offset]) || isset($this->frontMatter[$offset]);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 2 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169 2
        $fxnCall = 'get' . ucfirst($offset);
170
171 2
        if (in_array($fxnCall, FrontMatterDocument::$whiteListedFunctions) && method_exists($this, $fxnCall))
172
        {
173
            return call_user_func_array([$this, $fxnCall], []);
174
        }
175
176 2
        if (isset($this->data[$offset]))
177
        {
178 2
            return $this->data[$offset];
179
        }
180
181 1
        if (isset($this->frontMatter[$offset]))
182
        {
183
            return $this->frontMatter[$offset];
184
        }
185
186 1
        return null;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function offsetSet($offset, $value)
193
    {
194
        throw new \LogicException('DataItems are read-only.');
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function offsetUnset($offset)
201
    {
202
        throw new \LogicException('DataItems are read-only.');
203
    }
204
}
205