AppRelease   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 90
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 18 1
A attributeLabels() 0 15 1
A behaviors() 0 15 1
A fields() 0 15 2
1
<?php
2
3
namespace zacksleo\yii2\apprelease\models;
4
5
use Yii;
6
use yii\helpers\Url;
7
use yii\web\UploadedFile;
8
use yii\behaviors\TimestampBehavior;
9
use zacksleo\yii2\apprelease\Module;
10
use zacksleo\yii2\apprelease\behaviors\UploadBehavior;
11
12
/**
13
 * This is the model class for table "{{%app_release}}".
14
 *
15
 * @property integer $id
16
 * @property integer $version
17
 * @property integer $is_forced
18
 * @property string $url
19
 * @property string $md5
20
 * @property integer $status
21
 * @property string $description
22
 * @property integer $created_at
23
 * @property integer $updated_at
24
 * @property UploadedFile $file
25
 */
26
class AppRelease extends \yii\db\ActiveRecord
27
{
28
    public $file;
29
    const STATUS_PUBLISHED = 1;
30
    const STATUS_UNPUBLISHED = 0;
31
32
    /**
33
     * @inheritdoc
34
     */
35 9
    public static function tableName()
36
    {
37 9
        return '{{%app_release}}';
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 5
    public function rules()
44
    {
45
        return [
46 5
            [['version', 'description'], 'required'],
47
            [['is_forced', 'status'], 'integer'],
48
            [['is_forced', 'status'], 'default', 'value' => 1],
49
            [['description', 'version'], 'string', 'max' => 255],
50
            ['md5', 'string', 'max' => 255, 'on' => ['save']],
51
            [['url'], 'file',
52
                //'extensions' => 'apk',
53
                'skipOnEmpty' => true,
54
                'tooBig' => 'app文件大小不超过120M',
55
                'maxFiles' => 1,
56
                'maxSize' => 120 * 1024 * 1024,
57
                'on' => ['insert', 'update']
58
            ],
59
        ];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 2
    public function attributeLabels()
66
    {
67
        return [
68 2
            'id' => Module::t('apprelease', 'ID'),
69 2
            'version' => Module::t('apprelease', 'version'),
70 2
            'is_forced' => Module::t('apprelease', 'is forced'),
71 2
            'url' => Module::t('apprelease', 'url'),
72 2
            'md5' => Module::t('apprelease', 'MD5'),
73 2
            'status' => Module::t('apprelease', 'status'),
74 2
            'description' => Module::t('apprelease', 'description'),
75 2
            'created_at' => Module::t('apprelease', 'created at'),
76 2
            'updated_at' => Module::t('apprelease', 'updated at'),
77 2
            'file' => Module::t('apprelease', 'File'),
78
        ];
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 10
    public function behaviors()
85
    {
86
        return [
87 10
            'timestamp' => [
88 10
                'class' => TimestampBehavior::className(),
89
            ],
90
            [
91 10
                'class' => UploadBehavior::className(),
92 10
                'attribute' => 'url',
93
                'scenarios' => ['insert', 'update'],
94 10
                'path' => '@frontend/web/uploads/apps',
95 10
                'url' => '@web/uploads/apps',
96
            ],
97
        ];
98
    }
99
100 1
    public function fields()
101
    {
102 1
        $fields = parent::fields();
103 1
        $fields['url'] = function () {
104 1
            $path = str_replace('api/uploads/', '', $this->getUploadUrl('url'));
0 ignored issues
show
Documentation Bug introduced by
The method getUploadUrl does not exist on object<zacksleo\yii2\app...ease\models\AppRelease>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105 1
            if (isset($_ENV['API_HOST'])) {
106
                $url = $_ENV['API_HOST'] . 'files/' . $path;
107
            } else {
108 1
                $url = Url::to(['file/view', 'path' => $path], true);
109
            }
110 1
            return $url;
111
        };
112 1
        unset($fields['id'], $fields['created_at'], $fields['status'], $fields['updated_at']);
113 1
        return $fields;
114
    }
115
}
116