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