Passed
Pull Request — master (#13)
by Matthew
03:21
created

Importer::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Dynamic\Salsify\Model;
4
5
use \InvalidArgumentException;
6
use Dynamic\Salsify\Task\ImportTask;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
11
/**
12
 * Class Importer
13
 * @package Dynamic\Salsify\Model
14
 */
15
class Importer
16
{
17
    use Injectable;
18
    use Extensible;
19
    use Configurable;
20
21
    /**
22
     * @var string
23
     */
24
    protected $importerKey;
25
26
    /**
27
     * Importer constructor.
28
     * @param string $importerKey
29
     */
30
    public function __construct($importerKey = null)
31
    {
32
        if ($importerKey) {
33
            $this->setImporterKey($importerKey);
34
        }
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getImporterKey()
41
    {
42
        return $this->importerKey;
43
    }
44
45
    /**
46
     * @param string $importerKey
47
     * @return $this
48
     */
49
    public function setImporterKey($importerKey)
50
    {
51
        if (!is_string($importerKey)) {
0 ignored issues
show
introduced by
The condition is_string($importerKey) is always true.
Loading history...
52
            throw new InvalidArgumentException(sprintf(
53
                '%s importerKey must be a string',
54
                __CLASS__
55
            ));
56
        }
57
        if (empty($importerKey)) {
58
            throw new InvalidArgumentException(sprintf(
59
                '%s importerKey must cannot be empty',
60
                __CLASS__
61
            ));
62
        }
63
        if (preg_match('/[^A-Za-z0-9_-]/', $importerKey)) {
64
            throw new InvalidArgumentException(sprintf(
65
                '%s importerKey may only contain alphanumeric characters, dashes, and underscores',
66
                __CLASS__
67
            ));
68
        }
69
        $this->importerKey = $importerKey;
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $class
75
     * @return mixed
76
     */
77
    public function getConfig($class)
78
    {
79
        return $class::config()->get($this->getImporterKey());
80
    }
81
82
    public function run()
83
    {
84
        ImportTask::echo('-------------------');
85
        ImportTask::echo('Now running import for ' . $this->getImporterKey());
86
        $mapperConfig = $this->getConfig(Mapper::class);
87
        if (!$mapperConfig || !$mapperConfig['mapping']) {
88
            ImportTask::echo('No mappings found');
89
            return;
90
        }
91
92
        $fetcher = new Fetcher($this->getConfig(Fetcher::class));
93
        $fetcher->startExportRun();
94
        ImportTask::echo('Started Salsify export.');
95
        $fetcher->waitForExportRunToComplete();
96
        ImportTask::echo('Salsify export complete');
97
98
        ImportTask::echo('Staring data import');
99
        ImportTask::echo('-------------------');
100
        $mapper = new Mapper($fetcher->getExportUrl(), $mapperConfig['mapping']);
101
        $mapper->map();
102
    }
103
}
104