Passed
Push — master ( 3a3f23...fdc038 )
by litefeel
02:23
created

Writing_On_GitHub_Blob::front_matter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * API Blob model.
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Blob
9
 */
10
class Writing_On_GitHub_Blob {
11
12
    /**
13
     * Complete blob content.
14
     *
15
     * @var string
16
     */
17
    protected $content;
18
19
    /**
20
     * Blob sha.
21
     *
22
     * @var string
23
     */
24
    protected $sha;
25
26
    /**
27
     * Blob path.
28
     *
29
     * @var string
30
     */
31
    protected $path;
32
33
    /**
34
     * Post id.
35
     *
36
     * @var int
37
     */
38
    protected $id;
39
40
    /**
41
     * Whether the blob has frontmatter.
42
     *
43
     * @var boolean
44
     */
45
    protected $frontmatter = false;
46
47
    /**
48
     * The front matter of github post
49
     * @var string
50
     */
51
    protected $front_matter = '';
52
53
    /**
54
     * Instantiates a new Blob object.
55
     *
56
     * @param stdClass $data Raw blob data.
57
     */
58
    public function __construct( stdClass $data ) {
59
        $this->interpret_data( $data );
60
    }
61
62
    public function id() {
63
        return $this->id;
64
    }
65
66
    public function set_id($id) {
67
        $this->id = $id;
68
    }
69
70
    /**
71
     * Returns the raw blob content.
72
     *
73
     * @return string
74
     */
75
    public function content() {
76
        return $this->content;
77
    }
78
79
    /**
80
     * Set's the blob's content.
81
     *
82
     * @param string $content Raw blob content.
83
     * @param bool   $base64 Whether the content is base64 encoded.
84
     *
85
     * @return $this
86
     */
87
    public function set_content( $content, $base64 = false ) {
88
        if ( $base64 ) {
89
            $content = base64_decode( $content );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $content. This often makes code more readable.
Loading history...
90
        }
91
92
        $this->frontmatter = '---' === substr( $this->content = $content, 0, 3 );
93
94
        return $this;
95
    }
96
    /**
97
     * Returns the blob sha.
98
     *
99
     * @return string
100
     */
101
    public function sha() {
102
        return $this->sha;
103
    }
104
105
    /**
106
     * Return's the blob path.
107
     *
108
     * @return string
109
     */
110
    public function path() {
111
        return $this->path;
112
    }
113
114
    /**
115
     * Whether the blob has frontmatter.
116
     *
117
     * @return bool
118
     */
119
    public function has_frontmatter() {
120
        return $this->frontmatter;
121
    }
122
123
    /**
124
     * The front matter of github post
125
     * @return string
126
     */
127
    public function front_matter() {
128
        return $this->front_matter;
129
    }
130
131
    /**
132
     * Returns the formatted/filtered blob content used for import.
133
     *
134
     * @return string
135
     */
136
    public function content_import() {
137
        $content = $this->content();
138
139
        if ( $this->has_frontmatter() ) {
140
            // Break out content.
141
            preg_match( '/(^---(.*?)---$)?(.*)/ms', $content, $matches );
142
            $this->front_matter = $matches[1];
143
            $content = array_pop( $matches );
144
        }
145
146
        if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) {
147
            $content = wpmarkdown_markdown_to_html( $content );
148
        }
149
150
        /**
151
         * Filters the content for import.
152
         */
153
        return apply_filters( 'wogh_content_import', trim( $content ) );
154
    }
155
156
    /**
157
     * Returns the blob meta.
158
     *
159
     * @return array
160
     */
161
    public function meta() {
162
        $meta = array();
163
164
        if ( $this->has_frontmatter() ) {
165
            // Break out meta, if present.
166
            preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches );
167
            array_pop( $matches );
168
169
            $meta = spyc_load( $matches[2] );
170
            if ( 'yes' == get_option('wogh_ignore_author') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
171
                unset($meta['author']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
172
            }
173
            // if ( isset( $meta['link'] ) ) {
174
            //  $meta['link'] = str_replace( home_url(), '', $meta['link'] );
175
            // }
176
        }
177
178
        return $meta;
179
    }
180
181
    /**
182
     * Formats the blob into an API call body.
183
     *
184
     * @return stdClass
185
     */
186
    // public function to_body() {
187
    //  $data = new stdClass;
188
189
    //  $data->mode = '100644';
190
    //  $data->type = 'blob';
191
192
    //  $data->path = $this->path();
193
194
    //  if ( $this->sha() ) {
195
    //      $data->sha = $this->sha();
196
    //  } else {
197
    //      $data->content = $this->content();
198
    //  }
199
200
    //  return $data;
201
    // }
202
203
204
    /**
205
     * Formats the blob into an API call body.
206
     *
207
     * @return stdClass
208
     */
209
    public function to_body() {
210
        $data = new stdClass;
211
212
        // $data->mode = '100644';
213
        // $data->type = 'blob';
214
215
        $data->path = $this->path();
216
        $data->content = base64_encode( $this->content() );
217
        $data->sha = $this->sha;
218
219
        return $data;
220
    }
221
222
    /**
223
     * Interprets the blob's data into properties.
224
     */
225
    protected function interpret_data( $data ) {
0 ignored issues
show
Complexity introduced by
This operation has 625 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

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

Loading history...
226
        $this->sha  = isset( $data->sha  ) ? $data->sha  : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
227
        $this->path = isset( $data->path ) ? $data->path : '';
228
229
        $this->set_content(
230
            isset( $data->content ) ? trim( $data->content ) : '',
231
            isset( $data->encoding ) && 'base64' === $data->encoding ? true : false
232
        );
233
    }
234
}
235