Passed
Pull Request — development (#682)
by Nick
07:09
created

FieldNoteService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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