diff options
Diffstat (limited to 'src/DTS/BookmarkRepository.php')
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 40 |
1 files changed, 12 insertions, 28 deletions
diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index a2d80cd..01f6de4 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -43,9 +43,7 @@ class BookmarkRepository implements \Iterator public function find(string $id): ?Bookmark { - $index = $this->findBookmarkIndex($id); - - return $this->repository[$index] ?? null; + return $this->repository[$id] ?? null; } public function add(Bookmark $bookmark): bool @@ -57,10 +55,8 @@ class BookmarkRepository implements \Iterator public function update(Bookmark $bookmark): bool { - $index = $this->findBookmarkIndex($bookmark->id); - - if ($index !== null) { - $this->repository[$index] = $bookmark; + if (array_key_exists($bookmark->id, $this->repository)) { + $this->repository[$bookmark->id] = $bookmark; return $this->save(); } @@ -70,10 +66,8 @@ class BookmarkRepository implements \Iterator public function delete(Bookmark $bookmark): bool { - $index = $this->findBookmarkIndex($bookmark->id); - - if ($index !== null) { - unset($this->repository[$index]); + if (array_key_exists($bookmark->id, $this->repository)) { + unset($this->repository[$bookmark->id]); return $this->save(); } @@ -106,17 +100,6 @@ class BookmarkRepository implements \Iterator return isset($this->repository[$this->position]); } - private function findBookmarkIndex(string $id): ?int - { - foreach ($this->repository as $index => $bookmark) { - if ($bookmark->id == $id) { - return $index; - } - } - - return null; - } - private function load(): void { if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { @@ -127,14 +110,16 @@ class BookmarkRepository implements \Iterator throw new \Exception("Unable to read from repository {$this->pathToRepository}"); } + $id = 0; + while (($data = fgetcsv($fp)) !== FALSE) { $bookmark = new Bookmark(); - $bookmark->id = $data[0]; - $bookmark->url = $data[1]; - $bookmark->title = $data[2]; - $bookmark->tag = $data[3]; - $bookmark->addedAt = $data[4]; + $bookmark->id = $id++; + $bookmark->url = $data[0]; + $bookmark->title = $data[1]; + $bookmark->tag = $data[2]; + $bookmark->addedAt = $data[3]; $this->repository[] = $bookmark; } @@ -152,7 +137,6 @@ class BookmarkRepository implements \Iterator foreach ($this->repository as $bookmark) { $success = $success && fputcsv($fp, [ - $bookmark->id, $bookmark->url, $bookmark->title, $bookmark->tag, |
