Completed
Push — master ( 59e426...a0562a )
by Dmitry
02:54
created

PackageRepository::markAvoided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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