ZendclouddocumentserviceDatabase::connect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Database;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
8
// |                                                                           |
9
// | For the full copyright and license information, please view the LICENSE   |
10
// | file that was distributed with this source code. You can also view the    |
11
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
12
// |   vi: set noexpandtab:                                                    |
13
// |   Local Variables:                                                        |
14
// |   indent-tabs-mode: t                                                     |
15
// |   End:                                                                    |
16
// +---------------------------------------------------------------------------+
17
use Agavi\Exception\DatabaseException;
18
use Agavi\Exception\InitializationException;
19
20
/**
21
 * AgaviZendclouddocumentserviceDatabase provides connectivity to databases
22
 * using Zend Framework's Zend_Cloud_DocumentService functionality.
23
 *
24
 * Parameters:
25
 *   'factory_class'    Name of the factory class to use; the default value is
26
 *                      "Zend_Cloud_DocumentService_Factory".
27
 *   'factory_options'  Array of options for Zend_Cloud_DocumentService_Factory,
28
 *                      must at least contain one sub-element with the key
29
 *                      Zend_Cloud_DocumentService_Factory::DOCUMENT_ADAPTER_KEY
30
 *                      (or just "document_adapter"). This class must either be
31
 *                      already loaded with include() or be autoloadable.
32
 *   'collection'       Optional name of a default collection; if this is set,
33
 *                      then convenience methods defined directly in this class
34
 *                      can be used to perform operations without having to
35
 *                      specify the name of the collection/domain every time.
36
 *
37
 * @package    agavi
38
 * @subpackage database
39
 *
40
 * @author     David Zülke <[email protected]>
41
 * @copyright  Authors
42
 * @copyright  The Agavi Project
43
 *
44
 * @since      1.0.5
45
 *
46
 * @version    $Id$
47
 */
48
class ZendclouddocumentserviceDatabase extends Database
49
{
50
    /**
51
     * Initialize this Database.
52
     *
53
     * @param      DatabaseManager $databaseManager The database manager of this instance.
54
     * @param      array           $parameters An assoc array of initialization params.
55
     *
56
     * @throws     InitializationException If an error occurs while
57
     *                                                 initializing this Database.
58
     *
59
     * @author     David Zülke <[email protected]>
60
     * @since      1.0.5
61
     */
62
    public function initialize(DatabaseManager $databaseManager, array $parameters = array())
63
    {
64
        parent::initialize($databaseManager, $parameters);
65
        
66
        if (!$this->hasParameter('factory_class')) {
67
            $this->setParameter('factory_class', 'Zend_Cloud_DocumentService_Factory');
68
        }
69
        
70
        if (!class_exists($this->getParameter('factory_class'))) {
71
            if (!class_exists('Zend_Loader')) {
72
                require('Zend/Loader.php');
73
            }
74
            Zend_Loader::loadClass($this->getParameter('factory_class'));
75
        }
76
        
77
        $factoryOptions = array();
78
        foreach ((array)$this->getParameter('factory_options', array()) as $name => $value) {
79
            // resolve constants like "Zend_Cloud_DocumentService_Factory::DOCUMENT_ADAPTER_KEY"
80
            if (strpos($name, '::') && defined($name)) {
81
                $name = constant($name);
82
            }
83
            
84
            $factoryOptions[$name] = $value;
85
        }
86
        
87
        $this->setParameter('factory_options', $factoryOptions);
88
    }
89
    
90
    /**
91
     * Connect to the database.
92
     * If a default "collection" is configured, this collection will be created.
93
     *
94
     * @throws     <b>AgaviDatabaseException</b> If a connection could not be
95
     *                                           created.
96
     *
97
     * @author     David Zülke <[email protected]>
98
     * @since      1.0.5
99
     */
100
    public function connect()
101
    {
102
        try {
103
            $this->connection = call_user_func(array($this->getParameter('factory_class'), 'getAdapter'), $this->getParameter('factory_options'));
104
        } catch (Zend_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Agavi\Database\Zend_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
105
            throw new DatabaseException(sprintf("Caught exception of type %s while creating adapter instance; details:\n\n%s", get_class($e), $e->getMessage()), 0, $e);
106
        }
107
    }
108
    
109
    /**
110
     * Retrieve the underlying implementation used by the adapter.
111
     *
112
     * @return     mixed The service implementation.
113
     *
114
     * @author     David Zülke <[email protected]>
115
     * @since      1.0.5
116
     */
117
    public function getResource()
118
    {
119
        return $this->getConnection()->getClient();
120
    }
121
    
122
    /**
123
     * Shut down this database connection.
124
     *
125
     * @author     David Zülke <[email protected]>
126
     * @since      1.0.5
127
     */
128
    public function shutdown()
129
    {
130
        $this->connection = $this->resource = null;
131
    }
132
    
133
    /**
134
     * List all documents in a collection.
135
     * This is a convenience function using the configured "collection" parameter.
136
     *
137
     * @see        Zend_Cloud_DocumentService_Adapter::listDocuments()
138
     *
139
     * @param      array $options An array of options.
140
     *
141
     * @return     Zend_Cloud_DocumentService_DocumentSet A list of documents.
142
     *
143
     * @author     David Zülke <[email protected]>
144
     * @since      1.0.5
145
     */
146 View Code Duplication
    public function listDocuments(array $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        if (!$this->hasParameter('collection')) {
149
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
150
        }
151
        
152
        return $this->getConnection()->listDocuments($this->getParameter('collection'), $options);
153
    }
154
    
155
    /**
156
     * Insert document.
157
     * This is a convenience function using the configured "collection" parameter.
158
     *
159
     * @see        Zend_Cloud_DocumentService_Adapter::insertDocument()
160
     *
161
     * @param      array|Zend_Cloud_DocumentService_Document $document Document to insert.
162
     * @param      array                                     $options  An array of options.
163
     *
164
     * @author     David Zülke <[email protected]>
165
     * @since      1.0.5
166
     */
167 View Code Duplication
    public function insertDocument($document, $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        if (!$this->hasParameter('collection')) {
170
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
171
        }
172
        
173
        return $this->getConnection()->insertDocument($this->getParameter('collection'), $document, $options);
174
    }
175
    
176
    /**
177
     * Replace document.
178
     * The new document replaces the existing document with the same ID.
179
     * This is a convenience function using the configured "collection" parameter.
180
     *
181
     * @see        Zend_Cloud_DocumentService_Adapter::replaceDocument()
182
     *
183
     * @param      array|Zend_Cloud_DocumentService_Document $document The document.
184
     * @param      array                                     $options An array of options.
185
     *
186
     * @author     David Zülke <[email protected]>
187
     * @since      1.0.5
188
     */
189 View Code Duplication
    public function replaceDocument($document, $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        if (!$this->hasParameter('collection')) {
192
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
193
        }
194
        
195
        return $this->getConnection()->replaceDocument($this->getParameter('collection'), $document, $options);
196
    }
197
    
198
    /**
199
     * Update document.
200
     * The fields of the existing documents will be updated.
201
     * Fields not specified in the set will be left as-is.
202
     * This is a convenience function using the configured "collection" parameter.
203
     *
204
     * @see        Zend_Cloud_DocumentService_Adapter::updateDocument()
205
     *
206
     * @param      mixed|Zend_Cloud_DocumentService_Document $documentID The Document ID or an
207
     *                                                       instance with updates
208
     * @param      array|Zend_Cloud_DocumentService_Document $fieldset The fields to update.
209
     * @param      array                                     $options An array of options.
210
     *
211
     * @author     David Zülke <[email protected]>
212
     * @since      1.0.5
213
     */
214
    public function updateDocument($documentID, $fieldset = null, $options = null)
215
    {
216
        if (!$this->hasParameter('collection')) {
217
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
218
        }
219
        
220
        return $this->getConnection()->updateDocument($this->getParameter('collection'), $documentID, $fieldset, $options);
221
    }
222
    
223
    /**
224
     * Delete document.
225
     * This is a convenience function using the configured "collection" parameter.
226
     *
227
     * @see        Zend_Cloud_DocumentService_Adapter::deleteDocument()
228
     *
229
     * @param      mixed $documentID ID of the document to delete.
230
     * @param      array $options An array of options.
231
     *
232
     * @author     David Zülke <[email protected]>
233
     * @since      1.0.5
234
     */
235 View Code Duplication
    public function deleteDocument($documentID, $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        if (!$this->hasParameter('collection')) {
238
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
239
        }
240
        
241
        return $this->getConnection()->deleteDocument($this->getParameter('collection'), $documentID, $options);
242
    }
243
    
244
    /**
245
     * Fetch single document by ID.
246
     * This is a convenience function using the configured "collection" parameter.
247
     *
248
     * @see        Zend_Cloud_DocumentService_Adapter::fetchDocument()
249
     *
250
     * @param      mixed $documentID ID of the document to fetch.
251
     * @param      array $options An array of options.
252
     *
253
     * @return     Zend_Cloud_DocumentService_Document The document or bool false.
254
     *
255
     * @author     David Zülke <[email protected]>
256
     * @since      1.0.5
257
     */
258 View Code Duplication
    public function fetchDocument($documentID, $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
    {
260
        if (!$this->hasParameter('collection')) {
261
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
262
        }
263
        
264
        return $this->getConnection()->fetchDocument($this->getParameter('collection'), $documentID, $options);
265
    }
266
    
267
    /**
268
     * Query for documents stored in the document service. If a string is given
269
     * as the query, the query string will be passed directly to the service.
270
     * This is a convenience function using the configured "collection" parameter.
271
     *
272
     * @see        Zend_Cloud_DocumentService_Adapter::query()
273
     *
274
     * @param      Zend_Cloud_DocumentService_Query $query The query to perform.
275
     * @param      array                            $options An array of options.
276
     *
277
     * @return     Zend_Cloud_DocumentService_DocumentSet The query result.
278
     *
279
     * @author     David Zülke <[email protected]>
280
     * @since      1.0.5
281
     */
282 View Code Duplication
    public function query($query, $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
    {
284
        if (!$this->hasParameter('collection')) {
285
            throw new DatabaseException('Convenience functions require configuration parameter "collection".');
286
        }
287
        
288
        return $this->getConnection()->query($this->getParameter('collection'), $query, $options);
289
    }
290
    
291
    /**
292
     * Create query statement.
293
     * This is a convenience function; unlike the other convenience functions, it
294
     * does not use the configured "collection" parameter, but it included here to
295
     * make the service interface complete.
296
     *
297
     * @see        Zend_Cloud_DocumentService_Adapter::select()
298
     *
299
     * @param      string $fields The fields to select.
300
     *
301
     * @return     Zend_Cloud_DocumentService_Query An initialized query instance.
302
     *
303
     * @author     David Zülke <[email protected]>
304
     * @since      1.0.5
305
     */
306
    public function select($fields = null)
307
    {
308
        return $this->getConnection()->select($fields);
309
    }
310
}
311