StaticSiteImporter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 22
dl 0
loc 77
rs 10
c 2
b 1
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A runOnImportEnd() 0 8 1
A runOnImportStart() 0 5 1
A getExternalType() 0 3 1
A __construct() 0 4 1
A runRewriteLinksTask() 0 17 5
1
<?php
2
3
namespace PhpTek\Exodus\Transform;
4
5
use ExternalContentImporter;
6
use PhpTek\Exodus\Transform\StaticSitePageTransformer;
7
use PhpTek\Exodus\Model\StaticSiteImportDataObject;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Control\HTTPRequest;
10
use PhpTek\Exodus\Task\StaticSiteRewriteLinksTask;
11
use SilverStripe\Core\Injector\Injector;
12
13
/**
14
 * Physically brings content into SilverStripe as defined by URLs fetched
15
 * at the crawl stage, and utilises {@link StaticSitePageTransformer} and {@link StaticSiteFileTransformer}.
16
 *
17
 * @package phptek/silverstripe-exodus
18
 * @author Sam Minee <[email protected]>
19
 * @author Russell Michell <[email protected]>
20
 * @see {@link ExternalContentImporter}
21
 * @see {@link StaticSiteImportDataObject}
22
 */
23
class StaticSiteImporter extends ExternalContentImporter
24
{
25
    /**
26
     *
27
     * @return void
28
     */
29
    public function __construct()
30
    {
31
        $this->contentTransforms['sitetree'] = StaticSitePageTransformer::create();
32
        $this->contentTransforms['file'] = StaticSiteFileTransformer::create();
33
    }
34
35
    /**
36
     *
37
     * @param $item
38
     * @return string
39
     * @todo `$item` param seems to be instance of `ExternalContentSource` not `ExternalContentItem` for some reason
40
     */
41
    public function getExternalType($item)
42
    {
43
        return $item->getType();
44
    }
45
46
    /**
47
     * Run prior to the entire import process starting.
48
     *
49
     * Creates an import DataObject record for hooking-into later with the link-processing logic.
50
     *
51
     * @return void
52
     */
53
    public function runOnImportStart(): void
54
    {
55
        parent::runOnImportStart();
56
57
        StaticSiteImportDataObject::create()->start();
58
    }
59
60
    /**
61
     * Run right when the import process ends.
62
     *
63
     * @return void
64
     */
65
    public function runOnImportEnd()
66
    {
67
        parent::runOnImportEnd();
68
        $current = StaticSiteImportDataObject::current();
69
        $current->end();
70
71
        $importID = $current->ID;
72
        $this->runRewriteLinksTask($importID);
73
    }
74
75
    /**
76
     *
77
     * @param number $importID
78
     * @return void
79
     * @todo How to interject with external-content's "Import Complete" message to only show when
80
     * this method has completed?
81
     * @todo Use the returned task output, and display on-screen deploynaut style
82
     */
83
    protected function runRewriteLinksTask($importID)
84
    {
85
        $params = Controller::curr()->getRequest()->postVars();
86
        $sourceID = !empty($params['ID']) ? $params['ID'] : 0;
87
        $autoRun = !empty($params['AutoRunTask']) ? $params['AutoRunTask'] : null;
88
89
        if ($sourceID && $autoRun) {
90
            $getVars = [
91
                'SourceID' => $sourceID,
92
                'ImportID' => $importID,
93
                'SilentRun' => 1
94
            ];
95
96
            // Skip TaskRunner. Too few docs available on its use
97
            $request = new HTTPRequest('GET', '/dev/tasks/StaticSiteRewriteLinksTask', $getVars);
98
            $inst = Injector::inst()->create(StaticSiteRewriteLinksTask::class);
99
            $inst->run($request);
100
        }
101
    }
102
}
103