Passed
Pull Request — master (#88)
by
unknown
12:16
created

Database::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 5
crap 2
1
<?php
2
namespace Fathomminds\Rest\Database\MongoDB;
3
4
use MongoDB\Client;
5
use Fathomminds\Rest\Contracts\IDatabase;
6
7
/**
8
 *
9
 * @property \MongoDB\Client $client
10
 * @property string $databaseName
11
 *
12
 */
13
14
class Database implements IDatabase
15
{
16
    protected $client;
17
    protected $databaseName;
18
19 1
    public function __construct(Client $client = null, $databaseName = null)
20
    {
21 1
        $this->client = $client === null ?
22 1
            new Client(self::getUri(), self::getUriOptions(), self::getDriverOptions()) :
23
            $client;
24 1
        $this->databaseName = $databaseName === null ? getenv('MONGODB_DATABASE') : $databaseName;
25 1
    }
26
27 1
    public static function getUri() {
28
        return 'mongodb://' .
29 1
            getenv('MONGODB_USERNAME') . ':' .
30 1
            getenv('MONGODB_PASSWORD') . '@' .
31 1
            getenv('MONGODB_HOST') . '/' .
32 1
            getenv('MONGODB_AUTH_DATABASE');
33
    }
34
35 1
    public static function getUriOptions() {
36 1
        return [];
37
    }
38
39 1
    public static function getDriverOptions() {
40 1
        return [];
41
    }
42
43
    public function get($resourceName, $schema, $primaryKey, $resourceId = null)
44
    {
45
        return (new Resource(
46
            $resourceName,
47
            $schema,
48
            $primaryKey,
49
            $this->client,
50
            $this->databaseName
51
        ))->get($resourceId);
52
    }
53
54
    public function post($resourceName, $schema, $primaryKey, $newResource)
55
    {
56
        return (new Resource(
57
            $resourceName,
58
            $schema,
59
            $primaryKey,
60
            $this->client,
61
            $this->databaseName
62
        ))->post($newResource);
63
    }
64
65
    public function patch($resourceName, $schema, $primaryKey, $resourceId, $newResource)
66
    {
67
        return (new Resource(
68
            $resourceName,
69
            $schema,
70
            $primaryKey,
71
            $this->client,
72
            $this->databaseName
73
        ))->patch($resourceId, $newResource);
74
    }
75
76
    public function put($resourceName, $schema, $primaryKey, $resourceId, $newResource)
77
    {
78
        return (new Resource(
79
            $resourceName,
80
            $schema,
81
            $primaryKey,
82
            $this->client,
83
            $this->databaseName
84
        ))->put($resourceId, $newResource);
85
    }
86
87
    public function delete($resourceName, $schema, $primaryKey, $resourceId)
88
    {
89
        return (new Resource(
90
            $resourceName,
91
            $schema,
92
            $primaryKey,
93
            $this->client,
94
            $this->databaseName
95
        ))->delete($resourceId);
96
    }
97
98
    public function setDatabaseName($databaseName)
99
    {
100
        $this->databaseName = $databaseName;
101
    }
102
103
    public function getDatabaseName()
104
    {
105
        return $this->databaseName;
106
    }
107
108
    public function getClient()
109
    {
110
        return $this->client;
111
    }
112
}
113