Completed
Pull Request — master (#22)
by Sergey
14:44
created

RuntimeCacheRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 8
loc 52
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHash() 0 4 1
A instantiate() 8 8 2
A find() 0 8 2
A all() 0 4 1
A insert() 0 4 1
A update() 0 4 1
A delete() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}