Passed
Push — ci ( 9342ca...3ea26a )
by litefeel
02:42
created

Writing_On_GitHub_Blob::set_content()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 9.6666
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
     * Content without front matter
55
     * @var string
56
     */
57
    protected $post_content;
58
59
    /**
60
     * Instantiates a new Blob object.
61
     *
62
     * @param stdClass $data Raw blob data.
63
     */
64
    public function __construct( stdClass $data ) {
65
        $this->interpret_data( $data );
66
    }
67
68
    public function id() {
69
        return $this->id;
70
    }
71
72
    public function set_id($id) {
73
        $this->id = $id;
74
    }
75
76
    /**
77
     * Returns the raw blob content.
78
     *
79
     * @return string
80
     */
81
    public function content() {
82
        return $this->content;
83
    }
84
85
    /**
86
     * Set's the blob's content.
87
     *
88
     * @param string $content Raw blob content.
89
     * @param bool   $base64 Whether the content is base64 encoded.
90
     *
91
     * @return $this
92
     */
93
    public function set_content( $content, $base64 = false ) {
94
        if ( $base64 ) {
95
            $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...
96
        }
97
98
        $this->frontmatter = '---' === substr( $this->content = $content, 0, 3 );
99
100
        return $this;
101
    }
102
    /**
103
     * Returns the blob sha.
104
     *
105
     * @return string
106
     */
107
    public function sha() {
108
        return $this->sha;
109
    }
110
111
    /**
112
     * Return's the blob path.
113
     *
114
     * @return string
115
     */
116
    public function path() {
117
        return $this->path;
118
    }
119
120
    /**
121
     * Whether the blob has frontmatter.
122
     *
123
     * @return bool
124
     */
125
    public function has_frontmatter() {
126
        return $this->frontmatter;
127
    }
128
129
    /**
130
     * The front matter of github post
131
     * @return string
132
     */
133
    public function front_matter() {
134
        return $this->front_matter;
135
    }
136
137
    /**
138
     * Content without front matter
139
     * @return string
140
     */
141
    public function post_content() {
142
        if ( ! $this->post_content ) {
143
            $this->content_import();
144
        }
145
        return $this->post_content;
146
    }
147
148
    /**
149
     * Returns the formatted/filtered blob content used for import.
150
     *
151
     * @return string
152
     */
153
    public function content_import() {
154
        $this->post_content = $content = $this->content();
155
156
        if ( $this->has_frontmatter() ) {
157
            // Break out content.
158
            preg_match( '/(^---(.*?)---$(\r\n|\n|\r)?)?(.*)/ms', $content, $matches );
159
            $this->front_matter = $matches[1];
160
            $this->post_content = $content = array_pop( $matches );
161
        }
162
163
        if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) {
164
            $content = wpmarkdown_markdown_to_html( $content );
165
        }
166
167
        /**
168
         * Filters the content for import.
169
         */
170
        return apply_filters( 'wogh_content_import', trim( $content ) );
171
    }
172
173
    /**
174
     * Returns the blob meta.
175
     *
176
     * @return array
177
     */
178
    public function meta() {
179
        $meta = array();
180
181
        if ( $this->has_frontmatter() ) {
182
            // Break out meta, if present.
183
            preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches );
184
            array_pop( $matches );
185
186
            $meta = spyc_load( $matches[2] );
187
            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...
188
                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...
189
            }
190
            // if ( isset( $meta['link'] ) ) {
191
            //  $meta['link'] = str_replace( home_url(), '', $meta['link'] );
192
            // }
193
        }
194
195
        return $meta;
196
    }
197
198
    /**
199
     * Formats the blob into an API call body.
200
     *
201
     * @return stdClass
202
     */
203
    // public function to_body() {
204
    //  $data = new stdClass;
205
206
    //  $data->mode = '100644';
207
    //  $data->type = 'blob';
208
209
    //  $data->path = $this->path();
210
211
    //  if ( $this->sha() ) {
212
    //      $data->sha = $this->sha();
213
    //  } else {
214
    //      $data->content = $this->content();
215
    //  }
216
217
    //  return $data;
218
    // }
219
220
221
    /**
222
     * Formats the blob into an API call body.
223
     *
224
     * @return stdClass
225
     */
226
    public function to_body() {
227
        $data = new stdClass;
228
229
        // $data->mode = '100644';
230
        // $data->type = 'blob';
231
232
        $data->path = $this->path();
233
        $data->content = base64_encode( $this->content() );
234
        $data->sha = $this->sha;
235
236
        return $data;
237
    }
238
239
    /**
240
     * Interprets the blob's data into properties.
241
     */
242
    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...
243
        $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...
244
        $this->path = isset( $data->path ) ? $data->path : '';
245
246
        $this->set_content(
247
            isset( $data->content ) ? trim( $data->content ) : '',
248
            isset( $data->encoding ) && 'base64' === $data->encoding ? true : false
249
        );
250
    }
251
}
252