public function loadData($pid, $name, $tmpDir)
 {
     // Upload to WebDav
     $webDav = new WebDav($this->gdClient->getUsername(), $this->gdClient->getPassword(), $this->gdClient->getUserUploadUrl());
     $dimensionName = Identifiers::getIdentifier($name);
     $tmpFolderNameDimension = "{$pid}-{$dimensionName}-" . uniqid();
     $tmpFolderDimension = $tmpDir . '/' . Identifiers::getIdentifier($name);
     mkdir($tmpFolderDimension);
     $timeDimensionManifest = self::getDataLoadManifest($dimensionName);
     file_put_contents("{$tmpFolderDimension}/upload_info.json", $timeDimensionManifest);
     copy(__DIR__ . '/time-dimension.csv', "{$tmpFolderDimension}/{$dimensionName}.csv");
     $webDav->createFolder($tmpFolderNameDimension);
     $webDav->upload("{$tmpFolderDimension}/upload_info.json", $tmpFolderNameDimension);
     $webDav->upload("{$tmpFolderDimension}/{$dimensionName}.csv", $tmpFolderNameDimension);
     // Run ETL task of time dimensions
     try {
         $this->gdClient->getDatasets()->loadData($pid, $tmpFolderNameDimension);
     } catch (Exception $e) {
         $debugFile = "{$tmpFolderDimension}/{$pid}-etl.log";
         $logSaved = $webDav->saveLogs($tmpFolderDimension, $debugFile);
         if ($logSaved) {
             $data = file_get_contents($debugFile);
             if ($data) {
                 $data = json_decode($data, true);
                 if ($data) {
                     $e = new Exception(json_decode(file_get_contents($debugFile), true), $e->getCode(), $e);
                 }
             }
         }
         throw $e;
     }
 }
 public function create($pid, $name, $identifier = null, $template = null)
 {
     if (!$identifier) {
         $identifier = Identifiers::getIdentifier($name);
         if (!$identifier) {
             throw new Exception("Identifier derived from dimension name '{$name}' is not valid. " . "Choose other name or custom identifier.");
         }
     }
     $call = $this->client->get("/gdc/md/{$pid}/data/sets");
     $existingDataSets = [];
     foreach ($call['dataSetsInfo']['sets'] as $r) {
         $existingDataSets[] = $r['meta']['identifier'];
     }
     if (!in_array(Identifiers::getDateDimensionId($name, $template), $existingDataSets)) {
         $this->client->getDatasets()->executeMaql($pid, sprintf('INCLUDE TEMPLATE "%s" MODIFY (IDENTIFIER "%s", TITLE "%s");', self::getTemplateUrn($template), $identifier, $name));
     }
 }
 public function dropDataSet($pid, $dataSetName)
 {
     $dataSetId = Identifiers::getIdentifier($dataSetName);
     $model = $this->view($pid);
     if (isset($model['projectModel']['datasets'])) {
         foreach ($model['projectModel']['datasets'] as $i => $dataSet) {
             if ($dataSet['dataset']['title'] == $dataSetName) {
                 unset($model['projectModel']['datasets'][$i]);
                 break;
             }
         }
         $model['projectModel']['datasets'] = array_values($model['projectModel']['datasets']);
     }
     $update = $this->diff($pid, $model);
     if (count($update['moreDestructiveMaql'])) {
         foreach ($update['moreDestructiveMaql'] as $m) {
             $this->client->getDatasets()->executeMaql($pid, $m);
         }
         $this->client->getDatasets()->executeMaql($pid, sprintf('DROP IF EXISTS {dim.%s};', $dataSetId));
         $this->client->getDatasets()->executeMaql($pid, sprintf('DROP IF EXISTS {ffld.%s};', $dataSetId));
         return $update['description'];
     }
     return false;
 }