Storable::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Picqer\Financials\Exact\Persistance;
4
5
use Picqer\Financials\Exact\Connection;
6
7
trait Storable
8
{
9
    /**
10
     * @return bool
11
     */
12
    abstract public function exists();
13
14
    /**
15
     * @param array $attributes
16
     */
17
    abstract public function fill(array $attributes);
18
19
    /**
20
     * @param int  $options
21
     * @param bool $withDeferred
22
     *
23
     * @return string
24
     */
25
    abstract public function json($options = 0, $withDeferred = false);
26
27
    /**
28
     * @return Connection
29
     */
30
    abstract public function connection();
31
32
    /**
33
     * @return string
34
     */
35
    abstract public function url();
36
37
    /**
38
     * @return mixed
39
     */
40
    abstract public function primaryKeyContent();
41
42
    /**
43
     * @return $this
44
     */
45
    public function save()
46
    {
47
        if ($this->exists()) {
48
            $this->fill($this->update());
49
        } else {
50
            $this->fill($this->insert());
51
        }
52
53
        return $this;
54
    }
55
56
    public function insert()
57
    {
58
        return $this->connection()->post($this->url(), $this->json(0, true));
59
    }
60
61
    public function update()
62
    {
63
        $primaryKey = $this->primaryKeyContent();
64
65
        return $this->connection()->put($this->url() . "(guid'$primaryKey')", $this->json());
66
    }
67
68
    public function delete()
69
    {
70
        $primaryKey = $this->primaryKeyContent();
71
72
        return $this->connection()->delete($this->url() . "(guid'$primaryKey')");
73
    }
74
}
75