Test Failed
Push — master ( 357bf5...71cdad )
by Russell
03:53
created

ExternalContentImporter::importChildren()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 20
rs 8.4444
cc 8
nc 5
nop 4
1
<?php
2
3
use SilverStripe\Core\ClassInfo;
4
use SilverStripe\Core\Config\Configurable;
5
use SilverStripe\Core\Extensible;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\ArrayList;
8
use Symbiote\QueuedJobs\Jobs\AbstractQueuedJob;
9
10
/**
11
 * Implements ExternalContentImporter ?
12
 * @author Marcus Nyeholt <[email protected]>
13
 * @license BSD License http://silverstripe.org/bsd-license
14
 *
15
 */
16
abstract class ExternalContentImporter
17
{
18
    use Injectable;
19
    use Configurable;
20
    use Extensible;
21
22
    protected $contentTransforms = array();
23
    protected $params = array();
24
25
    private static $use_queue = true;
26
27
    /**
28
     * @return array
29
     */
30
    public function getParams()
31
    {
32
        return $this->params;
33
    }
34
35
    /**
36
     * Import from a content source to a particular target
37
     *
38
     * @param ExternalContentItem $contentItem
39
     * @param SiteTree $target
40
     * @param boolean $includeParent
41
     * 			Whether to include the selected item in the import or not
42
     * @param String $duplicateStrategy
43
     * 			How to handle duplication
44
     * @param array $params All parameters passed with the import request.
45
     */
46
    public function import($contentItem, $target, $includeParent = false, $includeChildren = true, $duplicateStrategy='Overwrite', $params = array())
47
    {
48
        $this->runOnImportStart();
49
        $this->params = $params;
50
51
        // if the queuedjobs module exists, use that
52
        $queuedVersion = 'Queued' . get_class($this);
53
54
        if (
55
            $this->config()->get('use_queue') &&
56
            ClassInfo::exists(AbstractQueuedJob::class) &&
57
            ClassInfo::exists($queuedVersion)) {
58
            $importer = new $queuedVersion(
59
                $contentItem,
60
                $target,
61
                $includeParent,
62
                $includeChildren,
63
                $duplicateStrategy,
64
                $params
65
            );
66
67
            $service = singleton(QueuedJobService::class);
68
            $service->queueJob($importer);
69
70
            return $importer;
71
        }
72
73
        $children = null;
74
75
        if ($includeParent) {
76
            // Get the children of a particular node
77
            $children = ArrayList::create();
78
            $children->push($contentItem);
79
        } else {
80
            $children = $contentItem->stageChildren();
81
        }
82
83
        $this->importChildren($children, $target, $includeChildren, $duplicateStrategy);
84
        $this->runOnImportEnd();
85
86
        return true;
87
    }
88
89
    /**
90
     * Execute the importing of several children
91
     *
92
     * @param SS_List $children
93
     * @param SiteTree $parent
94
     * @param string $includeChildren (numeric string..)
95
     * @param string $duplicateStrategy e.g. "skip"
96
     */
97
    protected function importChildren($children, $parent, $includeChildren, $duplicateStrategy)
98
    {
99
        if (!$children) {
100
            return;
101
        }
102
103
        // get the importer to use, import, then see if there's any
104
        foreach ($children as $child) {
105
            $pageType = $this->getExternalType($child);
106
107
            if (isset($this->contentTransforms[$pageType])) {
108
                $transformer = $this->contentTransforms[$pageType];
109
                $result = $transformer->transform($child, $parent, $duplicateStrategy);
110
111
                $this->extend('onAfterImport', $result);
112
113
                // if there's more, then transform them
114
                if ($includeChildren && $result && $result->children && count($result->children)) {
115
                    // import the children
116
                    $this->importChildren($result->children, $result->page, $includeChildren, $duplicateStrategy);
117
                }
118
            }
119
        }
120
    }
121
122
    /**
123
     * Get the type of the item as far as the remote system
124
     * is concerned. This should match up with what is defined
125
     * in the contentTransforms array
126
     *
127
     * @return string
128
     * 			The type of the ExternalContentItem
129
     */
130
    abstract protected function getExternalType($item);
131
132
    /**
133
     * Allow subclasses to run custom logic immediately prior to import start.
134
     * Not declared abstract so method can be optionally defined on subclasses.
135
     */
136
    public function runOnImportStart()
137
    {
138
    }
139
140
    /**
141
     * Allow subclasses to run custom logic immediately after to import end.
142
     * Not declared abstract so method can be optionally defined on subclasses.
143
     */
144
    public function runOnImportEnd()
145
    {
146
    }
147
}
148