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) { |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.