summaryrefslogtreecommitdiffstats
path: root/src/Project.php
blob: 1336163229404f963607a3c5adf709e80e1f3a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace Drupal\ml_engine;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

class Project {

  private $credential_required_keys;

  public function __construct() {
    $this->credential_required_keys = ['type', 'project_id', 'private_key_id', 'private_key', 'client_email', 'client_id', 'auth_uri', 'token_uri', 'auth_provider_x509_cert_url', 'client_x509_cert_url'];
  }

  public static function create(ContainerInterface $container) {
    return new static();
  }

  public function get_project(){
    return \Drupal::configFactory()->getEditable('ml_engine.test.project');
  }

  public function get_credential(){
    return json_decode($this->get_project()->get('credential'),true);
  }

  public function get_name(){
    return $this->get_project()->get('name');
  }

  public function get_bucket(){
    return $this->get_project()->get('bucket');
  }

  public function get_bucket_repo(){
    return 'drupal-ml-repo';
  }

  public function verify_credential($para){

    $key_difference = array_diff($this->credential_required_keys, array_keys($para['credential']));

    if ($key_difference){
      $message = t("Credential Keys [ '@keys' ] are missing",array('@keys'=>join("', '",$key_difference)));
      return array(
        'success' => 0,
        'response'=>array('message'=>$message),
      );
    }

    $service = \Drupal::service('ml_engine.cloud_service')->create_service();

    try{ 
        $response = $service->projects->getConfig($para['name']);
        return array('success' => 1,'response' =>$response);
    } catch (\DomainException $ex){
        $error = array('message' => $ex->getMessage());
        return array('success' => 0,'response'=> array('message'=>"Please check the private key"));
    } catch (\Google_Service_Exception $ex){
        $error = json_decode($ex->getMessage(), true)['error'];
        return array( "success" => 0, "response" => $error);
    }

  }

}