Completed
Push — master ( 0f36fe...0316df )
by Oscar
02:37
created

FlyCrudEntity::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace App\Admin;
4
5
use Folk\Entities\EntityInterface;
6
use Folk\Entities\AbstractEntity;
7
use Folk\SearchQuery;
8
use FlyCrud\Document;
9
10
abstract class FlyCrudEntity extends AbstractEntity implements EntityInterface
11
{
12
    /**
13
     * Returns the fly-crud repository.
14
     * 
15
     * @return \FlyCrud\Repository
16
     */
17
    abstract protected function getRepository();
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function search(SearchQuery $search)
23
    {
24
        $result = [];
25
26
        foreach ($this->getRepository()->getAll() as $id => $document) {
27
            $result[$id] = $document->getData();
28
        }
29
30
        return $result;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function create(array $data)
37
    {
38
        $repository = $this->getRepository();
39
        $data = $this->beforeSave($data);
0 ignored issues
show
Bug introduced by
The method beforeSave() does not seem to exist on object<App\Admin\FlyCrudEntity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
        $document = new Document($data);
41
        $document->setId($this->generateId($document));
42
43
        $repository->save($document);
44
45
        return $document->getId();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function read($id)
52
    {
53
        $repository = $this->getRepository();
54
55
        return $repository->get($id)->getData();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function update($id, array $data)
62
    {
63
        $repository = $this->getRepository();
64
65
        $document = $repository->get($id);
66
        $data = $this->beforeSave($data);
0 ignored issues
show
Bug introduced by
The method beforeSave() does not seem to exist on object<App\Admin\FlyCrudEntity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        $document->setData($data);
68
69
        $repository->save($document);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function delete($id)
76
    {
77
        $repository = $this->getRepository();
78
79
        $document = $repository->get($id);
80
        $repository->delete($document);
81
    }
82
83
    /**
84
     * Returns the id for the new documents
85
     * Useful to customize the id before save.
86
     * 
87
     * @param Document $document
88
     * 
89
     * @return string
90
     */
91
    protected function generateId(Document $document)
92
    {
93
        return $document->getId();
94
    }
95
}
96