BaseBlock   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isPublishedIcon() 0 11 1
B forTemplate() 0 19 5
A Link() 0 7 2
A getUploadDirectory() 0 3 1
A getBlockName() 0 3 1
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     BaseBlock
7
 *
8
 * @property string  Title
9
 * @property string  Content
10
 * @property string  BlockArea
11
 * @property string  CanViewType
12
 * @property string  ExtraCSSClasses
13
 * @property int     Weight
14
 * @property string  Area
15
 * @property boolean Published
16
 */
17
class BaseBlock extends \ContentBlock {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
19
    /**
20
     * Default directory to upload your files.
21
     *
22
     * @var string
23
     * @config
24
     */
25
    protected static $upload_directory = 'Uploads/ContentBlocks';
26
27
    /**
28
     * Include default styles (css) at frontend side?
29
     *
30
     * @var bool
31
     */
32
    private static $default_styles = true;
0 ignored issues
show
Unused Code introduced by
The property $default_styles is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
33
34
    /**
35
     * Re-add (un)publish images.
36
     *
37
     * @return HTMLText
38
     */
39
    public function isPublishedIcon() {
40
        $content = HTMLText::create();
41
        $images = [
42
            0 => sprintf('%s/assets/images/cancel.png', CONTENT_BLOCKS_DIR),
43
            1 => sprintf('%s/assets/images/success.png', CONTENT_BLOCKS_DIR),
44
        ];
45
46
        $content->setValue(sprintf('<img src="%s" />', $images[(int) $this->isPublished()]));
47
48
        return $content;
49
    }
50
51
    /**
52
     * Renders this block with appropriate templates
53
     * looks for templates that match BlockClassName_AreaName
54
     * falls back to BlockClassName.
55
     *
56
     * Updates: if function will find the template at blocks/ directory,
57
     * this will also render it.
58
     *
59
     * @return string
60
     **/
61
    public function forTemplate() {
62
        if (static::config()->default_styles) {
63
            \Requirements::css(sprintf('%s/assets/styles/app.css', CONTENT_BLOCKS_DIR));
64
        }
65
66
        if ($this->BlockArea) {
67
            $template = [$this->class.'_'.$this->BlockArea];
68
69
            if (\SSViewer::hasTemplate($template)) {
70
                return $this->renderWith($template);
71
            }
72
        }
73
74
        if (\SSViewer::hasTemplate($path = "blocks/{$this->ClassName}")) {
75
            return $this->renderWith($path, $this->getController());
76
        }
77
78
        return $this->renderWith($this->ClassName, $this->getController());
79
    }
80
81
    /**
82
     * @return bool|string
83
     */
84
    public function Link() {
85
        if ($page = $this->getCurrentPage()) {
86
            return $page->Link();
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getUploadDirectory() {
96
        return static::config()->upload_directory;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getBlockName() {
103
        return strtolower(str_replace(' ', '-', \FormField::name_to_label($this->ClassName)));
104
    }
105
}