/**
  * @param $data
  * @return mixed|ProfileEntity
  * @throws \Enlight_Exception
  * @throws \Exception
  */
 public function createProfileModel($data)
 {
     $event = Shopware()->Events()->notifyUntil('Shopware_Components_SwagImportExport_Factories_CreateProfileModel', ['subject' => $this, 'data' => $data]);
     if ($event && $event instanceof \Enlight_Event_EventArgs && $event->getReturn() instanceof ProfileEntity) {
         return $event->getReturn();
     }
     if (!isset($data['name'])) {
         throw new \Exception('Profile name is required');
     }
     if (!isset($data['type'])) {
         throw new \Exception('Profile type is required');
     }
     if (isset($data['hidden']) && $data['hidden']) {
         $tree = TreeHelper::getTreeByHiddenProfileType($data['type']);
     } else {
         if (isset($data['baseProfile'])) {
             $tree = TreeHelper::getDefaultTreeByBaseProfile($data['baseProfile']);
         } else {
             $tree = TreeHelper::getDefaultTreeByProfileType($data['type']);
         }
     }
     $profileEntity = new ProfileEntity();
     $profileEntity->setName($data['name']);
     $profileEntity->setBaseProfile($data['baseProfile']);
     $profileEntity->setType($data['type']);
     $profileEntity->setTree($tree);
     if (isset($data['hidden'])) {
         $profileEntity->setHidden($data['hidden']);
     }
     $this->modelManager->persist($profileEntity);
     $this->modelManager->flush();
     return $profileEntity;
 }
 /**
  * @inheritdoc
  */
 public function importProfile(UploadedFile $file)
 {
     /** @var \Shopware_Components_Plugin_Namespace $namespace */
     $namespace = $this->snippetManager->getNamespace('backend/swag_import_export/controller');
     if (strtolower($file->getClientOriginalExtension()) !== 'json') {
         $this->fileSystem->remove($file->getPathname());
         throw new \Exception($namespace->get('swag_import_export/profile/profile_import_no_json_error'));
     }
     $content = file_get_contents($file->getPathname());
     if (empty($content)) {
         $this->fileSystem->remove($file->getPathname());
         throw new \Exception($namespace->get('swag_import_export/profile/profile_import_no_data_error'));
     }
     $profileData = (array) json_decode($content);
     if (empty($profileData['name']) || empty($profileData['type']) || empty($profileData['tree'])) {
         $this->fileSystem->remove($file->getPathname());
         throw new \Exception($namespace->get('swag_import_export/profile/profile_import_no_valid_data_error'));
     }
     try {
         $profile = new Profile();
         $profile->setName($profileData['name']);
         $profile->setType($profileData['type']);
         $profile->setTree(json_encode($profileData['tree']));
         $this->modelManager->persist($profile);
         $this->modelManager->flush($profile);
         $this->fileSystem->remove($file->getPathname());
     } catch (\Exception $e) {
         $this->fileSystem->remove($file->getPathname());
         $message = $e->getMessage();
         $msg = $namespace->get('swag_import_export/profile/profile_import_error');
         if (strpbrk('Duplicate entry', $message) !== false) {
             $msg = $namespace->get('swag_import_export/profile/profile_import_duplicate_error');
         }
         throw new \Exception($msg);
     }
 }
 /**
  * @param string $profileType
  * @param string $profileName
  */
 private function createProfile($profileType, $profileName)
 {
     $defaultTree = TreeHelper::getDefaultTreeByProfileType($profileType);
     $profile = new Profile();
     $profile->setHidden(0);
     $profile->setName($profileName);
     $profile->setType($profileType);
     $profile->setTree($defaultTree);
     $this->modelManager->persist($profile);
     $this->modelManager->flush();
     $this->profileIds[$profileType] = $profile->getId();
 }
 /**
  * @return Enlight_View|Enlight_View_Default
  * @throws Exception
  */
 public function duplicateProfileAction()
 {
     $manager = $this->getModelManager();
     $snippetManager = $this->get('snippets');
     $profileId = (int) $this->Request()->getParam('profileId');
     $loadedProfile = $manager->find(Profile::class, $profileId);
     if (!$loadedProfile) {
         throw new \Exception(sprintf('Profile with id %s does NOT exist', $profileId));
     }
     $profile = new Profile();
     $profile->setBaseProfile($loadedProfile->getId());
     $profile->setName($loadedProfile->getName() . ' (' . $snippetManager->getNamespace('backend/swag_import_export/controller')->get('swag_import_export/profile/copy') . ')');
     $profile->setType($loadedProfile->getType());
     $profile->setTree($loadedProfile->getTree());
     $manager->persist($profile);
     try {
         $manager->flush();
     } catch (DBALException $e) {
         return $this->View()->assign(['success' => false, 'message' => $snippetManager->getNamespace('backend/swag_import_export/controller')->get('swag_import_export/profile/duplicate_unique_error_msg')]);
     } catch (\Exception $e) {
         return $this->View()->assign(['success' => false, 'message' => $e->getMessage()]);
     }
     return $this->View()->assign(['success' => true, 'data' => ['id' => $profile->getId(), 'name' => $profile->getName(), 'type' => $profile->getType(), 'baseProfile' => $profile->getBaseProfile(), 'tree' => $profile->getTree(), 'default' => $profile->getDefault()]]);
 }