Passed
Pull Request — master (#13)
by Matthew
01:38
created

Importer::createServices()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 19
rs 9.9332
1
<?php
2
3
namespace Dynamic\Salsify\Model;
4
5
use Dynamic\Salsify\Task\ImportTask;
6
use InvalidArgumentException;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Extensible;
10
use SilverStripe\Core\Injector\Injectable;
11
use SilverStripe\Core\Injector\Injector;
12
13
/**
14
 * Class Importer
15
 * @package Dynamic\Salsify\Model
16
 */
17
class Importer
18
{
19
    use Injectable;
20
    use Extensible;
21
    use Configurable;
22
23
    /**
24
     * @var string
25
     */
26
    protected $importerKey;
27
28
    /**
29
     * @var \Dynamic\Salsify\Model\Fetcher
30
     */
31
    protected $fetcher;
32
33
    /**
34
     * Importer constructor.
35
     * @param string $importerKey
36
     * @param \Dynamic\Salsify\Model\Fetcher $fetcher
37
     */
38
    public function __construct($importerKey, $fetcher = null)
39
    {
40
        if ($importerKey) {
41
            $this->setImporterKey($importerKey);
42
        }
43
44
        $this->fetcher = $fetcher;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getImporterKey()
51
    {
52
        return $this->importerKey;
53
    }
54
55
    /**
56
     * @param string $importerKey
57
     * @return $this
58
     */
59
    public function setImporterKey($importerKey)
60
    {
61
        if (!is_string($importerKey)) {
0 ignored issues
show
introduced by
The condition is_string($importerKey) is always true.
Loading history...
62
            throw new InvalidArgumentException(sprintf(
63
                '%s importerKey must be a string',
64
                __CLASS__
65
            ));
66
        }
67
        if (empty($importerKey)) {
68
            throw new InvalidArgumentException(sprintf(
69
                '%s importerKey must cannot be empty',
70
                __CLASS__
71
            ));
72
        }
73
        if (preg_match('/[^A-Za-z0-9_-]/', $importerKey)) {
74
            throw new InvalidArgumentException(sprintf(
75
                '%s importerKey may only contain alphanumeric characters, dashes, and underscores',
76
                __CLASS__
77
            ));
78
        }
79
        $this->importerKey = $importerKey;
80
        return $this;
81
    }
82
83
    public function createServices()
84
    {
85
        /** @var string|Configurable $mapperService */
86
        $fetcherService = Fetcher::class . '.' . $this->getImporterKey();
87
        /** @var string|Configurable $mapperService */
88
        $mapperService = Mapper::class . '.' . $this->getImporterKey();
89
90
        if (!Injector::inst()->has($fetcherService)) {
91
            Injector::inst()->load([
92
                $fetcherService => [
93
                    'class' => Fetcher::class,
94
                ],
95
            ]);
96
        }
97
98
        if (!Injector::inst()->has($mapperService)) {
99
            Injector::inst()->load([
100
                $mapperService => [
101
                    'class' => Mapper::class,
102
                ],
103
            ]);
104
        }
105
    }
106
107
    /**
108
     * @throws \Exception
109
     */
110
    public function run()
111
    {
112
        $this->createServices();
113
114
        /** @var string|Configurable $mapperService */
115
        $fetcherService = Fetcher::class . '.' . $this->getImporterKey();
116
        /** @var string|Configurable $mapperService */
117
        $mapperService = Mapper::class . '.' . $this->getImporterKey();
118
119
        ImportTask::echo('-------------------');
120
        ImportTask::echo('Now running import for ' . $this->getImporterKey());
121
122
        if (
123
            !Config::forClass($fetcherService)->get('apiKey') &&
124
            !Config::forClass(Fetcher::class)->get('apiKey')
125
        ) {
126
            ImportTask::echo('No api key found');
127
            return;
128
        }
129
130
        if (!Config::forClass($fetcherService)->get('channel')) {
131
            ImportTask::echo('No channel found');
132
            return;
133
        }
134
135
        if (!Config::forClass($mapperService)->get('mapping')) {
136
            ImportTask::echo('No mappings found');
137
            return;
138
        }
139
140
        $fetcher = Injector::inst()->createWithArgs($fetcherService, [
141
            'importerKey' => $this->getImporterKey(),
142
        ]);
143
144
        $fetcher->startExportRun();
145
        ImportTask::echo('Started Salsify export.');
146
        $fetcher->waitForExportRunToComplete();
147
        ImportTask::echo('Salsify export complete');
148
149
        ImportTask::echo('Staring data import');
150
        ImportTask::echo('-------------------');
151
        $mapper = Injector::inst()->createWithArgs($mapperService, [
152
            'importerKey' => $this->getImporterKey(),
153
            'file' => $fetcher->getExportUrl(),
154
        ]);
155
        $mapper->map();
156
    }
157
}
158