Passed
Push — master ( 410349...4e2bae )
by litefeel
02:24
created

Writing_On_GitHub_Blob   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 246
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0
wmc 25
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A id() 0 3 1
A set_id() 0 3 1
A content() 0 3 1
A set_content() 0 13 2
A sha() 0 3 1
A path() 0 3 1
A has_frontmatter() 0 3 1
A front_matter() 0 3 1
A post_content() 0 6 2
A content_import() 0 19 3
A meta() 0 19 3
A to_body() 0 12 1
B interpret_data() 0 9 6
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
        // remove whitespace from the beginning of content,
99
        // To prevent blank lines before yml
100
        $this->content = ltrim( $content );
101
102
        $this->frontmatter = '---' === substr( $this->content, 0, 3 );
103
104
        return $this;
105
    }
106
    /**
107
     * Returns the blob sha.
108
     *
109
     * @return string
110
     */
111
    public function sha() {
112
        return $this->sha;
113
    }
114
115
    /**
116
     * Return's the blob path.
117
     *
118
     * @return string
119
     */
120
    public function path() {
121
        return $this->path;
122
    }
123
124
    /**
125
     * Whether the blob has frontmatter.
126
     *
127
     * @return bool
128
     */
129
    public function has_frontmatter() {
130
        return $this->frontmatter;
131
    }
132
133
    /**
134
     * The front matter of github post
135
     * @return string
136
     */
137
    public function front_matter() {
138
        return $this->front_matter;
139
    }
140
141
    /**
142
     * Content without front matter
143
     * @return string
144
     */
145
    public function post_content() {
146
        if ( ! $this->post_content ) {
147
            $this->content_import();
148
        }
149
        return $this->post_content;
150
    }
151
152
    /**
153
     * Returns the formatted/filtered blob content used for import.
154
     *
155
     * @return string
156
     */
157
    public function content_import() {
158
        $this->post_content = $content = $this->content();
159
160
        if ( $this->has_frontmatter() ) {
161
            // Break out content.
162
            preg_match( '/(^---(.*?)---$(\r\n|\n|\r)?)?(.*)/ms', $content, $matches );
163
            $this->front_matter = $matches[1];
164
            $this->post_content = $content = array_pop( $matches );
165
        }
166
167
        if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) {
168
            $content = wpmarkdown_markdown_to_html( $content );
169
        }
170
171
        /**
172
         * Filters the content for import.
173
         */
174
        return apply_filters( 'wogh_content_import', trim( $content ) );
175
    }
176
177
    /**
178
     * Returns the blob meta.
179
     *
180
     * @return array
181
     */
182
    public function meta() {
183
        $meta = array();
184
185
        if ( $this->has_frontmatter() ) {
186
            // Break out meta, if present.
187
            preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches );
188
            array_pop( $matches );
189
190
            $meta = spyc_load( $matches[2] );
191
            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...
192
                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...
193
            }
194
            // if ( isset( $meta['link'] ) ) {
195
            //  $meta['link'] = str_replace( home_url(), '', $meta['link'] );
196
            // }
197
        }
198
199
        return $meta;
200
    }
201
202
    /**
203
     * Formats the blob into an API call body.
204
     *
205
     * @return stdClass
206
     */
207
    // public function to_body() {
208
    //  $data = new stdClass;
209
210
    //  $data->mode = '100644';
211
    //  $data->type = 'blob';
212
213
    //  $data->path = $this->path();
214
215
    //  if ( $this->sha() ) {
216
    //      $data->sha = $this->sha();
217
    //  } else {
218
    //      $data->content = $this->content();
219
    //  }
220
221
    //  return $data;
222
    // }
223
224
225
    /**
226
     * Formats the blob into an API call body.
227
     *
228
     * @return stdClass
229
     */
230
    public function to_body() {
231
        $data = new stdClass;
232
233
        // $data->mode = '100644';
234
        // $data->type = 'blob';
235
236
        $data->path = $this->path();
237
        $data->content = base64_encode( $this->content() );
238
        $data->sha = $this->sha;
239
240
        return $data;
241
    }
242
243
    /**
244
     * Interprets the blob's data into properties.
245
     */
246
    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...
247
        $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...
248
        $this->path = isset( $data->path ) ? $data->path : '';
249
250
        $this->set_content(
251
            isset( $data->content ) ? $data->content : '',
252
            isset( $data->encoding ) && 'base64' === $data->encoding ? true : false
253
        );
254
    }
255
}
256