Passed
Push — master ( eb9c5e...f73080 )
by Jason
02:30
created

ElementTestimonials::getSummary()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 0
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
ccs 11
cts 12
cp 0.9167
crap 4.0092
1
<?php
2
3
namespace Dynamic\Elements\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Model\Testimonial;
7
use Dynamic\Elements\Model\TestimonialCategory;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
10
use SilverStripe\Forms\ListboxField;
11
use SilverStripe\ORM\DB;
12
use SilverStripe\ORM\FieldType\DBField;
13
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
14
15
/**
16
 * Class ElementTestimonials
17
 * @package Dynamic\Elements\Elements
18
 *
19
 * @property int $Limit
20
 * @property string $Content
21
 */
22
class ElementTestimonials extends BaseElement
23
{
24
    /**
25
     * @var string
26
     */
27
    private static $icon = 'font-icon-chat';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
28
29
    /**
30
     * @var string
31
     */
32
    private static $singular_name = 'Testimonials Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var string
36
     */
37
    private static $plural_name = 'Testimonials Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @var string
41
     */
42
    private static $table_name = 'ElementTestimonials';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
43
44
    /**
45
     * @var array
46
     */
47
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
48
        'Limit' => 'Int',
49
        'Content' => 'HTMLText',
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
56
        'TestimonialCategories' => TestimonialCategory::class,
57
    ];
58
59
    /**
60
     * @var array
61
     */
62
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
63
        'Limit' => 3,
64
    ];
65
66
    /**
67
     * @return DBHTMLText
0 ignored issues
show
Bug introduced by
The type Dynamic\Elements\Elements\DBHTMLText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
     */
69 1
    public function getSummary()
70
    {
71 1
        if ($this->TestimonialCategories()->count() > 0) {
0 ignored issues
show
Bug introduced by
The method TestimonialCategories() does not exist on Dynamic\Elements\Elements\ElementTestimonials. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        if ($this->/** @scrutinizer ignore-call */ TestimonialCategories()->count() > 0) {
Loading history...
72 1
            $ct = 0;
73 1
            foreach ($this->TestimonialCategories() as $category) {
74 1
                $ct += $category->Testimonials()->count();
75
            }
76 1
            if ($ct == 1) {
0 ignored issues
show
introduced by
The condition $ct == 1 is always false.
Loading history...
77
                $label = ' testimonial';
78
            } else {
79 1
                $label = ' testimonials';
80
            }
81 1
            return DBField::create_field(
82 1
                'HTMLText',
83 1
                $ct . $label
84 1
            )->Summary(20);
85
        }
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function provideBlockSchema()
92
    {
93
        $blockSchema = parent::provideBlockSchema();
94
        $blockSchema['content'] = $this->getSummary();
95
        return $blockSchema;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 1
    public function getType()
102
    {
103 1
        return _t(__CLASS__ . '.BlockType', 'Testimonials');
104
    }
105
106
    /**
107
     * @return FieldList
108
     */
109
    public function getCMSFields()
110
    {
111 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
112 1
            $fields->dataFieldByName('Content')
113 1
                ->setRows(8);
114
115 1
            $fields->dataFieldByName('Limit')
116 1
                ->setTitle('Number of testimonials to show');
117
118 1
            if ($this->exists()) {
119 1
                $fields->removeByName([
120 1
                    'TestimonialCategories'
121
                ]);
122
123 1
                $fields->addFieldToTab('Root.Main',
124 1
                    'Limit',
0 ignored issues
show
Bug introduced by
'Limit' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $field of SilverStripe\Forms\FieldList::addFieldToTab(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
                    /** @scrutinizer ignore-type */ 'Limit',
Loading history...
125 1
                    ListboxField::create(
126 1
                        'TestimonialCategories',
127 1
                        'Categories',
128 1
                        TestimonialCategory::get()->map()->toArray()
129
                    )
130
                );
131
            }
132 1
        });
133
134 1
        return parent::getCMSFields();
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 1
    public function getTestimonialsList()
141
    {
142 1
        $random = DB::get_conn()->random();
143 1
        $testimonials = Testimonial::get();
144
145 1
        $categories = $this->TestimonialCategories();
146 1
        if ($categories->count() > 0) {
147 1
            $testimonials = $testimonials->filterAny(['TestimonialCategories.ID' => $categories->column()]);
148
        }
149
150 1
        $testimonials = $testimonials->sort($random);
151 1
        if (0 < $this->Limit) {
152 1
            $testimonials = $testimonials->limit($this->Limit);
153
        }
154
155 1
        return $testimonials;
156
    }
157
}
158