Completed
Push — master ( 3fa4d0...3f005a )
by Tõnis
02:22
created

Field::getName()   A

Complexity

Conditions 3
Paths 3

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 3
eloc 5
nc 3
nop 0
1
<?php
2
namespace dameter\abstracts\models;
3
4
use yii\base\InvalidConfigException;
5
use yii\base\Model;
6
7
/**
8
 * Class Field describes a column in responses data table
9
 *
10
 * @property string $type technical type for DB
11
 * @property string $name fieldName - actual column name in DB
12
 * @property string $tokenFieldCollation the suitable collation for token field
13
 *
14
 * @package dameter\abstracts\models
15
 * @author Tõnis Ormisson <[email protected]>
16
 */
17
class Field extends Model
18
{
19
20
    const TYPE_STRING = 'string';
21
    const TYPE_CHAR = 'char';
22
    const TYPE_INTEGER = 'integer';
23
    const TYPE_DOUBLE = 'double';
24
    const TYPE_DATE = 'date';
25
    const TYPE_DATETIME = 'datetime';
26
27
    const DEFAULT_STRING_LENGTH = 5;
28
    const DEFAULT_DOUBLE_LENGTH = 30;
29
    const DEFAULT_DOUBLE_DECIMALS = 10;
30
31
    const SYSFIELD_ID = 'id';
32
    const SYSFIELD_TOKEN = 'token';
33
    const SYSFIELD_SEED = 'seed';
34
    const SYSFIELD_STARTLANGUAGE = 'startlanguage';
35
    const SYSFIELD_STARTDATE = 'startdate';
36
    const SYSFIELD_SUBMITDATE = 'submitdate';
37
    const SYSFIELD_DATESTAMP = 'datestamp';
38
    const SYSFIELD_LASTPAGE = 'lastpage';
39
    const SYSFIELD_REFURL = 'refurl';
40
    const SYSFIELD_IP_ADDRESS = 'ipaddr';
41
42
43
    /** @var BaseQuestion */
44
    public $question;
45
46
    /** @var string $systemFieldName Name for non-question field */
47
    public $systemFieldName;
48
49
50
    /** @var string */
51
    public $id;
52
53
    /** @var bool $isCommentField  */
54
    public $isCommentField = false;
55
56
    /** @var bool $isOtherField  */
57
    public $isOtherField = false;
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function init()
63
    {
64
        parent::init();
65
        if (!($this->question instanceof BaseQuestion)) {
0 ignored issues
show
introduced by
$this->question is always a sub-type of dameter\abstracts\models\BaseQuestion. If $this->question can have other possible types, add them to src/models/Field.php:43.
Loading history...
66
            throw new InvalidConfigException();
67
        }
68
    }
69
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function attributeLabels()
75
    {
76
        return [
77
            'name' => \Yii::t('dmadmin', "Name"),
78
        ];
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getType()
85
    {
86
        if (!empty($this->question)) {
87
            if ($this->isCommentField || $this->isOtherField) {
88
                return "text";
89
            }
90
            return $this->question->questionType->fieldType;
91
        } else {
92
            return $this->systemFieldType();
93
        }
94
    }
95
96
97
    /**
98
     * @return string
99
     * @throws \Exception
100
     */
101
    private function systemFieldType()
102
    {
103
        switch ($this->systemFieldName) {
104
            case self::SYSFIELD_ID:
105
                return "pk";
106
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
107
            case self::SYSFIELD_SEED:
108
                return "string(31)";
109
                break;
110
            case self::SYSFIELD_STARTLANGUAGE:
111
                return "string(31) string(20) NOT NULL";
112
                break;
113
            case self::SYSFIELD_STARTDATE:
114
            case self::SYSFIELD_DATESTAMP:
115
                return "datetime NOT NULL";
116
                break;
117
            case self::SYSFIELD_SUBMITDATE:
118
                return "datetime";
119
                break;
120
            case self::SYSFIELD_LASTPAGE:
121
                return "integer";
122
                break;
123
            case self::SYSFIELD_TOKEN:
124
                return "string(36) " . $this->tokenFieldCollation;
125
                break;
126
            case self::SYSFIELD_REFURL:
127
                return "text";
128
                break;
129
            case self::SYSFIELD_IP_ADDRESS:
130
                return "string(45)";
131
                break;
132
            default:
133
                throw new \Exception("Undefined system column {$this->systemFieldName}");
134
        }
135
    }
136
137
    /**
138
     * @return string
139
     * @throws \Exception
140
     */
141
    public function getName()
142
    {
143
        if (!empty($this->question)) {
144
            return $this->question->code;
145
        } elseif (!empty($this->systemFieldName)) {
146
            return $this->systemFieldName;
147
        }
148
        throw new \Exception('Either question or systemFieldName must be defined for Field');
149
    }
150
151
152
153
    /**
154
     * @return string
155
     * @throws \Exception
156
     */
157
    public function getTokenFieldCollation()
158
    {
159
        $driverName = \Yii::$app->db->driverName;
160
        if ($driverName === 'mysql') {
161
            return " COLLATE 'utf8mb4_bin'";
162
        }
163
        if ($driverName == 'sqlsrv' || $driverName == 'dblib' || $driverName == 'mssql') {
164
            return " COLLATE SQL_Latin1_General_CP1_CS_AS";
165
        }
166
        throw new \Exception('Unsupported database engine ' . $driverName);
167
    }
168
169
170
171
}