Completed
Push — master ( bc20b9...9515fd )
by Dmitry
02:03
created

PackageRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 16
loc 94
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 8 2
A insert() 7 7 1
A update() 9 9 1
A exists() 0 7 1
A getExpired() 0 9 1
A hydrate() 0 9 2

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 hiqdev\assetpackagist\repositories;
4
5
use hiqdev\assetpackagist\models\AssetPackage;
6
use yii\db\Connection;
7
use yii\db\Query;
8
9
class PackageRepository
10
{
11
    /**
12
     * @var Connection
13
     */
14
    protected $db;
15
16
    /**
17
     * PackageRepository constructor.
18
     * @param Connection $db
19
     */
20
    public function __construct(Connection $db)
21
    {
22
        $this->db = $db;
23
    }
24
25
    /**
26
     * @param AssetPackage $package
27
     * @return int
28
     */
29
    public function save(AssetPackage $package)
30
    {
31
        if ($this->exists($package)) {
32
            return $this->update($package);
33
        } else {
34
            return $this->insert($package);
35
        }
36
    }
37
38
    /**
39
     * @param AssetPackage $package
40
     * @return int
41
     */
42 View Code Duplication
    public function insert(AssetPackage $package) {
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...
43
        return $this->db->createCommand()->insert('package', [
44
            'type' => $package->getType(),
45
            'name' => $package->getName(),
46
            'last_update' => $package->getUpdateTime(),
47
        ])->execute();
48
    }
49
50
    /**
51
     * @param AssetPackage $package
52
     * @return int
53
     */
54 View Code Duplication
    public function update(AssetPackage $package)
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...
55
    {
56
        return $this->db->createCommand()->update('package', [
57
            'last_update' => $package->getUpdateTime()
58
        ], [
59
            'type' => $package->getType(),
60
            'name' => $package->getName(),
61
        ])->execute();
62
    }
63
64
    /**
65
     * @param AssetPackage $package
66
     * @return bool
67
     */
68
    public function exists(AssetPackage $package)
69
    {
70
        return (new Query())
71
            ->from('package')
72
            ->where(['type' => $package->getType(), 'name' => $package->getName()])
73
            ->exists($this->db);
74
    }
75
76
    /**
77
     * @return \hiqdev\assetpackagist\models\AssetPackage[]
78
     */
79
    public function getExpired()
80
    {
81
        $rows = (new Query())
82
            ->from('package')
83
            ->where(['<', 'last_update', time() - 60 * 60 * 24 * 7]) // Older than 7 days
84
            ->all();
85
86
        return $this->hydrate($rows);
87
    }
88
89
    /**
90
     * @param array $rows
91
     * @return AssetPackage[]
92
     */
93
    public function hydrate($rows)
94
    {
95
        $result = [];
96
        foreach ($rows as $row) {
97
            $result[] = new AssetPackage($row['type'], $row['name']);
98
        }
99
100
        return $result;
101
    }
102
}
103