Test Failed
Push — master ( 8ca562...1286bb )
by Josh
02:13
created

Template::onDeleteRevertRelatedPieces()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
rs 8.8333
c 0
b 0
f 0
cc 7
nc 9
nop 0
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 detachTV($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
    protected function attachRelatedPieces()
154
    {
155
        if (isset($this->related_data['seeds'])) {
156
            foreach ($this->related_data['seeds'] as $tv) {
157
                // blend the the TV from seed:
158
                $tvSeed = new TemplateVariable($this->modx, $this->blender, $tv['name']);
159
                $tvSeed
160
                    ->setSeedsDir($this->getSeedsDir())
161
                    ->blendFromSeed($tv['seed_key'], true);
162
163
                $this->attachTemplateVariable($tv['name'], $tv['rank']);
164
            }
165
        }
166
167
        if (isset($this->related_data['attach']) && count($this->related_data['attach']) > 0) {
168
            $tvs = [];
169
            foreach ($this->related_data['attach'] as $tv_name_data) {
170
                // get the TV:
171
                $tv = $this->modx->getObject('modTemplateVar', ['name' => $tv_name_data['name']]);
172
                if ($tv) {
173
                    $tvt = $this->modx->newObject('modTemplateVarTemplate');
174
                    $tvt->set('tmplvarid', $tv->get('id'));
175
                    $tvt->set('rand', $tv_name_data['rank']);
176
177
                    $tvs[] = $tvt;
178
                } else {
179
                    $this->error = true;
180
181
                }
182
183
            }
184
            $this->xPDOSimpleObject->addMany($tvs, 'TemplateVarTemplates');
0 ignored issues
show
Bug introduced by
The method addMany() 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

184
            $this->xPDOSimpleObject->/** @scrutinizer ignore-call */ 
185
                                     addMany($tvs, 'TemplateVarTemplates');

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