forked from IBM-Cloud/get-started-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudant.php
More file actions
144 lines (131 loc) · 3.84 KB
/
Cloudant.php
File metadata and controls
144 lines (131 loc) · 3.84 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* Copyright IBM Corp. 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once('./sag/Sag.php');
/**
* Class to handle performing basic CRUD operations on a Couch DB.
* This class uses the Sag library to talk to the Couch DB.
*/
final class Cloudant {
private static $inst = null;
private $sag;
private $db_exists = false;
public static function Instance() {
if (self::$inst === null) {
self::$inst = new Cloudant();
}
return self::$inst;
}
public function isConnected() {
return $this->db_exists;
}
private function __construct() {
#If running locally enter your own host, port, username and password
$host = getenv('CLOUDANT_HOST');
$port = '443';
$username = getenv('CLOUDANT_USERNAME');
$password = getenv('CLOUDANT_PASSWORD');
if($vcapStr = getenv('VCAP_SERVICES')) {
$vcap = json_decode($vcapStr, true);
foreach ($vcap as $serviceTypes) {
foreach ($serviceTypes as $service) {
if($service['label'] == 'cloudantNoSQLDB') {
$credentials = $service['credentials'];
$username = $credentials['username'];
$password = $credentials['password'];
$parsedUrl = parse_url($credentials['url']);
$host = $parsedUrl['host'];
$port = isset($parsedUrl['port']) ?
$parsedUrl['port'] : $parsedUrl['scheme'] == 'http' ?
'80' : '443';
break;
}
}
}
}
$this->sag = new Sag($host, $port);
$this->sag->useSSL(true);
$dbsession = $this->sag->login($username, $password);
try {
$this->sag->setDatabase('mydb', true);
$this->createView();
$this->db_exists = true;
} catch (Exception $e) {
$this->db_exists = false;
}
}
/**
* Transforms the Visitor JSON from the DB to the JSON
* the client will expect.
*/
private function toClientVisitor($couchVisitor) {
$clientVisitor = array('id' => $couchVisitor->id);
$clientVisitor['name'] = $couchVisitor->value->name;
return $clientVisitor;
}
/**
* Creates a view to use in the DB if one does not already exist.
*/
private function createView() {
try {
$view = $this->sag->get('_design/visitors');
} catch(SagCouchException $e) {
$allvisitors = array('reduce' => '_count',
'map' => 'function(doc){if(doc.name != null){emit(doc.order,{name: doc.name})}}');
$views = array('allvisitors' => $allvisitors);
$designDoc = array('views' => $views);
$this->sag->put('_design/visitors', $designDoc);
}
}
/**
* Gets all visitors from the DB.
*/
public function get() {
$visitors = array();
$docs = $this->sag->get('_design/visitors/_view/allvisitors?reduce=false')->body;
foreach ($docs->rows as $row) {
$visitors[] = $row->value->name;;
}
return $visitors;
}
/**
* Creates a new Visitor in the DB.
*/
public function post($visitor) {
$resp = $this->sag->post($visitor);
$visitor['id'] = $resp->body->id;
return $visitor;
}
/**
* Updates a Visitor in the DB.
*/
public function put($id, $visitor) {
$couchTodo = $this->sag->get($id)->body;
$couchTodo->name = $visitor['name'];
$this->sag->put($id, $couchTodo);
$couchTodo->id = $id;
unset($couchTodo->_id);
unset($couchTodo->_rev);
return $couchTodo;
}
/**
* Deletes a Visitor from the DB.
*/
public function delete($id) {
$rev = $this->sag->get($id)->body->_rev;
$this->sag->delete($id, $rev);
}
}