Completed
Push — master ( eb0e60...e48222 )
by Dmitry
02:26
created

PackageRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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
    public function __construct(Connection $db)
17
    {
18
        $this->db = $db;
19
    }
20
21
    public function save(AssetPackage $package)
22
    {
23
        if ($this->exists($package)) {
24
            $this->update($package);
25
        } else {
26
            $this->insert($package);
27
        }
28
    }
29
30 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...
31
        $this->db->createCommand()->insert('package', [
32
            'type' => $package->getType(),
33
            'name' => $package->getName(),
34
            'last_update' => $package->getUpdateTime(),
35
        ])->execute();
36
    }
37
38 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...
39
    {
40
        $this->db->createCommand()->update('package', [
41
            'last_update' => $package->getUpdateTime()
42
        ], [
43
            'type' => $package->getType(),
44
            'name' => $package->getName(),
45
        ])->execute();
46
    }
47
48
    /**
49
     * @param AssetPackage $package
50
     * @return bool
51
     */
52
    public function exists(AssetPackage $package)
53
    {
54
        return (new Query())
55
            ->from('package')
56
            ->where(['type' => $package->getType(), 'name' => $package->getName()])
57
            ->exists($this->db);
58
    }
59
}
60