Passed
Push — develop ( 60641a...1612eb )
by Csaba
58s
created

Database::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
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
    }
33
34 1
    public static function getUriOptions() {
35 1
        return [];
36
    }
37
38 1
    public static function getDriverOptions() {
39 1
        return [];
40
    }
41
42
    public function get($resourceName, $primaryKey, $resourceId = null)
43
    {
44
        return (new Resource(
45
            $resourceName,
46
            $primaryKey,
47
            $this->client,
48
            $this->databaseName
49
        ))->get($resourceId);
50
    }
51
52
    public function post($resourceName, $primaryKey, $newResource)
53
    {
54
        return (new Resource(
55
            $resourceName,
56
            $primaryKey,
57
            $this->client,
58
            $this->databaseName
59
        ))->post($newResource);
60
    }
61
62
    public function patch($resourceName, $primaryKey, $resourceId, $newResource)
63
    {
64
        return (new Resource(
65
            $resourceName,
66
            $primaryKey,
67
            $this->client,
68
            $this->databaseName
69
        ))->patch($resourceId, $newResource);
70
    }
71
72
    public function put($resourceName, $primaryKey, $resourceId, $newResource)
73
    {
74
        return (new Resource(
75
            $resourceName,
76
            $primaryKey,
77
            $this->client,
78
            $this->databaseName
79
        ))->put($resourceId, $newResource);
80
    }
81
82
    public function delete($resourceName, $primaryKey, $resourceId)
83
    {
84
        return (new Resource(
85
            $resourceName,
86
            $primaryKey,
87
            $this->client,
88
            $this->databaseName
89
        ))->delete($resourceId);
90
    }
91
92
    public function setDatabaseName($databaseName)
93
    {
94
        $this->databaseName = $databaseName;
95
    }
96
97
    public function getDatabaseName()
98
    {
99
        return $this->databaseName;
100
    }
101
102
    public function getClient()
103
    {
104
        return $this->client;
105
    }
106
}
107