| 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)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 (!Config::forClass($fetcherService)->get('apiKey') && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             !Config::forClass(Fetcher::class)->get('apiKey')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ImportTask::echo('No api key found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         if (!Config::forClass($fetcherService)->get('channel')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ImportTask::echo('No channel found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (!Config::forClass($mapperService)->get('mapping')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ImportTask::echo('No mappings found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $fetcher = Injector::inst()->createWithArgs($fetcherService, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'importerKey' => $this->getImporterKey(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $fetcher->startExportRun(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         ImportTask::echo('Started Salsify export.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $fetcher->waitForExportRunToComplete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         ImportTask::echo('Salsify export complete'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         ImportTask::echo('Staring data import'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         ImportTask::echo('-------------------'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $mapper = Injector::inst()->createWithArgs($mapperService, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'importerKey' => $this->getImporterKey(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'file' => $fetcher->getExportUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $mapper->map(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  |