/**
  * I am a Singleton
  *
  * @return PlentymarketsImportStackItem
  */
 public static function getInstance()
 {
     if (!self::$Instance instanceof self) {
         self::$Instance = new self();
     }
     return self::$Instance;
 }
 /**
  * Reads the items of plentymarkets that have changed
  */
 public function run()
 {
     // Number of items
     $chunkSize = PlentymarketsConfig::getInstance()->getImportItemChunkSize(self::DEFAULT_CHUNK_SIZE);
     // get the chunk out of the stack
     $chunk = PlentymarketsImportStackItem::getInstance()->getChunk($chunkSize);
     // Import each item
     try {
         while (($item = array_shift($chunk)) && is_array($item)) {
             // for each assigned store
             $storeIds = explode('|', $item['storeIds']);
             foreach ($storeIds as $storeId) {
                 // Import the item
                 $this->importItem($item['itemId'], $storeId);
             }
         }
     } catch (PlentymarketsImportException $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item', $E->getMessage(), $E->getCode());
         // return to the stack
         foreach ($chunk as $item) {
             // for each assigned store
             $storeIds = explode('|', $item['storeIds']);
             foreach ($storeIds as $storeId) {
                 // Import the item
                 PlentymarketsImportStackItem::getInstance()->addItem($item['itemId'], $storeId);
             }
         }
         PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', 'Returned ' . count($chunk) . ' items to the stack');
     }
     $numberOfItems = count($this->itemIdsDone);
     // Log
     if ($numberOfItems == 0) {
         PlentymarketsLogger::getInstance()->message('Sync:Item', 'No item has been updated or created.');
     } else {
         if ($numberOfItems == 1) {
             PlentymarketsLogger::getInstance()->message('Sync:Item', '1 item has been updated or created.');
         } else {
             PlentymarketsLogger::getInstance()->message('Sync:Item', $numberOfItems . ' items have been updated or created.');
         }
     }
     // Log stack information
     $stackSize = count(PlentymarketsImportStackItem::getInstance());
     if ($stackSize == 1) {
         PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', '1 item left in the stack');
     } else {
         if ($stackSize > 1) {
             PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', $stackSize . ' items left in the stack');
         } else {
             PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', 'The stack is empty');
         }
     }
     // Post processed
     $this->finish();
 }
 /**
  * Runs the item import stack cronjob.
  *
  * @param Shopware_Components_Cron_CronJob $Job
  */
 public function runItemImportStackUpdate(Shopware_Components_Cron_CronJob $Job)
 {
     $this->Config->setImportItemStackLastRunTimestamp(time());
     $this->Config->setImportItemStackNextRunTimestamp(time() + $Job->getJob()->getInterval());
     if (!$this->Status->maySynchronize()) {
         $this->Config->setImportItemStackStatus(0);
         return;
     }
     try {
         PlentymarketsImportStackItem::getInstance()->update();
         $this->Config->setImportItemStackStatus(1);
         $this->Config->eraseImportItemStackError();
     } catch (Exception $E) {
         $this->Config->setImportItemStackStatus(2);
         $this->Config->setImportItemStackError($E->getMessage());
     }
 }