Passed
Pull Request — development (#682)
by Thomas
06:58
created

FieldNoteService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 10 2
A fetchBy() 0 10 2
A fetchOneBy() 0 10 2
A create() 0 4 1
A update() 0 4 1
A remove() 0 4 1
A getUserListing() 0 11 2
A getLatestUserFieldNote() 0 10 2
1
<?php
2
3
namespace Oc\FieldNotes\Persistence;
4
5
use Oc\Repository\Exception\RecordNotFoundException;
6
use Oc\Repository\Exception\RecordsNotFoundException;
7
8
class FieldNoteService
9
{
10
    /**
11
     * @var FieldNoteRepository
0 ignored issues
show
introduced by
FieldNoteRepository => \Array\FieldNoteRepository
Loading history...
12
     */
13
    private $fieldNoteRepository;
14
15
    /**
16
     * @param FieldNoteRepository $fieldNoteRepository
0 ignored issues
show
introduced by
FieldNoteRepository => \Array\FieldNoteRepository
Loading history...
17
     */
18
    public function __construct(FieldNoteRepository $fieldNoteRepository)
19
    {
20
        $this->fieldNoteRepository = $fieldNoteRepository;
21
    }
22
23
    /**
24
     * Fetches all field notes.
25
     *
26
     * @return FieldNoteEntity[]
0 ignored issues
show
introduced by
FieldNoteEntity[] => \Array\FieldNoteEntity[]
Loading history...
27
     */
28
    public function fetchAll()
29
    {
30
        try {
31
            $result = $this->fieldNoteRepository->fetchAll();
32
        } catch (RecordsNotFoundException $e) {
33
            $result = [];
34
        }
35
36
        return $result;
37
    }
38
39
    /**
40
     * Fetch by given where clause.
41
     *
42
     * @param array $where
43
     * @param array $order
44
     *
45
     * @return FieldNoteEntity[]
0 ignored issues
show
introduced by
FieldNoteEntity[] => \Array\FieldNoteEntity[]
Loading history...
46
     */
47
    public function fetchBy(array $where = [], array $order = [])
48
    {
49
        try {
50
            $result = $this->fieldNoteRepository->fetchBy($where, $order);
51
        } catch (RecordsNotFoundException $e) {
52
            $result = [];
53
        }
54
55
        return $result;
56
    }
57
58
    /**
59
     * Fetches a page by slug.
60
     *
61
     * @param array $where
62
     *
63
     * @return null|FieldNoteEntity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
64
     */
65
    public function fetchOneBy(array $where = [])
66
    {
67
        try {
68
            $result = $this->fieldNoteRepository->fetchOneBy($where);
69
        } catch (RecordNotFoundException $e) {
70
            $result = null;
71
        }
72
73
        return $result;
74
    }
75
76
    /**
77
     * Creates a field note in the database.
78
     *
79
     * @param FieldNoteEntity $entity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
80
     *
81
     * @return FieldNoteEntity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
82
     */
83
    public function create(FieldNoteEntity $entity)
84
    {
85
        return $this->fieldNoteRepository->create($entity);
86
    }
87
88
    /**
89
     * Update a field note in the database.
90
     *
91
     * @param FieldNoteEntity $entity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
92
     *
93
     * @return FieldNoteEntity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
94
     */
95
    public function update(FieldNoteEntity $entity)
96
    {
97
        return $this->fieldNoteRepository->update($entity);
98
    }
99
100
    /**
101
     * Removes a field note from the database.
102
     *
103
     * @param FieldNoteEntity $entity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
104
     *
105
     * @return FieldNoteEntity
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
106
     */
107
    public function remove(FieldNoteEntity $entity)
108
    {
109
        return $this->fieldNoteRepository->remove($entity);
110
    }
111
112
    /**
113
     * Fetch all field notes for field note listing in profile.
114
     *
115
     * @param int $userId
116
     *
117
     * @return FieldNoteEntity[]
0 ignored issues
show
introduced by
FieldNoteEntity[] => \Array\FieldNoteEntity[]
Loading history...
118
     */
119
    public function getUserListing($userId)
120
    {
121
        $fieldNotes = $this->fetchBy([
122
            'user_id' => $userId
123
        ], [
124
            'date' => 'ASC',
125
            'id' => 'ASC'
126
        ]);
127
128
        return $fieldNotes ?: [];
129
    }
130
131
    /**
132
     * Fetches the latest field note for given user id.
133
     *
134
     * @param int $userId
135
     *
136
     * @return FieldNoteEntity|null
0 ignored issues
show
introduced by
FieldNoteEntity => \Array\FieldNoteEntity
Loading history...
137
     */
138
    public function getLatestUserFieldNote($userId)
139
    {
140
        try {
141
            $result = $this->fieldNoteRepository->getLatestUserFieldNote($userId);
142
        } catch (RecordNotFoundException $e) {
143
            $result = null;
144
        }
145
146
        return $result;
147
    }
148
}
149