FileSystemRepository::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 6/02/16
6
 * Time: 13:05.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem;
12
13
use NilPortugues\Assert\Assert;
14
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;
15
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter;
16
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity;
17
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page;
18
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
19
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository;
20
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository;
21
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort;
22
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository;
23
use NilPortugues\Foundation\Domain\Model\Repository\Page as ResultPage;
24
use NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem\Contracts\FileSystem;
25
use NilPortugues\Foundation\Infrastructure\Model\Repository\InMemory\Filter as InMemoryFilter;
26
use NilPortugues\Foundation\Infrastructure\Model\Repository\InMemory\Sorter as InMemorySorter;
27
28
/**
29
 * Class FileSystemRepository.
30
 */
31
class FileSystemRepository implements ReadRepository, WriteRepository, PageRepository
32
{
33
    /**
34
     * @var FileSystem
35
     */
36
    protected $fileSystem;
37
38
    /**
39
     * FileSystemRepository constructor.
40
     *
41
     * @param FileSystem $fileSystem
42
     */
43
    public function __construct(FileSystem $fileSystem)
44
    {
45
        $this->fileSystem = $fileSystem;
46
    }
47
48
    /**
49
     * Retrieves an entity by its id.
50
     *
51
     * @param Identity    $id
52
     * @param Fields|null $fields
53
     *
54
     * @return mixed
55
     */
56
    public function find(Identity $id, Fields $fields = null)
57
    {
58
        return $this->fileSystem->read($id->id());
59
    }
60
61
    /**
62
     * Returns the total amount of elements in the repository given the restrictions provided by the Filter object.
63
     *
64
     * @param Filter|null $filter
65
     *
66
     * @return int
67
     */
68
    public function count(Filter $filter = null)
69
    {
70
        $allFiles = $this->fileSystem->files();
71
72
        if ($filter) {
73
            $allFiles = InMemoryFilter::filter($allFiles, $filter);
74
        }
75
76
        return count($allFiles);
77
    }
78
79
    /**
80
     * Returns whether an entity with the given id exists.
81
     *
82
     * @param $id
83
     *
84
     * @return bool
85
     */
86
    public function exists(Identity $id)
87
    {
88
        return $this->fileSystem->exists($id);
89
    }
90
91
    /**
92
     * Adds a collections of entities to the storage.
93
     *
94
     * @param array $values
95
     *
96
     * @return mixed
97
     */
98
    public function addAll(array $values)
99
    {
100
        foreach ($values as $value) {
101
            Assert::isInstanceOf($value, Identity::class);
102
            $this->add($value);
103
        }
104
    }
105
106
    /**
107
     * Adds a new entity to the storage.
108
     *
109
     * @param Identity $value
110
     *
111
     * @return mixed
112
     */
113
    public function add(Identity $value)
114
    {
115
        $this->fileSystem->write($value->id(), $value);
116
    }
117
118
    /**
119
     * Removes all elements in the repository given the restrictions provided by the Filter object.
120
     * If $filter is null, all the repository data will be deleted.
121
     *
122
     * @param Filter $filter
123
     *
124
     * @return bool
125
     */
126
    public function removeAll(Filter $filter = null)
127
    {
128
        if (null === $filter) {
129
            $this->fileSystem->deleteAll();
130
131
            return true;
132
        }
133
134
        $elements = (array) $this->findBy($filter);
135
        foreach ($elements as $element) {
136
            $this->remove($element);
137
        }
138
139
        return true;
140
    }
141
142
    /**
143
     * Returns all instances of the type.
144
     *
145
     * @param Filter|null $filter
146
     * @param Sort|null   $sort
147
     * @param Fields|null $fields
148
     *
149
     * @return array
150
     */
151
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
152
    {
153
        $allFiles = $this->fileSystem->files();
154
155
        if ($filter) {
156
            $allFiles = InMemoryFilter::filter($allFiles, $filter);
157
        }
158
159
        if ($sort) {
160
            $allFiles = InMemorySorter::sort($allFiles, $sort);
161
        }
162
163
        return $allFiles;
164
    }
165
166
    /**
167
     * Removes the entity with the given id.
168
     *
169
     * @param $id
170
     */
171
    public function remove(Identity $id)
172
    {
173
        $this->fileSystem->delete($id);
174
    }
175
176
    /**
177
     * Returns a Page of entities meeting the paging restriction provided in the Pageable object.
178
     *
179
     * @param Pageable $pageable
180
     *
181
     * @return Page
182
     */
183
    public function findAll(Pageable $pageable = null)
184
    {
185
        if (null === $pageable) {
186
            $files = $this->findBy();
187
188
            return new ResultPage($files, count($files), 1, 1);
189
        }
190
191
        $results = $this->findBy($pageable->filters(), $pageable->sortings());
192
193
        return new ResultPage(
194
            array_slice($results, $pageable->offset() - $pageable->pageSize(), $pageable->pageSize()),
195
            count($results),
196
            $pageable->pageNumber(),
197
            ceil(count($results) / $pageable->pageSize())
198
        );
199
    }
200
}
201