page.js ➔ page   F
last analyzed

Complexity

Conditions 17
Paths 60

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 17
c 1
b 0
f 1
nc 60
nop 2
dl 0
loc 78
rs 1.8

2 Functions

Rating   Name   Duplication   Size   Complexity  
A page.js ➔ ... ➔ cmsData.source.catch 0 3 1
A page.js ➔ ... ➔ ??? 0 5 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like page.js ➔ page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import path from 'path'
2
import {cmsData, config, Page, cmsTemplates, coreUtils} from '../../cli'
3
4
var page = function(req, res) {
5
  var html = req.query.html ? true : false
6
  var json = null
7
  var editor = false
8
  if (typeof req.body.json !== 'undefined' && req.body.json !== null) {
9
    editor = true
10
    if (typeof req.body.json === 'string') {
11
      json = JSON.parse(req.body.json)
12
    } else {
13
      json = req.body.json
14
    }
15
  }
16
17
  var filepath = req.originalUrl.replace('/abe/page', '')
18
19
  if (typeof filepath !== 'undefined' && filepath !== null) {
20
    var jsonPath = null
21
    var linkPath = null
0 ignored issues
show
Unused Code introduced by
The variable linkPath seems to be never used. Consider removing it.
Loading history...
22
23
    var filePathTest = cmsData.revision.getDocumentRevision(filepath)
24
    if (typeof filePathTest !== 'undefined' && filePathTest !== null) {
25
      jsonPath = filePathTest.path
26
      linkPath = filePathTest.abe_meta.link
27
    }
28
29
    if (jsonPath === null || !coreUtils.file.exist(jsonPath)) {
30
      res.status(404).send('Not found')
31
      return
32
    }
33
34
    if (typeof json === 'undefined' || json === null) {
35
      json = cmsData.file.get(jsonPath)
36
    }
37
38
    let meta = config.meta.name
39
40
    var templateId = ''
41
    if (json[meta] && json[meta].template) {
42
      templateId = json[meta].template
43
    } else {
44
      templateId = req.params[0]
45
    }
46
    var text = cmsTemplates.template.getTemplate(templateId, json)
47
48
    if (!editor) {
49
      cmsData.source
50
        .getDataList(text, json)
51
        .then(() => {
52
          var page = new Page(text, json, html)
53
          res.set('Content-Type', 'text/html')
54
          res.send(page.html)
55
        })
56
        .catch(function(e) {
57
          console.error(e)
58
        })
59
    } else {
60
      text = cmsData.source.removeDataList(text)
61
      var page = new Page(text, json, html)
62
      res.set('Content-Type', 'text/html')
63
      res.send(page.html)
64
    }
65
  } else {
66
    // not 404 page if tag abe image upload into each block
67
    if (/upload%20image/g.test(req.url)) {
68
      var b64str =
69
        'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
70
      var img = new Buffer(b64str, 'base64')
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
71
72
      res.writeHead(200, {
73
        'Content-Type': 'image/jpeg',
74
        'Content-Length': img.length
75
      })
76
      res.end(img)
77
    } else {
78
      res.status(404).send('Not found')
79
    }
80
  }
81
}
82
83
export default page
84