Passed
Push — master ( 3396a4...cb1d79 )
by Josh
02:38
created

Template   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 91
dl 0
loc 265
rs 9.0399
c 0
b 0
f 0
wmc 42

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldName() 0 3 1
A setTvs() 0 3 1
A setFieldName() 0 3 1
A getFieldTemplateName() 0 3 1
A setFieldIcon() 0 4 1
A setFieldTemplateName() 0 4 1
A getFieldTemplateType() 0 3 1
A getFieldIcon() 0 3 1
A setFieldTemplateType() 0 4 1
A getCurrentVersion() 0 5 1
A attachTemplateVariable() 0 10 2
A seedRelated() 0 29 5
A detachTV() 0 3 1
B attachRelatedPieces() 0 36 8
B attachRelatedPiecesAfterSave() 0 13 7
A detachTemplateVariable() 0 7 2
B onDeleteRevertRelatedPieces() 0 19 7

How to fix   Complexity   

Complex Class

Complex classes like Template 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.

While breaking up the class, it is a good idea to analyze how other classes use Template, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jgulledge
5
 * Date: 9/30/2017
6
 * Time: 2:44 PM
7
 */
8
9
namespace LCI\Blend\Blendable;
10
11
12
use SebastianBergmann\CodeCoverage\Report\PHP;
13
14
class Template extends Element
15
{
16
    /** @var string  */
17
    protected $opt_cache_key = 'elements/templates';
18
19
    /** @var string ~ the xPDO class name */
20
    protected $xpdo_simple_object_class = 'modTemplate';
21
22
    /** @var string  */
23
    protected $unique_key_column = 'templatename';
24
25
    /** @var array  */
26
    protected $tv_names = [];
27
28
    /** @var array  */
29
    protected $detach_tvs = [];
30
31
    /** @var array  */
32
    protected $tv_seeds = [];
33
34
    /**
35
     * @return \LCI\Blend\Blendable\Template
36
     */
37
    public function getCurrentVersion()
38
    {
39
        /** @var \LCI\Blend\Blendable\Template $element */
40
        $element = new self($this->modx, $this->blender, $this->getFieldName());
41
        return $element->setSeedsDir($this->getSeedsDir());
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getFieldIcon()
48
    {
49
        return $this->blendable_xpdo_simple_object_data['icon'];
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getFieldName()
56
    {
57
        return $this->getFieldTemplateName();
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getFieldTemplateName()
64
    {
65
        return $this->blendable_xpdo_simple_object_data['templatename'];
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getFieldTemplateType()
72
    {
73
        return $this->blendable_xpdo_simple_object_data['template_type'];
74
    }
75
76
    /**
77
     * @param string $icon
78
     * @return $this
79
     */
80
    public function setFieldIcon($icon)
81
    {
82
        $this->blendable_xpdo_simple_object_data['icon'] = $icon;
83
        return $this;
84
    }
85
    /**
86
     * @param string $name
87
     * @return $this
88
     */
89
    public function setFieldName($name)
90
    {
91
        return $this->setFieldTemplateName($name);
92
    }
93
94
    /**
95
     * @param string $name
96
     * @return $this
97
     */
98
    public function setFieldTemplateName($name)
99
    {
100
        $this->blendable_xpdo_simple_object_data['templatename'] = $name;
101
        return $this;
102
    }
103
104
    /**
105
     * @param string $name
106
     * @return $this
107
     */
108
    public function setFieldTemplateType($name)
109
    {
110
        $this->blendable_xpdo_simple_object_data['template_type'] = $name;
111
        return $this;
112
    }
113
114
    /**
115
     * @param array $tvs
116
     */
117
    public function setTvs($tvs)
118
    {
119
        $this->tv_seeds = $tvs;
120
    }
121
122
    /**
123
     * @param string $tv_name
124
     * @param int $rank
125
     *
126
     * @return $this
127
     */
128
    public function attachTemplateVariable($tv_name, $rank = 0)
129
    {
130
        if (!isset($this->related_data['attach'])) {
131
            $this->related_data['attach'] = [];
132
        }
133
        $this->related_data['attach'][] = [
134
            'name' => $tv_name,
135
            'rank' => $rank
136
        ];
137
        return $this;
138
    }
139
140
    /**
141
     * @param $tv_name
142
     * @return $this
143
     */
144
    public function detachTemplateVariable($tv_name)
145
    {
146
        if (!isset($this->related_data['detach'])) {
147
            $this->related_data['detach'] = [];
148
        }
149
        $this->related_data['detach'][] = $tv_name;
150
        return $this;
151
    }
152
153
    /**
154
     * @deprecated use detachTemplateVariable
155
     * @param $tv_name
156
     * @return $this
157
     */
158
    public function detachTV($tv_name)
159
    {
160
        return $this->detachTemplateVariable($tv_name);
161
    }
162
163
    protected function attachRelatedPieces()
164
    {
165
        if (isset($this->related_data['seeds'])) {
166
            foreach ($this->related_data['seeds'] as $tv) {
167
                // blend the the TV from seed:
168
                $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']);
169
                $tvSeed
170
                    ->setSeedsDir($this->getSeedsDir())
171
                    ->blendFromSeed($tv['seed_key'], true);
172
173
                $this->attachTemplateVariable($tv['name'], $tv['rank']);
174
            }
175
        }
176
177
        if (isset($this->related_data['attach']) && count($this->related_data['attach']) > 0) {
178
            $tvs = [];
179
            foreach ($this->related_data['attach'] as $tv_name_data) {
180
                // get the TV:
181
                $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name_data['name']]);
182
                if ($tv) {
183
                    $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $tv->get('id'), 'templateid' => $this->xPDOSimpleObject->getPrimaryKey()]);
0 ignored issues
show
Bug introduced by
The method getPrimaryKey() does not exist on null. ( Ignorable by Annotation )

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

183
                    $tvt = $this->modx->getObject('modTemplateVarTemplate', ['tmplvarid' => $tv->get('id'), 'templateid' => $this->xPDOSimpleObject->/** @scrutinizer ignore-call */ getPrimaryKey()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
185
                    if (!$tvt) {
186
                        $tvt = $this->modx->newObject('modTemplateVarTemplate');
187
                    }
188
                    $tvt->set('tmplvarid', $tv->get('id'));
189
                    $tvt->set('rand', $tv_name_data['rank']);
190
191
                    $tvs[] = $tvt;
192
                } else {
193
                    $this->error = true;
194
195
                }
196
197
            }
198
            $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates');
199
        }
200
    }
201
202
    protected function attachRelatedPiecesAfterSave()
203
    {
204
        if (isset($this->related_data['detach']) && count($this->related_data['detach']) > 0) {
205
            foreach ($this->related_data['detach'] as $tv_name) {
206
                // get the TV:
207
                $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name]);
208
                if ($tv) {
209
                    $templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate', array(
210
                        'tmplvarid' => $tv->get('id'),
211
                        'templateid' => $this->xPDOSimpleObject->get('id'),
212
                    ));
213
                    if ($templateVarTemplate && $templateVarTemplate instanceof \modTemplateVarTemplate) {
0 ignored issues
show
Bug introduced by
The type modTemplateVarTemplate 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...
214
                        $templateVarTemplate->remove();
215
                    }
216
                }
217
            }
218
        }
219
    }
220
221
    /**
222
     *
223
     */
224
    protected function onDeleteRevertRelatedPieces()
225
    {
226
        if (!isset($this->related_data['seeds']) || empty($this->related_data['seeds'])) {
227
            // If this is a seed then the TVs are not in the revert file but the seed file
228
            $name = $this->getFieldTemplateName();
229
            if (empty ($name) && isset($this->current_xpdo_simple_object_data['templatename'])) {
230
                $name = $this->current_xpdo_simple_object_data['templatename'];
231
            }
232
            $this->loadObjectDataFromSeed($this->blender->getSeedKeyFromName($name));
233
        }
234
235
        if (isset($this->related_data['seeds'])) {
236
237
            foreach ($this->related_data['seeds'] as $tv) {
238
                // seed the TV:
239
                $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']);
240
                $tvSeed
241
                    ->setSeedsDir($this->getSeedsDir())
242
                    ->revertBlend();
243
            }
244
        }
245
    }
246
247
    /**
248
     * @var string $type blend or revert
249
     */
250
    protected function seedRelated($type = 'blend')
251
    {
252
        // get all related TVs:
253
        $tv_keys = [];
254
        if (is_object($this->xPDOSimpleObject)) {
255
            $tvTemplates = $this->xPDOSimpleObject->getMany('TemplateVarTemplates');
256
            /** @var \modTemplateVarTemplate $tvTemplate */
257
            foreach ($tvTemplates as $tvTemplate) {
258
                /** @var \modTemplateVar $tv */
259
                $tv = $tvTemplate->getOne('TemplateVar');
260
                $tv_name = $tv->get('name');
261
262
                $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv_name);
263
                $seed_key = $tvSeed
264
                    ->setSeedsDir($this->getSeedsDir())
265
                    ->seed($this->type == 'revert' ? 'revert' : 'seed');
266
267
                $tv_keys[] = [
268
                    'seed_key' => $seed_key,
269
                    'name' => $tv_name,
270
                    'rank' => $tvTemplate->get('rank')
271
                ];
272
                $this->blender->out('TV '.$tv_name.' has been seeded: '.$seed_key);
273
            }
274
275
            $this->related_data['seeds'] = $tv_keys;
276
277
        } elseif ($type != 'revert') {
278
            $this->related_data['seeds'] = $tv_keys;
279
        }
280
281
    }
282
}
283