Passed
Pull Request — master (#84)
by Csaba
02:32
created

Resource::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 9
nc 4
nop 4
crap 12
1
<?php
2
namespace Fathomminds\Rest\Database\MongoDB;
3
4
use MongoDB\Client;
5
use Fathomminds\Rest\Exceptions\RestException;
6
use Fathomminds\Rest\Contracts\IResource;
7
8
class Resource implements IResource
9
{
10
    protected $client;
11
    protected $databaseName;
12
    protected $collection;
13
    protected $resourceName;
14
    protected $primaryKey;
15
16
    public function __construct($resourceName, $primaryKey, Client $client = null, $databaseName = null)
17
    {
18
        $this->resourceName = $resourceName;
19
        $this->primaryKey = $primaryKey;
20
        $this->client = $client === null ?
21
            new Client(Database::getUri(), Database::getUriOptions(), Database::getDriverOptions()) :
22
            $client;
23
        $this->databaseName = $databaseName === null ? getenv('MONGODB_DATABASE') : $databaseName;
24
        $mongodb = $this->client->selectDatabase($this->databaseName);
25
        $this->collection = $mongodb->selectCollection($this->resourceName);
26
    }
27
28
    public function get($resourceId = null)
29
    {
30
        if ($resourceId !== null) {
31
            return $this->getOne($resourceId);
32
        }
33
        return $this->getAll();
34
    }
35
36
    protected function getOne($resourceId)
37
    {
38
        $res = $this->collection->findOne([$this->primaryKey => $resourceId]);
39
        return json_decode(json_encode($res));
40
    }
41
42
    protected function getAll()
43
    {
44
        $res = $this->collection->find();
45
        return json_decode(json_encode($res->toArray()));
46
    }
47
48
    public function post($newResource)
49
    {
50
        try {
51
            $res = $this->collection->insertOne($newResource);
52
            $newResource->{$this->primaryKey} = $res->getInsertedId();
53
            return $newResource;
54
        } catch (\Exception $ex) {
55
            throw new RestException($ex->getMessage(), []);
56
        }
57
    }
58
59
    public function patch($resourceId, $newResource)
60
    {
61
        try {
62
            if (isset($newResource->{$this->primaryKey})) {
63
                unset($newResource->{$this->primaryKey});
64
            }
65
            $this->collection->updateOne(
66
                [$this->primaryKey => $resourceId],
67
                ['$set' => get_object_vars($newResource)]
68
            );
69
            $newResource->{$this->primaryKey} = $resourceId;
70
            return $newResource;
71
        } catch (\Exception $ex) {
72
            throw new RestException($ex->getMessage(), []);
73
        }
74
    }
75
76
    public function put($resourceId, $newResource)
77
    {
78
        try {
79
            if (isset($newResource->{$this->primaryKey})) {
80
                unset($newResource->{$this->primaryKey});
81
            }
82
            $this->collection->replaceOne(
83
                [$this->primaryKey => $resourceId],
84
                $newResource
85
            );
86
            $newResource->{$this->primaryKey} = $resourceId;
87
            return $newResource;
88
        } catch (\Exception $ex) {
89
            throw new RestException($ex->getMessage(), []);
90
        }
91
    }
92
93
    public function delete($resourceId)
94
    {
95
        try {
96
            $this->collection->deleteOne([$this->primaryKey => $resourceId]);
97
            return null;
98
        } catch (\Exception $ex) {
99
            throw new RestException($ex->getMessage(), []);
100
        }
101
    }
102
}
103