Passed
Push — master ( 03e013...bd5966 )
by Anatoly
01:18 queued 14s
created

EntryService::multipleDelete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
eloc 8
nc 3
nop 1
dl 0
loc 15
c 0
b 0
f 0
cc 3
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 10
1
<?php declare(strict_types=1);
2
3
namespace App\Service;
4
5
/**
6
 * Import classes
7
 */
8
use App\Entity\Entry;
9
use App\ContainerAwareTrait;
10
11
/**
12
 * EntryService
13
 */
14
final class EntryService
15
{
16
    use ContainerAwareTrait;
17
18
    /**
19
     * Gets the numbers of entries
20
     *
21
     * @return int
22
     */
23 14
    public function countAll() : int
24
    {
25 14
        $manager = $this->container->get('entityManager');
26 14
        $repository = $manager->getRepository(Entry::class);
27
28 14
        return $repository->countAll();
29
    }
30
31
    /**
32
     * Creates an entry
33
     *
34
     * @param array $data
35
     *
36
     * @return void
37
     */
38 7
    public function create(array $data) : void
39
    {
40 7
        $manager = $this->container->get('entityManager');
41
42 7
        $entry = new Entry();
43 7
        $entry->setName($data['name']);
44
45 7
        $manager->persist($entry);
46 7
        $manager->flush();
47 7
    }
48
49
    /**
50
     * Multiple creation of entries
51
     *
52
     * @param array ...$variety
53
     *
54
     * @return void
55
     */
56 3
    public function multipleCreate(array ...$variety) : void
57
    {
58 3
        $manager = $this->container->get('entityManager');
59
60 3
        foreach ($variety as $data) {
61 3
            $entry = new Entry();
62 3
            $entry->setName($data['name']);
63
64 3
            $manager->persist($entry);
65
        }
66
67 3
        $manager->flush();
68 3
    }
69
70
    /**
71
     * Updates an entry by the given ID
72
     *
73
     * @param int $id
74
     * @param array $data
75
     *
76
     * @return void
77
     */
78 1
    public function updateById(int $id, array $data) : void
79
    {
80 1
        if (empty($data)) {
81
            return;
82
        }
83
84 1
        $manager = $this->container->get('entityManager');
85 1
        $repository = $manager->getRepository(Entry::class);
86 1
        $entry = $repository->findById($id);
87
88 1
        if (empty($entry)) {
89
            return;
90
        }
91
92 1
        if (isset($data['name'])) {
93 1
            $entry->setName($data['name']);
94
        }
95
96 1
        $manager->flush();
97 1
    }
98
99
    /**
100
     * Deletes an entry by the given ID
101
     *
102
     * @param int $id
103
     *
104
     * @return void
105
     */
106 1
    public function deleteById(int $id) : void
107
    {
108 1
        $manager = $this->container->get('entityManager');
109 1
        $repository = $manager->getRepository(Entry::class);
110 1
        $entry = $repository->findById($id);
111
112 1
        if (empty($entry)) {
113
            return;
114
        }
115
116 1
        $manager->remove($entry);
117 1
        $manager->flush();
118 1
    }
119
120
    /**
121
     * Multiple deletion of entries
122
     *
123
     * @param int ...$ids
124
     *
125
     * @return void
126
     */
127 1
    public function multipleDelete(int ...$ids) : void
128
    {
129 1
        $manager = $this->container->get('entityManager');
130 1
        $repository = $manager->getRepository(Entry::class);
131 1
        $entries = $repository->findByIds(...$ids);
132
133 1
        if (empty($entries)) {
134
            return;
135
        }
136
137 1
        foreach ($entries as $entry) {
138 1
            $manager->remove($entry);
139
        }
140
141 1
        $manager->flush();
142 1
    }
143
144
    /**
145
     * Checks if an entry exists by the given ID
146
     *
147
     * @param int $id
148
     *
149
     * @return bool
150
     */
151 6
    public function existsById(int $id) : bool
152
    {
153 6
        $manager = $this->container->get('entityManager');
154 6
        $repository = $manager->getRepository(Entry::class);
155 6
        $entry = $repository->findById($id);
156
157 6
        return isset($entry);
158
    }
159
160
    /**
161
     * Read an entry by the given ID
162
     *
163
     * @param int $id
164
     *
165
     * @return null|array
166
     */
167 1
    public function readById(int $id) : ?array
168
    {
169 1
        $manager = $this->container->get('entityManager');
170 1
        $repository = $manager->getRepository(Entry::class);
171 1
        $entry = $repository->findById($id);
172
173 1
        if (empty($entry)) {
174
            return null;
175
        }
176
177
        return [
178 1
            'id' => $entry->getId(),
179 1
            'name' => $entry->getName(),
180
        ];
181
    }
182
183
    /**
184
     * Gets all entries
185
     *
186
     * @return array
187
     */
188 2
    public function getAll() : array
189
    {
190 2
        $manager = $this->container->get('entityManager');
191 2
        $repository = $manager->getRepository(Entry::class);
192
193 2
        $result = [];
194 2
        foreach ($repository->getAll() as $entry) {
195
            $item = [
196 1
                'id' => $entry->getId(),
197 1
                'name' => $entry->getName(),
198
            ];
199
200 1
            $result[] = $item;
201
        }
202
203 2
        return $result;
204
    }
205
}
206