Completed
Push — master ( a41cf5...7efa4f )
by Vladimir
02:55
created

DataItem::readContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\DataTransformer\DataTransformer;
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 array */
21
    protected $data;
22
23
    /** @var DataTransformer */
24
    protected $dataTransformer;
25
26
    /**
27
     * DataItem constructor.
28
     */
29 8
    public function __construct(File $file)
30
    {
31 8
        $this->noReadOnConstructor = true;
32
33 8
        parent::__construct($file);
34 7
    }
35
36
    /**
37
     * Set the transformer used to convert the file contents into an array,
38
     */
39 7
    public function setDataTransformer(DataTransformerManager $manager)
40
    {
41 7
        $this->dataTransformer = $manager->getTransformer($this->getExtension());
42 6
        $this->readContent();
43 6
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function evaluateFrontMatter(array $variables = [], array $complexVariables = [])
49
    {
50
        $workspace = array_merge($this->data, $variables);
51
        $parser = new FrontMatterParser($workspace, array(
52
            'filename' => $this->getFileName(),
53
            'basename' => $this->getBaseName(),
54
        ));
55
        $parser->addComplexVariables($complexVariables);
56
        $parser->parse();
57
58
        if (!is_null($parser) && $parser->hasExpansion())
59
        {
60
            throw new \LogicException('The permalink for this item has not been set.');
61
        }
62
63
        $permalink = $workspace['permalink'];
64
65 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...
66
        {
67
            $this->permalink = $permalink[0];
68
            array_shift($permalink);
69
            $this->redirects = $permalink;
70
        }
71
        else
72
        {
73
            $this->permalink = $permalink;
74
            $this->redirects = array();
75
        }
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function buildPermalink($force = false)
82
    {
83
        return;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 6
    public function readContents($mixed)
90
    {
91 6
        $content = $this->file->getContents();
92 6
        $this->data = $this->dataTransformer->transformData($content);
93 6
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public function getContent()
99
    {
100 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...
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function isDraft()
107
    {
108 2
        return false;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 4 View Code Duplication
    public function createJail()
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...
115
    {
116 4
        $whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [
117 4
        ]);
118
119
        $jailedFunctions = [
120 4
            'getPageView' => 'getJailedPageView',
121
            'getDataset'  => 'getNamespace',
122
        ];
123
124 4
        return (new JailedDocument($this, $whiteListedFunctions, $jailedFunctions));
125
    }
126
127
    ///
128
    // JsonSerializable implementation
129
    ///
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function jsonSerialize()
135
    {
136
        return $this->data;
137
    }
138
139
    ///
140
    // IteratorAggregate implementation
141
    ///
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function getIterator()
147
    {
148 1
        return new \ArrayIterator($this->data);
149
    }
150
151
    ///
152
    // ArrayAccess implementation
153
    ///
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function offsetExists($offset)
159
    {
160
        return isset($this->data[$offset]);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 2
    public function offsetGet($offset)
167
    {
168 2
        return $this->data[$offset];
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function offsetSet($offset, $value)
175
    {
176
        throw new \LogicException('DataItems are read-only.');
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function offsetUnset($offset)
183
    {
184
        throw new \LogicException('DataItems are read-only.');
185
    }
186
}
187