1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Repository; |
4
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Collection\Collection; |
6
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
7
|
|
|
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException; |
8
|
|
|
use Isswp101\Persimmon\Model\IEloquent; |
9
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
10
|
|
|
|
11
|
|
|
class RuntimeCacheRepository implements IRepository |
12
|
|
|
{ |
13
|
|
|
private $collection; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->collection = new Collection(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function getHash(string $class, string $id): string |
21
|
|
|
{ |
22
|
|
|
return $class . '@' . $id; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function instantiate(string $class): Storable |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$instance = new $class; |
28
|
|
|
if (!$instance instanceof IEloquent) { |
29
|
|
|
throw new ClassTypeErrorException(IEloquent::class); |
30
|
|
|
} |
31
|
|
|
return $instance; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function find($id, string $class, array $columns = []): Storable |
35
|
|
|
{ |
36
|
|
|
$hash = $this->getHash($class, $id); |
37
|
|
|
if (!$this->collection->has($hash)) { |
38
|
|
|
$this->collection->put($hash, new EloquentCache()); |
39
|
|
|
} |
40
|
|
|
return $this->collection->get($hash); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null) |
44
|
|
|
{ |
45
|
|
|
// TODO: Implement all() method. |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function insert(Storable $model) |
49
|
|
|
{ |
50
|
|
|
// TODO: Implement insert() method. |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function update(Storable $model) |
54
|
|
|
{ |
55
|
|
|
// TODO: Implement update() method. |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function delete(Storable $model) |
59
|
|
|
{ |
60
|
|
|
// TODO: Implement delete() method. |
61
|
|
|
} |
62
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.