Completed
Pull Request — master (#178)
by Corey
02:50
created

CustomBehaviorForm   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
c 1
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 6 1
A attributeLabels() 0 4 1
A rules() 0 6 1
1
<?php
2
namespace site\models;
3
4
use common\models\User;
5
use yii\base\Model;
6
use Yii;
7
8
/**
9
 * custom behavior form
10
 */
11
class CustomBehaviorForm extends Model {
12
  public $name;
13
  public $category_id;
14
15
  private $user;
16
  private $categories;
17
18
  /**
19
   * Creates a form model
20
   *
21
   * @param  object $user
22
   * @param  array  $config name-value pairs that will be used to initialize the object properties
23
   */
24
  public function __construct(\common\interfaces\UserInterface $user, \common\interfaces\CategoryInterface $category, $config = []) {
25
    $this->user = $user;
26
    $this->categories = $category->categories;
0 ignored issues
show
Bug introduced by
Accessing categories on the interface common\interfaces\CategoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
27
    parent::__construct($config);
28
  }
29
30
  /**
31
   * @inheritdoc
32
   * @codeCoverageIgnore
33
   */
34
  public function rules() {
35
    return [
36
      [['name', 'category_id'], 'required'],
37
      ['name', 'filter', 'filter' => 'trim'],
38
      ['category_id', 'integer'],
39
      ['category_id', 'in', 'range'=>array_keys($this->categories)],
40
41
    ];
42
  }
43
44
  /**
45
   * @codeCoverageIgnore
46
   */
47
  public function attributeLabels() {
48
    return [
49
      'name'        => 'Behavior Name',
50
      'category_id' => 'Behavior Category',
51
      ];
52
  }
53
54
55
  public function save() {
56
    $custom_behavior = new \common\models\CustomBehavior();
57
    $custom_behavior->name = $this->name;
58
    $custom_behavior->user_id = $this->user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface common\interfaces\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
59
    $custom_behavior->category_id = $this->category_id;
60
    return $custom_behavior->save();
61
  }
62
}
63