summaryrefslogtreecommitdiff
path: root/src/DTS/TodoRepository.php
blob: 5ec759f4b003268117b06932c52aa87e176722f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

declare(strict_types=1);

namespace DTS;

use DTS\Todo;

class TodoRepository implements \Iterator
{
    private string $pathToRepository;

    private array $repository = [];

    private array $tags = [];

    private int $position = 0;

    function __construct(string $pathToRepository)
    {
        $this->pathToRepository = $pathToRepository;

        $this->load();
    }

    public function sort(bool $asc = true): TodoRepository
    {
        usort($this->repository, function ($a, $b) use ($asc) {
            return $asc ? $a->task <=> $b->task
                : $b->task <=> $a->task;
        });

        return $this;
    }

    public function filter(string $tag): TodoRepository
    {
        $this->repository = array_filter(
            $this->repository,
            fn($todo) => $todo->tag === $tag
        );

        return $this;
    }

    public function find(string $id): ?Todo
    {
        return $this->repository[$id] ?? null;
    }

    public function add(Todo $todo): bool
    {
        $this->repository[] = $todo;

        return $this->save();
    }

    public function update(Todo $todo): bool
    {
        if (array_key_exists($todo->id, $this->repository)) {
            $this->repository[$todo->id] = $todo;

            return $this->save();
        }

        return false;
    }

    public function delete(Todo $todo): bool
    {
        if (array_key_exists($todo->id, $this->repository)) {
            unset($this->repository[$todo->id]);

            return $this->save();
        }

        return false;
    }

    public function tags(): array
    {
        return $this->tags;
    }

    public function current()
    {
        return $this->repository[$this->position];
    }

    public function key()
    {
        return $this->position;
    }

    public function next(): void
    {
        ++$this->position;
    }

    public function rewind(): void
    {
        $this->position = 0;
    }

    public function valid(): bool
    {
        return isset($this->repository[$this->position]);
    }

    private function load(): void
    {
        if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) {
            throw new \Exception("Unable to locate repository {$this->pathToRepository}");
        }

        if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) {
            throw new \Exception("Unable to read from repository {$this->pathToRepository}");
        }

        $id = 0;

        while (($data = fgetcsv($fp)) !== FALSE) {
            $todo = new Todo();

            $todo->id = $id++;
            $todo->task = $data[0];
            $todo->tag = $data[1];
            $todo->addedAt = $data[2];

            $this->repository[] = $todo;

            $this->tags[] = $todo->tag;
        }

        $this->tags = array_filter(array_unique($this->tags));

        sort($this->tags);

        fclose($fp);
    }

    private function save(): bool
    {
        if (($fp = fopen($this->pathToRepository, 'w')) === FALSE) {
            throw new \Exception("Unable to open repository {$this->pathToRepository}");
        }

        $success = true;

        foreach ($this->repository as $todo) {
            $success = $success && fputcsv($fp, [
                $todo->task,
                $todo->tag,
                $todo->addedAt,
            ]) !== false;
        }

        fclose($fp);

        return $success;
    }
}