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

ScheduledExternalImportJob   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 15
eloc 36
c 2
b 1
f 1
dl 0
loc 72
rs 10
1
<?php
2
3
use SilverStripe\Core\Injector\Injectable;
4
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
5
use Symbiote\QueuedJobs\Services\QueuedJobService;
6
7
/**
8
 * @author [email protected]
9
 * @license BSD License http://silverstripe.org/bsd-license/
10
 */
11
class ScheduledExternalImportJob extends AbstractQueuedJob
12
{
13
    use Injectable;
14
15
    public const MIN_REPEAT = 300;
16
17
    public function __construct($repeat = null, $contentItem = null, $target = null, $includeParent = false, $includeChildren = true, $targetType = null, $duplicateStrategy='Overwrite', $params = array())
18
    {
19
        if ($contentItem) {
20
            $this->sourceObjectID = $contentItem->ID;
21
            $this->setObject($target);
22
23
            $this->includeParent = $includeParent;
24
            $this->includeChildren = $includeChildren;
25
            $this->duplicateStrategy = $duplicateStrategy;
26
            $this->targetType = $targetType;
27
28
            $this->params = $params;
29
            $repeat = (int) $repeat;
30
            if ($repeat > 0) {
31
                $this->repeat = $repeat < self::MIN_REPEAT ? self::MIN_REPEAT : $repeat;
32
            } else {
33
                $this->repeat = 0;
34
            }
35
36
            $this->totalSteps = 1;
37
        }
38
    }
39
40
    protected $source;
41
42
    public function getSource()
43
    {
44
        if ($this->sourceObjectID && !$this->source) {
45
            $this->source = ExternalContent::getDataObjectFor($this->sourceObjectID);
46
        }
47
        return $this->source;
48
    }
49
50
    public function getTitle()
51
    {
52
        return "Scheduled import from " . $this->getSource()->Title;
53
    }
54
55
    public function getSignature()
56
    {
57
        return parent::getSignature();
58
    }
59
60
    public function process()
61
    {
62
        $source = $this->getSource();
63
        $target = $this->getObject();
64
65
        if ($source && $target) {
66
            $externalSource = $source instanceof ExternalContentItem ? $source->getSource() : $source;
67
68
            $importer = null;
69
            $importer = $externalSource->getContentImporter($this->targetType);
70
71
            if ($importer) {
72
                $importer->import($source, $target, $this->includeParent, $this->includeChildren, $this->duplicateStrategy, $this->params);
73
            }
74
75
            if ($this->repeat) {
76
                $job = ScheduledExternalImportJob::create($this->repeat, $source, $target, $this->includeParent, $this->includeChildren, $this->targetType, $this->duplicateStrategy, $this->params);
77
                singleton(QueuedJobService::class)->queueJob($job, date('Y-m-d H:i:s', time() + $this->repeat));
78
            }
79
        }
80
81
        $this->currentStep++;
82
        $this->isComplete = true;
83
    }
84
}
85