Completed
Push — master ( 0316df...ba7074 )
by Oscar
02:41
created

FlyCrud::generateId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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

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

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