Completed
Push — master ( cf9e4e...549566 )
by Andrii
10:33
created

PackageRepository::isAvoided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Asset Packagist.
4
 *
5
 * @link      https://github.com/hiqdev/asset-packagist
6
 * @package   asset-packagist
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\assetpackagist\repositories;
12
13
use hiqdev\assetpackagist\models\AssetPackage;
14
use yii\db\Connection;
15
use yii\db\Query;
16
17
class PackageRepository
18
{
19
    /**
20
     * @var Connection
21
     */
22
    protected $db;
23
24
    /**
25
     * PackageRepository constructor.
26
     * @param Connection $db
27
     */
28
    public function __construct(Connection $db)
29
    {
30
        $this->db = $db;
31
    }
32
33
    /**
34
     * @param AssetPackage $package
35
     * @return int
36
     */
37
    public function save(AssetPackage $package)
38
    {
39
        if ($this->exists($package)) {
40
            return $this->update($package);
41
        } else {
42
            return $this->insert($package);
43
        }
44
    }
45
46
    /**
47
     * @param AssetPackage $package
48
     * @return int
49
     */
50
    public function insert(AssetPackage $package)
51
    {
52
        return $this->db->createCommand()->insert('package', [
53
            'type' => $package->getType(),
54
            'name' => $package->getName(),
55
            'last_update' => $package->getUpdateTime(),
56
        ])->execute();
57
    }
58
59
    /**
60
     * @param AssetPackage $package
61
     * @return int
62
     */
63
    public function update(AssetPackage $package)
64
    {
65
        return $this->db->createCommand()->update('package', [
66
            'last_update' => $package->getUpdateTime(),
67
        ], $this->getWhereCondition($package))->execute();
68
    }
69
70
    public function markAvoided(AssetPackage $package)
71
    {
72
        $this->db->createCommand()->update('package', [
73
            'is_avoided' => true,
74
        ], $this->getWhereCondition($package))->execute();
75
    }
76
77
    public function isAvoided(AssetPackage $package)
78
    {
79
        return (new Query())
80
            ->select('is_avoided')
81
            ->from('package')
82
            ->where($this->getWhereCondition($package))
83
            ->scalar($this->db);
84
    }
85
86
    protected function getWhereCondition(AssetPackage $package)
87
    {
88
        return [
89
            'type' => $package->getType(),
90
            'name' => $package->getName(),
91
        ];
92
    }
93
94
    /**
95
     * @param AssetPackage $package
96
     * @return bool
97
     */
98
    public function exists(AssetPackage $package)
99
    {
100
        return (new Query())
101
            ->from('package')
102
            ->where($this->getWhereCondition($package))
103
            ->exists($this->db);
104
    }
105
106
    /**
107
     * @return \hiqdev\assetpackagist\models\AssetPackage[]
108
     */
109
    public function getExpiredForUpdate()
110
    {
111
        $rows = (new Query())
112
            ->from('package')
113
            ->where(['<', 'last_update', time() - 60 * 60 * 24 * 7]) // Older than 7 days
114
            ->andWhere(['not', ['last_update' => null]])
115
            ->andWhere(['is_avoided' => 0])
116
            ->all();
117
118
        return $this->hydrate($rows);
119
    }
120
121
    /**
122
     * @param array $rows
123
     * @return AssetPackage[]
124
     */
125
    public function hydrate($rows)
126
    {
127
        $result = [];
128
        foreach ($rows as $row) {
129
            $result[] = new AssetPackage($row['type'], $row['name']);
130
        }
131
132
        return $result;
133
    }
134
}
135