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
|
|
|
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
|
|
|
public function update(AssetPackage $package) |
55
|
|
|
{ |
56
|
|
|
return $this->db->createCommand()->update('package', [ |
57
|
|
|
'last_update' => $package->getUpdateTime() |
58
|
|
|
], $this->getWhereCondition($package))->execute(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function markAvoided(AssetPackage $package) |
62
|
|
|
{ |
63
|
|
|
$this->db->createCommand()->update('package', [ |
64
|
|
|
'is_avoided' => true |
65
|
|
|
], $this->getWhereCondition($package))->execute(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getWhereCondition(AssetPackage $package) |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'type' => $package->getType(), |
72
|
|
|
'name' => $package->getName(), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param AssetPackage $package |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function exists(AssetPackage $package) |
81
|
|
|
{ |
82
|
|
|
return (new Query()) |
83
|
|
|
->from('package') |
84
|
|
|
->where(['type' => $package->getType(), 'name' => $package->getName()]) |
85
|
|
|
->exists($this->db); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \hiqdev\assetpackagist\models\AssetPackage[] |
90
|
|
|
*/ |
91
|
|
|
public function getExpiredForUpdate() |
92
|
|
|
{ |
93
|
|
|
$rows = (new Query()) |
94
|
|
|
->from('package') |
95
|
|
|
->where(['<', 'last_update', time() - 60 * 60 * 24 * 7]) // Older than 7 days |
96
|
|
|
->andWhere(['not', ['last_update' => null]]) |
97
|
|
|
->andWhere(['is_avoided' => null]) |
98
|
|
|
->all(); |
99
|
|
|
|
100
|
|
|
return $this->hydrate($rows); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $rows |
105
|
|
|
* @return AssetPackage[] |
106
|
|
|
*/ |
107
|
|
|
public function hydrate($rows) |
108
|
|
|
{ |
109
|
|
|
$result = []; |
110
|
|
|
foreach ($rows as $row) { |
111
|
|
|
$result[] = new AssetPackage($row['type'], $row['name']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|