/**
  * Sets the categories. Non-existing categories will be created immediately.
  */
 protected function setCategories()
 {
     // No categories
     if (is_null($this->ItemBase->Categories)) {
         return;
     }
     if (is_null(self::$CategoryApi)) {
         self::$CategoryApi = Shopware\Components\Api\Manager::getResource('Category');
     }
     if (is_null(self::$CategoryRepository)) {
         self::$CategoryRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category');
     }
     // Categories
     /** @var PlentySoapObject_ItemCategory $Category */
     foreach ($this->ItemBase->Categories->item as $Category) {
         // FIX: corrupt category within plenty
         if ((int) $Category->ItemCategoryID <= 0 || empty($Category->ItemCategoryPathNames)) {
             continue;
         }
         try {
             $categoryId = PlentymarketsMappingEntityCategory::getCategoryByPlentyID($Category->ItemCategoryID, $this->storeId);
             $this->categories[] = array('id' => $categoryId);
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             $importEntityItemCategoryTree = new PlentymarketsImportEntityItemCategoryTree($Category, $this->storeId);
             $categoryId = $importEntityItemCategoryTree->import();
             // Only create a mapping and connect the category to the item,
             // of nothing went wrong during creation
             if ($categoryId) {
                 $this->categories[] = array('id' => $categoryId);
             }
         }
     }
 }
    /**
     * Performs the actual import
     *
     * @throws PlentymarketsExportException
     */
    public function run()
    {
        // Get the data from plentymarkets (for every mapped shop)
        $shopIds = Shopware()->Db()->fetchAll('
			SELECT * FROM plenty_mapping_shop
		');
        foreach ($shopIds as $shopId) {
            $mainShopId = PlentymarketsMappingController::getShopByPlentyID($shopId['plentyID']);
            $mainLang = array_values(PlentymarketsTranslation::getShopMainLanguage($mainShopId));
            $Request_GetItemCategoryTree = new PlentySoapRequest_GetItemCategoryTree();
            $Request_GetItemCategoryTree->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($mainLang[0]['locale']);
            $Request_GetItemCategoryTree->GetCategoryNames = true;
            $Request_GetItemCategoryTree->StoreID = $shopId['plentyID'];
            $Request_GetItemCategoryTree->GetAktivCategories = true;
            /** @var PlentySoapResponse_GetItemCategoryTree $Response_GetItemCategoryTree */
            $Response_GetItemCategoryTree = PlentymarketsSoapClient::getInstance()->GetItemCategoryTree($Request_GetItemCategoryTree);
            if (!$Response_GetItemCategoryTree->Success) {
                Shopware()->Db()->query('INSERT plenty_mapping_category SELECT * FROM plenty_mapping_category_old');
                throw new PlentymarketsImportException('The item category tree could not be retrieved', 2920);
            }
            /** @var PlentySoapObject_ItemCategoryTreeNode $Category */
            foreach ($Response_GetItemCategoryTree->MultishopTree->item[0]->CategoryTree->item as $Category) {
                $importEntityItemCategoryTree = new PlentymarketsImportEntityItemCategoryTree($Category, $shopId['plentyID'], $Request_GetItemCategoryTree->Lang);
                $importEntityItemCategoryTree->import();
            }
        }
        $this->rebuild();
    }