Passed
Pull Request — master (#178)
by Corey
02:57
created

CustomBehaviorForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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
   */
33
  public function rules() {
34
    return [
35
      [['name', 'category_id'], 'required'],
36
      ['name', 'filter', 'filter' => 'trim'],
37
      ['category_id', 'integer'],
38
      ['category_id', 'in', 'range'=>array_keys($this->categories)],
39
40
    ];
41
  }
42
43
  /**
44
   * @codeCoverageIgnore
45
   */
46
  public function attributeLabels() {
47
    return [
48
      'name'        => 'Behavior Name',
49
      'category_id' => 'Behavior Category',
50
      ];
51
  }
52
53
54
  public function save() {
55
    $custom_behavior = Yii::$container->get(\common\interfaces\CustomBehaviorInterface::class);
56
    $custom_behavior->name = $this->name;
57
    $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...
58
    $custom_behavior->category_id = $this->category_id;
59
    return $custom_behavior->save();
60
  }
61
}
62