Test Failed
Push — master ( 844051...8ca562 )
by Josh
02:22
created

Resource::loadRelatedData()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 51
rs 8.8817
c 0
b 0
f 0
cc 6
nc 8
nop 0

How to fix   Long Method   

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:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jgulledge
5
 * Date: 10/6/2017
6
 * Time: 9:01 AM
7
 */
8
9
namespace LCI\Blend\Blendable;
10
use LCI\Blend\Blender;
11
12
/**
13
 * Class Resource
14
 * @package LCI\Blend\Blendable
15
 */
16
class Resource extends Blendable
17
{
18
    use BlendableProperties;
19
20
    /** @var string  */
21
    protected $opt_cache_key = 'resources/web';
22
23
    /** @var string ex: modResource */
24
    protected $xpdo_simple_object_class = 'modResource';
25
26
    /** @var string  */
27
    protected $unique_key_column = 'alias';
28
29
    /** @var array ~ this should match data to be inserted via xPDO, ex [column_name => value, ...] */
30
    protected $blendable_xpdo_simple_object_data = [
31
        'alias' => '',
32
        'alias_visible' => true,
33
        'cacheable' => true,
34
        'class_key' => 'modDocument',
35
        'content' => '',
36
        'contentType' => 'text/html',
37
        'content_dispo' => false,
38
        'context_key' => 'web',
39
        'createdby' => 0,
40
        'deleted' => false,
41
        'description' => '',
42
        'donthit' => false,
43
        'hidemenu' => false,
44
        'hide_children_in_tree' => false,
45
        'introtext' => '',
46
        'isfolder' => false,
47
        'link_attributes' => '',
48
        'longtitle' => '',
49
        'menuindex' => 0,
50
        'menutitle' => '',
51
        'pagetitle' => '',
52
        'parent' => 0,
53
        'privatemgr' => false,
54
        'privateweb' => false,
55
        'properties' => '',
56
        'published' => false,
57
        'publishedby' => 0,
58
        'publishedon' => 0,
59
        'pub_date' => 0,
60
        'richtext' => true,
61
        'searchable' => true,
62
        'show_in_tree' => true,
63
        'template' => 0,
64
        'type' => 'document',
65
        'unpub_date' => 0,
66
        'uri' => '',
67
        'uri_override' => false,
68
    ];
69
70
    /** @var array ~ ['setMethodName' => 'setMethodActualName', 'setDoNotUseMethod' => false] overwrite in child classes */
71
    protected $load_from_array_aliases = [
72
        'setProperties' => 'mergePropertiesFromArray'
73
    ];
74
75
    /** @var array  */
76
    protected $portable_template_variables = [];
77
78
    /**
79
     * Resource constructor.
80
     *
81
     * @param \modx $modx
82
     * @param Blender $blender
83
     * @param string $alias ~ alias as string
84
     * @param string $context ~ will default to web context
85
     */
86
    public function __construct(\modx $modx, Blender $blender, $alias, $context = 'web')
0 ignored issues
show
Bug introduced by
The type modx 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...
87
    {
88
        $this->setFieldAlias($alias);
89
        $this->setFieldContextKey($context);
90
        parent::__construct($modx, $blender, ['alias' => $alias, 'context_key' => $context]);
91
92
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.mediaSources'));
93
        if (count($additional) > 0) {
94
            foreach ($additional as $tv_name) {
95
                $this->portable_template_variables[$tv_name] = 'media_source';
96
            }
97
        }
98
99
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.resources'));
100
        if (count($additional) > 0) {
101
            foreach ($additional as $tv_name) {
102
                $this->portable_template_variables[$tv_name] = 'resource';
103
            }
104
        }
105
106
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.templates'));
107
        if (count($additional) > 0) {
108
            foreach ($additional as $tv_name) {
109
                $this->portable_template_variables[$tv_name] = 'template';
110
            }
111
        }
112
    }
113
114
115
    // Column Getters:
116
    /**
117
     * @return string
118
     */
119
    public function getFieldAlias()
120
    {
121
        return $this->blendable_xpdo_simple_object_data['alias'];
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function getFieldAliasVisible()
128
    {
129
        return $this->blendable_xpdo_simple_object_data['alias_visible'];
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function getFieldCacheable()
136
    {
137
        return $this->blendable_xpdo_simple_object_data['cacheable'];
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getFieldClassKey()
144
    {
145
        return $this->blendable_xpdo_simple_object_data['class_key'];
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getFieldContent()
152
    {
153
        return $this->blendable_xpdo_simple_object_data['content'];
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getFieldContentType()
160
    {
161
        return $this->blendable_xpdo_simple_object_data['contentType'];
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function getFieldContentDispo()
168
    {
169
        return $this->blendable_xpdo_simple_object_data['content_dispo'];
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getFieldContextKey()
176
    {
177
        return $this->blendable_xpdo_simple_object_data['context_key'];
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getFieldCreatedby()
184
    {
185
        return $this->blendable_xpdo_simple_object_data['createdby'];
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function getFieldDeleted()
192
    {
193
        return $this->blendable_xpdo_simple_object_data['deleted'];
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getFieldDescription()
200
    {
201
        return $this->blendable_xpdo_simple_object_data['description'];
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function getFieldDonthit()
208
    {
209
        return $this->blendable_xpdo_simple_object_data['donthit'];
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function getFieldHidemenu()
216
    {
217
        return $this->blendable_xpdo_simple_object_data['hidemenu'];
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function getFieldHideChildrenInTree()
224
    {
225
        return $this->blendable_xpdo_simple_object_data['hide_children_in_tree'];
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getFieldIntrotext()
232
    {
233
        return $this->blendable_xpdo_simple_object_data['introtext'];
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function getFieldIsfolder()
240
    {
241
        return $this->blendable_xpdo_simple_object_data['isfolder'];
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getFieldLinkAttributes()
248
    {
249
        return $this->blendable_xpdo_simple_object_data['link_attributes'];
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function getFieldLongtitle()
256
    {
257
        return $this->blendable_xpdo_simple_object_data['longtitle'];
258
    }
259
260
    /**
261
     * @return int
262
     */
263
    public function getFieldMenuindex()
264
    {
265
        return $this->blendable_xpdo_simple_object_data['menuindex'];
266
    }
267
268
    /**
269
     * @return string
270
     */
271
    public function getFieldMenutitle()
272
    {
273
        return $this->blendable_xpdo_simple_object_data['menutitle'];
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public function getFieldPagetitle()
280
    {
281
        return $this->blendable_xpdo_simple_object_data['pagetitle'];
282
    }
283
284
    /**
285
     * @return int
286
     */
287
    public function getFieldParent()
288
    {
289
        return $this->blendable_xpdo_simple_object_data['parent'];
290
    }
291
292
    /**
293
     * @return bool
294
     */
295
    public function getFieldPrivatemgr()
296
    {
297
        return $this->blendable_xpdo_simple_object_data['privatemgr'];
298
    }
299
300
    /**
301
     * @return bool
302
     */
303
    public function getFieldPrivateweb()
304
    {
305
        return $this->blendable_xpdo_simple_object_data['privateweb'];
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    public function getFieldProperties()
312
    {
313
        return $this->blendable_xpdo_simple_object_data['properties'];
314
    }
315
316
    /**
317
     * @return bool
318
     */
319
    public function getFieldPublished()
320
    {
321
        return $this->blendable_xpdo_simple_object_data['published'];
322
    }
323
324
    /**
325
     * @return int
326
     */
327
    public function getFieldPublishedby()
328
    {
329
        return $this->blendable_xpdo_simple_object_data['publishedby'];
330
    }
331
332
    /**
333
     * @return int
334
     */
335
    public function getFieldPublishedon()
336
    {
337
        return $this->blendable_xpdo_simple_object_data['publishedon'];
338
    }
339
340
    /**
341
     * @return int
342
     */
343
    public function getFieldPubDate()
344
    {
345
        return $this->blendable_xpdo_simple_object_data['pub_date'];
346
    }
347
348
    /**
349
     * @return bool
350
     */
351
    public function getFieldRichtext()
352
    {
353
        return $this->blendable_xpdo_simple_object_data['richtext'];
354
    }
355
356
    /**
357
     * @return bool
358
     */
359
    public function getFieldSearchable()
360
    {
361
        return $this->blendable_xpdo_simple_object_data['searchable'];
362
    }
363
364
    /**
365
     * @return bool
366
     */
367
    public function getFieldShowInTree()
368
    {
369
        return $this->blendable_xpdo_simple_object_data['show_in_tree'];
370
    }
371
372
    /**
373
     * @return int
374
     */
375
    public function getFieldTemplate()
376
    {
377
        return $this->blendable_xpdo_simple_object_data['template'];
378
    }
379
380
    /**
381
     * @return string
382
     */
383
    public function getFieldType()
384
    {
385
        return $this->blendable_xpdo_simple_object_data['type'];
386
    }
387
388
    /**
389
     * @return int
390
     */
391
    public function getFieldUnpubDate()
392
    {
393
        return $this->blendable_xpdo_simple_object_data['unpub_date'];
394
    }
395
396
    /**
397
     * @return string
398
     */
399
    public function getFieldUri()
400
    {
401
        return $this->blendable_xpdo_simple_object_data['uri'];
402
    }
403
404
    /**
405
     * @return bool
406
     */
407
    public function getFieldUriOverride()
408
    {
409
        return $this->blendable_xpdo_simple_object_data['uri_override'];
410
    }
411
412
    // Column Setters:
413
    /**
414
     * @param string $value  max characters: 191
415
     * @return $this
416
     */
417
    public function setFieldAlias($value)
418
    {
419
        $this->blendable_xpdo_simple_object_data['alias'] = $value;
420
        return $this;
421
    }
422
423
    /**
424
     * @param bool $value
425
     * @return $this
426
     */
427
    public function setFieldAliasVisible($value)
428
    {
429
        $this->blendable_xpdo_simple_object_data['alias_visible'] = $value;
430
        return $this;
431
    }
432
433
    /**
434
     * @param bool $value
435
     * @return $this
436
     */
437
    public function setFieldCacheable($value)
438
    {
439
        $this->blendable_xpdo_simple_object_data['cacheable'] = $value;
440
        return $this;
441
    }
442
443
    /**
444
     * @param string $value  max characters: 100
445
     * @return $this
446
     */
447
    public function setFieldClassKey($value)
448
    {
449
        $this->blendable_xpdo_simple_object_data['class_key'] = $value;
450
        return $this;
451
    }
452
453
    /**
454
     * @param string $value
455
     * @return $this
456
     */
457
    public function setFieldContent($value)
458
    {
459
        $this->blendable_xpdo_simple_object_data['content'] = $value;
460
        return $this;
461
    }
462
463
    /**
464
     * @param string $value  max characters: 50
465
     * @return $this
466
     */
467
    public function setFieldContentType($value)
468
    {
469
        $this->blendable_xpdo_simple_object_data['contentType'] = $value;
470
        return $this;
471
    }
472
473
    /**
474
     * @param bool $value
475
     * @return $this
476
     */
477
    public function setFieldContentDispo($value)
478
    {
479
        $this->blendable_xpdo_simple_object_data['content_dispo'] = $value;
480
        return $this;
481
    }
482
483
    /**
484
     * @param string $value  max characters: 100
485
     * @return $this
486
     */
487
    public function setFieldContextKey($value)
488
    {
489
        $this->blendable_xpdo_simple_object_data['context_key'] = $value;
490
        $this->opt_cache_key = 'resources/'.$value;
491
        return $this;
492
    }
493
494
    /**
495
     * @param int $value
496
     * @return $this
497
     */
498
    public function setFieldCreatedby($value)
499
    {
500
        $this->blendable_xpdo_simple_object_data['createdby'] = $value;
501
        return $this;
502
    }
503
504
    /**
505
     * @param bool $value
506
     * @return $this
507
     */
508
    public function setFieldDeleted($value)
509
    {
510
        $this->blendable_xpdo_simple_object_data['deleted'] = $value;
511
        return $this;
512
    }
513
514
    /**
515
     * @param string $value  max characters: 191
516
     * @return $this
517
     */
518
    public function setFieldDescription($value)
519
    {
520
        $this->blendable_xpdo_simple_object_data['description'] = $value;
521
        return $this;
522
    }
523
524
    /**
525
     * @param bool $value
526
     * @return $this
527
     */
528
    public function setFieldDonthit($value)
529
    {
530
        $this->blendable_xpdo_simple_object_data['donthit'] = $value;
531
        return $this;
532
    }
533
534
    /**
535
     * @param bool $value
536
     * @return $this
537
     */
538
    public function setFieldHidemenu($value)
539
    {
540
        $this->blendable_xpdo_simple_object_data['hidemenu'] = $value;
541
        return $this;
542
    }
543
544
    /**
545
     * @param bool $value
546
     * @return $this
547
     */
548
    public function setFieldHideChildrenInTree($value)
549
    {
550
        $this->blendable_xpdo_simple_object_data['hide_children_in_tree'] = $value;
551
        return $this;
552
    }
553
554
    /**
555
     * @param string $value
556
     * @return $this
557
     */
558
    public function setFieldIntrotext($value)
559
    {
560
        $this->blendable_xpdo_simple_object_data['introtext'] = $value;
561
        return $this;
562
    }
563
564
    /**
565
     * @param bool $value
566
     * @return $this
567
     */
568
    public function setFieldIsfolder($value)
569
    {
570
        $this->blendable_xpdo_simple_object_data['isfolder'] = $value;
571
        return $this;
572
    }
573
574
    /**
575
     * @param string $value  max characters: 191
576
     * @return $this
577
     */
578
    public function setFieldLinkAttributes($value)
579
    {
580
        $this->blendable_xpdo_simple_object_data['link_attributes'] = $value;
581
        return $this;
582
    }
583
584
    /**
585
     * @param string $value  max characters: 191
586
     * @return $this
587
     */
588
    public function setFieldLongtitle($value)
589
    {
590
        $this->blendable_xpdo_simple_object_data['longtitle'] = $value;
591
        return $this;
592
    }
593
594
    /**
595
     * @param int $value
596
     * @return $this
597
     */
598
    public function setFieldMenuindex($value)
599
    {
600
        $this->blendable_xpdo_simple_object_data['menuindex'] = $value;
601
        return $this;
602
    }
603
604
    /**
605
     * @param string $value  max characters: 191
606
     * @return $this
607
     */
608
    public function setFieldMenutitle($value)
609
    {
610
        $this->blendable_xpdo_simple_object_data['menutitle'] = $value;
611
        return $this;
612
    }
613
614
    /**
615
     * @param string $value  max characters: 191
616
     * @return $this
617
     */
618
    public function setFieldPagetitle($value)
619
    {
620
        $this->blendable_xpdo_simple_object_data['pagetitle'] = $value;
621
        return $this;
622
    }
623
624
    /**
625
     * @param int $value
626
     * @return $this
627
     */
628
    public function setFieldParent($value)
629
    {
630
        $this->blendable_xpdo_simple_object_data['parent'] = $value;
631
        return $this;
632
    }
633
634
    /**
635
     * @param bool $value
636
     * @return $this
637
     */
638
    public function setFieldPrivatemgr($value)
639
    {
640
        $this->blendable_xpdo_simple_object_data['privatemgr'] = $value;
641
        return $this;
642
    }
643
644
    /**
645
     * @param bool $value
646
     * @return $this
647
     */
648
    public function setFieldPrivateweb($value)
649
    {
650
        $this->blendable_xpdo_simple_object_data['privateweb'] = $value;
651
        return $this;
652
    }
653
654
    /**
655
     * @param string $value
656
     * @return $this
657
     */
658
    public function setFieldProperties($value)
659
    {
660
        $this->blendable_xpdo_simple_object_data['properties'] = $value;
661
        return $this;
662
    }
663
664
    /**
665
     * @param bool $value
666
     * @return $this
667
     */
668
    public function setFieldPublished($value)
669
    {
670
        $this->blendable_xpdo_simple_object_data['published'] = $value;
671
        return $this;
672
    }
673
674
    /**
675
     * @param int $value
676
     * @return $this
677
     */
678
    public function setFieldPublishedby($value)
679
    {
680
        $this->blendable_xpdo_simple_object_data['publishedby'] = $value;
681
        return $this;
682
    }
683
684
    /**
685
     * @param int $value
686
     * @return $this
687
     */
688
    public function setFieldPublishedon($value)
689
    {
690
        $this->blendable_xpdo_simple_object_data['publishedon'] = $value;
691
        return $this;
692
    }
693
694
    /**
695
     * @param int $value
696
     * @return $this
697
     */
698
    public function setFieldPubDate($value)
699
    {
700
        $this->blendable_xpdo_simple_object_data['pub_date'] = $value;
701
        return $this;
702
    }
703
704
    /**
705
     * @param bool $value
706
     * @return $this
707
     */
708
    public function setFieldRichtext($value)
709
    {
710
        $this->blendable_xpdo_simple_object_data['richtext'] = $value;
711
        return $this;
712
    }
713
714
    /**
715
     * @param bool $value
716
     * @return $this
717
     */
718
    public function setFieldSearchable($value)
719
    {
720
        $this->blendable_xpdo_simple_object_data['searchable'] = $value;
721
        return $this;
722
    }
723
724
    /**
725
     * @param bool $value
726
     * @return $this
727
     */
728
    public function setFieldShowInTree($value)
729
    {
730
        $this->blendable_xpdo_simple_object_data['show_in_tree'] = $value;
731
        return $this;
732
    }
733
734
    /**
735
     * @param int $value
736
     * @return $this
737
     */
738
    public function setFieldTemplate($value)
739
    {
740
        $this->blendable_xpdo_simple_object_data['template'] = $value;
741
        return $this;
742
    }
743
744
    /**
745
     * @param string $value  max characters: 20
746
     * @return $this
747
     */
748
    public function setFieldType($value)
749
    {
750
        $this->blendable_xpdo_simple_object_data['type'] = $value;
751
        return $this;
752
    }
753
754
    /**
755
     * @param int $value
756
     * @return $this
757
     */
758
    public function setFieldUnpubDate($value)
759
    {
760
        $this->blendable_xpdo_simple_object_data['unpub_date'] = $value;
761
        return $this;
762
    }
763
764
    /**
765
     * @param string $value
766
     * @return $this
767
     */
768
    public function setFieldUri($value)
769
    {
770
        $this->blendable_xpdo_simple_object_data['uri'] = $value;
771
        return $this;
772
    }
773
774
    /**
775
     * @param bool $value
776
     * @return $this
777
     */
778
    public function setFieldUriOverride($value)
779
    {
780
        $this->blendable_xpdo_simple_object_data['uri_override'] = $value;
781
        return $this;
782
    }
783
784
785
    /**
786
     * @return array
787
     */
788
    protected function getUniqueCriteria()
789
    {
790
        return [
791
            $this->unique_key_column => $this->blendable_xpdo_simple_object_data[$this->unique_key_column],
792
            'context_key' => $this->getFieldContextKey()
793
        ];
794
    }
795
796
    /**
797
     * @param string|array $criteria ~ alias as string will default to web context or ['alias' => 'my-page.html', 'context_key' => 'web']
798
     */
799
    protected function setUniqueCriteria($criteria)
800
    {
801
        if (is_string($criteria)) {
802
            $this->setFieldAlias($criteria);
803
        } else {
804
            $this->setFieldAlias($criteria['alias']);
805
            $this->setFieldContextKey($criteria['context_key']);
806
        }
807
    }
808
809
    /**
810
     *
811
     */
812
    protected function assignResourceGroups()
813
    {
814
        $new_groups = (isset($this->related_data['resource_groups']) ? $this->related_data['resource_groups'] : []);
815
816
        // resource groups
817
        $current_groups = $this->xPDOSimpleObject->getResourceGroupNames();
0 ignored issues
show
Bug introduced by
The method getResourceGroupNames() 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

817
        /** @scrutinizer ignore-call */ 
818
        $current_groups = $this->xPDOSimpleObject->getResourceGroupNames();

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...
818
        foreach ($current_groups as $group) {
819
            if (!in_array($group, $new_groups)) {
820
                $this->xPDOSimpleObject->leaveGroup($group);
821
            }
822
        }
823
824
        foreach ($new_groups as $group) {
825
            /** @var \modResourceGroup $resourceGroup */
826
            $resourceGroup = $this->modx->getObject('modResourceGroup', ['name' => $group]);
827
            if (!$resourceGroup || !$resourceGroup instanceof \modResourceGroup) {
0 ignored issues
show
Bug introduced by
The type modResourceGroup 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...
828
                // create the resource group if it does not exist
829
                $this->blender->out('Attempting to create a new Resource Group: '.$group);
830
                $resourceGroup = $this->modx->newObject('modResourceGroup');
831
                $resourceGroup->set('name', $group);
832
                $resourceGroup->save();
833
            }
834
835
            if (!in_array($group, $current_groups)) {
836
                $this->xPDOSimpleObject->joinGroup($group);
837
            }
838
        }
839
    }
840
841
    /**
842
     * @param string $seed_key
843
     * @param string $context
844
     *
845
     * @return bool|\modResource
0 ignored issues
show
Bug introduced by
The type modResource 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...
846
     */
847
    public function getResourceFromSeedKey($seed_key, $context = 'web')
848
    {
849
        // get the alias:
850
        $alias = $this->blender->getAliasFromSeedKey($seed_key);
851
852
        return $this->modx->getObject('modResource', ['alias' => $alias, 'context_key' => $context]);
853
    }
854
855
    /**
856
     * @param string $type ~ seed or revert
857
     * @return string
858
     */
859
    public function getSeedKey($type = 'seed')
860
    {
861
        $key = $this->blender->getSeedKeyFromAlias($this->blendable_xpdo_simple_object_data['alias']);
862
863
        switch ($type) {
864
            case 'revert':
865
                $seed_key = 'revert-'.$key;
866
                break;
867
868
            case 'seed':
869
                // no break
870
            default:
871
                $seed_key = $key;
872
        }
873
874
        return $seed_key;
875
    }
876
877
878
    /**
879
     * @param array $resource_data
880
     *
881
     * @return $this
882
     */
883
    public function setResourceData(array $resource_data)
884
    {
885
        $this->blendable_xpdo_simple_object_data = $resource_data;
886
        return $this;
887
    }
888
889
890
    /**
891
     * @return Blendable
892
     */
893
    public function getCurrentVersion()
894
    {
895
        /** @var \LCI\Blend\Blendable\Resource $resource */
896
        $resource = new self($this->modx, $this->blender, $this->getFieldAlias(), $this->getFieldContextKey());
897
        return $resource
898
            ->setSeedsDir($this->getSeedsDir());
899
    }
900
901
    /**
902
     * Override in child classes
903
     */
904
    protected function loadRelatedData()
905
    {
906
        /** @var \modResource $this->xPDOSimpleObject */
907
        $this->xPDOSimpleObject;
908
        // no IDs only TV name
909
        $tvs = []; // TemplateVarResources modTemplateVarResource
910
911
        $template = false;
912
        $resource_groups = [];
913
        if (is_object($this->xPDOSimpleObject)) {
914
            /** @var \modTemplate $template */
915
            $template = $this->xPDOSimpleObject->getOne('Template');
916
            $resource_groups = $this->xPDOSimpleObject->getResourceGroupNames();
917
        }
918
        if (is_object($template)) {
919
920
            // get all TemplateValues
921
            $tvTemplates = $template->getMany('TemplateVarTemplates');
922
            /** @var \modTemplateVarTemplates $tvTemplate */
923
            foreach ($tvTemplates as $tvTemplate) {
924
                /** @var \modTemplateVar $tv */
925
                $tv = $tvTemplate->getOne('TemplateVar');
926
                $tv_name = $tv->get('name');
927
928
                // Raw TV value:
929
                $tvResource = $this->modx->getObject(
930
                    'modTemplateVarResource',
931
                    ['tmplvarid' => $tv->get('id'), 'contentid' => $this->xPDOSimpleObject->get('id')]
932
                );
933
934
                if ($tvResource instanceof \modTemplateVarResource) {
0 ignored issues
show
Bug introduced by
The type modTemplateVarResource 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...
935
                    $tvs[$tv_name] = $this->makePortableTVData($tv_name, $tv->get('type'), $tvResource->get('value'));
936
                }
937
            }
938
        }
939
940
        $extras = [];
941
        // tagger:
942
        $tagger = $this->blender->getTagger();
943
        if ($tagger instanceof \Tagger) {
0 ignored issues
show
Bug introduced by
The type Tagger 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...
944
            $extras['tagger'] = $this->getResourceTags();
945
        }
946
947
        $this->related_data = [
948
            'extras' => $extras,
949
            'tvs' => $tvs,
950
            'resource_groups' => $resource_groups
951
        ];
952
953
        // Calls on the event: OnBlendLoadRelatedData
954
        parent::loadRelatedData();
955
    }
956
957
    /**
958
     * @param string $tv_name
959
     * @return bool|string
960
     */
961
    protected function getPortableTVType($tv_name, $tv_type)
962
    {
963
        $type = false;
964
965
        // These are set via system settings:
966
        if (isset($this->portable_template_variables[$tv_name])) {
967
            return $this->portable_template_variables[$tv_name];
968
        }
969
970
        switch ($tv_type) {
971
            case 'resourcelist':
972
                $type = 'resource';
973
                break;
974
        }
975
976
        return $type;
977
    }
978
979
    /**
980
     * @param string $tv_name
981
     * @param string $tv_type
982
     * @param mixed $value
983
     * @return array
984
     */
985
    protected function makePortableTVData($tv_name, $tv_type, $value)
986
    {
987
        $portable = [
988
            'type' => $tv_type,
989
            'value' => $value
990
        ];
991
992
        $type = $this->getPortableTVType($tv_name, $tv_type);
993
994
        switch ($type) {
995
            case 'media_source':
996
                $mediaSource = $this->modx->getObject('modMediaSource', $value);
997
                if (is_object($mediaSource)) {
998
                    $portable['portable_type'] = 'media_source';
999
                    $portable['portable_value'] = $mediaSource->get('name');
1000
                }
1001
                break;
1002
1003
            case 'resource':
1004
                $portable['portable_type'] = 'resource';
1005
                $portable['portable_value'] = $this->blender->getResourceSeedKeyFromID($value);
1006
                break;
1007
1008
            case 'template':
1009
                $template = $this->modx->getObject('modTemplate', $value);
1010
                if (is_object($template)) {
1011
                    $portable['portable_type'] = 'media_source';
1012
                    $portable['portable_value'] = $template->get('templatename');
1013
                }
1014
                break;
1015
        }
1016
1017
        return $portable;
1018
    }
1019
1020
    protected function uniqueCriteria()
1021
    {
1022
        return [
1023
            $this->unique_key_column => $this->blendable_xpdo_simple_object_data[$this->unique_key_column],
1024
            'context' => $this->getFieldContextKey()
1025
        ];
1026
    }
1027
1028
    /**
1029
     * Create convert methods for any portable data column that needs to be converted to an int for a related primary key
1030
     */
1031
    /**
1032
     * @param array $parent
1033
     * @return int
1034
     */
1035
    protected function convertParent($parent)
1036
    {
1037
        if (!is_array($parent)) {
0 ignored issues
show
introduced by
The condition is_array($parent) is always true.
Loading history...
1038
            // @TODO throw exception
1039
            return 0;
1040
        }
1041
        return (int)$this->blender->getResourceIDFromSeedKey(
1042
            $parent['seed_key'],
1043
            $parent['context']
1044
        );
1045
    }
1046
    /**
1047
     * @param string $name
1048
     * @return int
1049
     */
1050
    protected function convertTemplate($name)
1051
    {
1052
        $id = 0;
1053
1054
        // Should there be an event fired here? Allowing to alter the name or returned ID?
1055
        $template = $this->modx->getObject('modTemplate', ['templatename' => $name]);
1056
        if ($template) {
1057
            $id = $template->get('id');
1058
            $this->blender->out(' Template ID set: '.$id);
1059
        } else {
1060
            $this->blender->out(' Template not found: '.$name, true);
1061
        }
1062
        return $id;
1063
    }
1064
1065
    /**
1066
     * @param array $tv_data
1067
     * @return string|int|mixed $value
1068
     */
1069
    protected function convertToLocalTVData($tv_data)
1070
    {
1071
        $value = $tv_data['value'];
1072
        if (is_array($tv_data) && isset($tv_data['portable_type']) && isset($tv_data['portable_value'])) {
1073
            switch ($tv_data['portable_type']) {
1074
                case 'media_source':
1075
                    $mediaSource = $this->modx->getObject('modMediaSource', ['name' => $tv_data['portable_value']]);
1076
                    if (is_object($mediaSource)) {
1077
                        $value = $mediaSource->get('id');
1078
                    }
1079
                    break;
1080
1081
                case 'resource':
1082
                    $value = $this->blender->getResourceIDFromSeedKey($tv_data['portable_value']['seed_key'], $tv_data['portable_value']['context']);
1083
                    break;
1084
1085
                case 'template':
1086
                    $template = $this->modx->getObject('modTemplate', ['templatename' => $tv_data['portable_value']]);
1087
                    if (is_object($template)) {
1088
                        $value = $template->get('id');
1089
                    }
1090
                    break;
1091
            }
1092
        }
1093
1094
        return $value;
1095
    }
1096
1097
    /**
1098
     * Create seed methods for any column that needs be portable, from an int to string|array
1099
     */
1100
    /**
1101
     * @param int $parent_id
1102
     * @return array
1103
     */
1104
    protected function seedParent($parent_id)
1105
    {
1106
        return $this->blender->getResourceSeedKeyFromID($parent_id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->blender->g...edKeyFromID($parent_id) also could return the type boolean which is incompatible with the documented return type array.
Loading history...
1107
    }
1108
1109
    /**
1110
     * @param int $template_id
1111
     * @return string
1112
     */
1113
    protected function seedTemplate($template_id)
0 ignored issues
show
Unused Code introduced by
The parameter $template_id is not used and could be removed. ( Ignorable by Annotation )

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

1113
    protected function seedTemplate(/** @scrutinizer ignore-unused */ $template_id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1114
    {
1115
        $template = $this->xPDOSimpleObject->getOne('Template');
1116
1117
        if (is_object($template)) {
1118
            return $template->get('templatename');
1119
        }
1120
        return '';
1121
    }
1122
1123
1124
    /**
1125
     * This method is called just before blend/save()
1126
     */
1127
    protected function attachRelatedPieces()
1128
    {
1129
1130
    }
1131
1132
    /**
1133
     * This method is called just after a successful blend/save()
1134
     */
1135
    protected function attachRelatedPiecesAfterSave()
1136
    {
1137
        if (isset($this->related_data['tvs'])) {
1138
            $tvs = $this->related_data['tvs'];
1139
            if (is_array($tvs) && count($tvs) > 0) {
1140
                foreach ($tvs as $tv_name => $tv_data) {
1141
                    $value = $this->convertToLocalTVData($tv_data);
1142
1143
                    $this->xPDOSimpleObject->setTVValue($tv_name, $value);
1144
                }
1145
            }
1146
        }
1147
1148
        $this->assignResourceGroups();
1149
1150
        // extras
1151
        $tagger = $this->blender->getTagger();
1152
        if ($tagger instanceof \Tagger) {
1153
            $this->setResourceTags($this->xPDOSimpleObject, (isset($this->related_data['extras']['tagger']) ? $this->related_data['extras']['tagger'] : []));
0 ignored issues
show
Bug introduced by
It seems like $this->xPDOSimpleObject can also be of type null; however, parameter $resource of LCI\Blend\Blendable\Resource::setResourceTags() does only seem to accept modResource, maybe add an additional type check? ( Ignorable by Annotation )

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

1153
            $this->setResourceTags(/** @scrutinizer ignore-type */ $this->xPDOSimpleObject, (isset($this->related_data['extras']['tagger']) ? $this->related_data['extras']['tagger'] : []));
Loading history...
1154
        }
1155
    }
1156
1157
    // current_xpdo_simple_object_data for seeds: and seedRelated
1158
1159
1160
    // Extras, to be removed
1161
    /**
1162
     * @return array
1163
     */
1164
    protected function getResourceTags()
1165
    {
1166
        $tags = [];
1167
        [
1168
            'group-alias' => [
1169
                'tags' => [],
1170
                'columns' => []
1171
            ]
1172
        ];
1173
        // get resource Group
1174
        // Get all tags for resource:
1175
1176
        $query = $this->modx->newQuery('TaggerTag');
1177
1178
        $query->leftJoin('TaggerTagResource', 'Resources');
1179
        $query->leftJoin('TaggerGroup', 'Group');
1180
        $query->leftJoin('modResource', 'Resource', ['Resources.resource = Resource.id']);
1181
1182
        $query->select($this->modx->getSelectColumns('TaggerTag', 'TaggerTag'));
1183
        $query->select($this->modx->getSelectColumns('TaggerGroup', 'Group', 'group_'));
1184
1185
        $query->where(['Resources.resource' => $this->xPDOSimpleObject->get('id')]);
1186
1187
        $query->prepare();
1188
        $sql = $query->toSql();
1189
1190
        $results = $this->modx->query($sql);
1191
1192
        while ($tag = $results->fetch(\PDO::FETCH_ASSOC)) {
1193
1194
            $tag_columns = $group_columns = [];
1195
            foreach ($tag as $name => $value) {
1196
                if (strpos(' '.$name, 'group_') === 1) {
1197
                    $group_columns[substr($name, strlen('group_'))] = $value;
1198
                } else {
1199
                    $tag_columns[$name] = $value;
1200
                }
1201
            }
1202
            if (!isset($tags[$tag['group_name']])) {
1203
                $tags[$tag['group_alias']] = [
1204
                    'columns' => $group_columns,
1205
                    'tags' => []
1206
                ];
1207
            }
1208
1209
            $tags[$tag['group_alias']]['tags'][$tag['alias']] = $tag_columns;
1210
        }
1211
1212
        return $tags;
1213
    }
1214
1215
    /**
1216
     * @param \modResource $resource
1217
     * @param array $tags
1218
     */
1219
    protected function setResourceTags(\modResource $resource, $tags = [])
1220
    {
1221
        $tagger_groups = [];
1222
1223
        $existing_tags = $this->getResourceTags();
1224
        foreach ($existing_tags as $tag_group_alias => $data) {
1225
1226
            $tagger_groups[$tag_group_alias] = $data['columns']['id'];
1227
1228
            $remove = false;
1229
            if (!isset($tags[$tag_group_alias])) {
1230
                // remove all related group tags as
1231
                $remove = true;
1232
            }
1233
            if (isset($data['tags'])) {
1234
                foreach ($data['tags'] as $tag_alias => $tag) {
1235
                    $not_current = false;
1236
                    if (!isset($tags[$tag_group_alias]) || !isset($tags[$tag_group_alias]['tags']) || !isset($tags[$tag_group_alias]['tags'][$tag_alias])) {
1237
                        $not_current = true;
1238
                    } else {
1239
                        // it already exists so remove from the save list
1240
                        unset($tags[$tag_group_alias]['tags'][$tag_alias]);
1241
                    }
1242
                    if ($remove || $not_current) {
1243
                        $resourceTag = $this->modx->getObject('TaggerTagResource', ['tag' => $tag['id'], 'resource' => $resource->get('id')]);
1244
                        if ($resourceTag) {
1245
                            $resourceTag->remove();
1246
                        }
1247
                    }
1248
                }
1249
            }
1250
        }
1251
1252
        // now save any remaining tags/groups
1253
        foreach ($tags as $tag_group_alias => $data) {
1254
            // does the tagger group exist?
1255
            if (!$tagger_groups[$tag_group_alias]) {
1256
                $taggerGroup = $this->modx->getObject('TaggerGroup', ['alias' => $tag_group_alias]);
1257
                if (!$taggerGroup) {
1258
                    $taggerGroup = $this->modx->newObject('TaggerGroup');
1259
                    unset($data['columns']['id']);
1260
                    $taggerGroup->fromArray($data['columns']);
1261
                    // @TODO show for templates column
1262
                    $taggerGroup->save();
1263
                }
1264
                $tagger_groups[$tag_group_alias] = $taggerGroup->get('id');
1265
            }
1266
1267
1268
            if (isset($data['tags'])) {
1269
                foreach ($data['tags'] as $tag_alias => $tag) {
1270
                    // does the tag exist?
1271
                    $taggerTag = $this->modx->getObject('TaggerTag', ['alias' => $tag_alias]);
1272
                    if (!$taggerTag) {
1273
                        $taggerTag = $this->modx->newObject('TaggerTag');
1274
                        unset($tag['id']);
1275
                        $taggerTag->fromArray($tag);
1276
                        $taggerTag->save();
1277
                    }
1278
1279
                    $resourceTag = $this->modx->newObject('TaggerTagResource');
1280
                    $resourceTag->set('tag', $taggerTag->get('id'));
1281
                    $resourceTag->set('resource', $resource->get('id'));
1282
                    $resourceTag->save();
1283
                }
1284
            }
1285
        }
1286
1287
    }
1288
}
1289