dynamic /
silverstripe-salsify
| 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
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
|
|||
| 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 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 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::output('-------------------'); |
||
| 95 | ImportTask::output('Now running import for ' . $this->getImporterKey()); |
||
| 96 | |||
| 97 | if ( |
||
| 98 | !Config::forClass($fetcherService)->get('apiKey') && |
||
| 99 | !Config::forClass(Fetcher::class)->get('apiKey') |
||
| 100 | ) { |
||
| 101 | ImportTask::output('No api key found'); |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!Config::forClass($fetcherService)->get('channel')) { |
||
| 106 | ImportTask::output('No channel found'); |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!Config::forClass($mapperService)->get('mapping')) { |
||
| 111 | ImportTask::output('No mappings found'); |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | $fetcher = $this->getFetcher(); |
||
| 116 | |||
| 117 | $fetcher->startExportRun(); |
||
| 118 | ImportTask::output('Started Salsify export.'); |
||
| 119 | $fetcher->waitForExportRunToComplete(); |
||
| 120 | ImportTask::output('Salsify export complete'); |
||
| 121 | |||
| 122 | $this->file = $fetcher->getExportUrl(); |
||
| 123 | |||
| 124 | ImportTask::output('Staring data import'); |
||
| 125 | ImportTask::output('-------------------'); |
||
| 126 | |||
| 127 | $this->changeToSalsifyUser(); |
||
| 128 | $this->getMapper()->map(); |
||
| 129 | $this->changeToPreviousUser(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |