Passed
Push — master ( 606bf7...dde9c0 )
by Matthew
01:54
created

Importer::createServices()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
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 Dynamic\Salsify\Traits\InstanceCreator;
7
use InvalidArgumentException;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Extensible;
11
use SilverStripe\Core\Injector\Injectable;
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
    use InstanceCreator;
0 ignored issues
show
Bug introduced by
The trait Dynamic\Salsify\Traits\InstanceCreator requires the property $noChannel which is not provided by Dynamic\Salsify\Model\Importer.
Loading history...
23
24
    /**
25
     * @var string
26
     */
27
    protected $importerKey;
28
29
    /**
30
     * @var
31
     */
32
    protected $file;
33
34
    /**
35
     * Importer constructor.
36
     * @param string $importerKey
37
     */
38
    public function __construct($importerKey)
39
    {
40
        if ($importerKey) {
41
            $this->setImporterKey($importerKey);
42
        }
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getImporterKey()
49
    {
50
        return $this->importerKey;
51
    }
52
53
    /**
54
     * @param string $importerKey
55
     * @return $this
56
     */
57
    public function setImporterKey($importerKey)
58
    {
59
        if (!is_string($importerKey)) {
0 ignored issues
show
introduced by
The condition is_string($importerKey) is always true.
Loading history...
60
            throw new InvalidArgumentException(sprintf(
61
                '%s importerKey must be a string',
62
                __CLASS__
63
            ));
64
        }
65
        if (empty($importerKey)) {
66
            throw new InvalidArgumentException(sprintf(
67
                '%s importerKey must cannot be empty',
68
                __CLASS__
69
            ));
70
        }
71
        if (preg_match('/[^A-Za-z0-9_-]/', $importerKey)) {
72
            throw new InvalidArgumentException(sprintf(
73
                '%s importerKey may only contain alphanumeric characters, dashes, and underscores',
74
                __CLASS__
75
            ));
76
        }
77
        $this->importerKey = $importerKey;
78
        return $this;
79
    }
80
81
    /**
82
     * @throws \Exception
83
     */
84
    public function run()
85
    {
86
        $this->createServices();
87
88
        /** @var string|Configurable $mapperService */
89
        $fetcherService = Fetcher::class . '.' . $this->getImporterKey();
90
        /** @var string|Configurable $mapperService */
91
        $mapperService = Mapper::class . '.' . $this->getImporterKey();
92
93
        ImportTask::echo('-------------------');
94
        ImportTask::echo('Now running import for ' . $this->getImporterKey());
95
96
        if (!Config::forClass($fetcherService)->get('apiKey') &&
97
            !Config::forClass(Fetcher::class)->get('apiKey')) {
98
            ImportTask::echo('No api key found');
99
            return;
100
        }
101
102
        if (!Config::forClass($fetcherService)->get('channel')) {
103
            ImportTask::echo('No channel found');
104
            return;
105
        }
106
107
        if (!Config::forClass($mapperService)->get('mapping')) {
108
            ImportTask::echo('No mappings found');
109
            return;
110
        }
111
112
        $fetcher = $this->getFetcher();
113
114
        $fetcher->startExportRun();
115
        ImportTask::echo('Started Salsify export.');
116
        $fetcher->waitForExportRunToComplete();
117
        ImportTask::echo('Salsify export complete');
118
119
        $this->file = $fetcher->getExportUrl();
120
121
        ImportTask::echo('Staring data import');
122
        ImportTask::echo('-------------------');
123
        $this->getMapper()->map();
124
    }
125
}
126