Test Setup Failed
Push — master ( 6d6991...a81a2e )
by Gabriel
02:14
created

HasModelTrait::addInputModelError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip\Records\AbstractModels\Record;
6
7
/**
8
 * Trait HasModelTrait
9
 * @package Nip\Form\Traits
10
 */
11
trait HasModelTrait
12
{
13
    /**
14
     * @var Record
15
     */
16
    protected $model;
17
18
    /**
19
     * @param $name
20
     * @return $this
21
     */
22
    public function addModelError($name)
23
    {
24
        return $this->addError($this->getModelMessage($name));
0 ignored issues
show
Bug introduced by
It seems like addError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
    }
26
27
    /**
28
     * @param $name
29
     * @param array $variables
30
     * @return mixed
31
     */
32
    public function getModelMessage($name, $variables = [])
33
    {
34
        return $this->getModelManager()->getMessage('form.'.$name, $variables);
35
    }
36
37
    /**
38
     * @return Record
39
     */
40
    public function getModel()
41
    {
42
        return $this->model;
43
    }
44
45
    /**
46
     * @param Record $model
47
     * @return $this
48
     */
49
    public function setModel(Record $model)
50
    {
51
        $this->model = $model;
52
        $this->getDataFromModel();
53
54
        return $this;
55
    }
56
57
    protected function getDataFromModel()
58
    {
59
        $elements = $this->getElements();
0 ignored issues
show
Bug introduced by
It seems like getElements() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
        if (is_array($elements)) {
61
            foreach ($elements as $name => $element) {
62
                if (isset($this->getModel()->{$name})) {
63
                    $element->getData($this->getModel()->{$name}, 'model');
64
                }
65
            }
66
        }
67
    }
68
69
    /**
70
     * @param $input
71
     * @param $name
72
     * @param array $variables
73
     * @return $this
74
     */
75
    public function addInputModelError($input, $name, $variables = [])
76
    {
77
        return $this->getElement($input)->addError($this->getModelMessage($name, $variables));
0 ignored issues
show
Bug introduced by
It seems like getElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
    }
79
80
    /**
81
     * @param $name
82
     * @return mixed
83
     */
84
    public function getModelLabel($name)
85
    {
86
        return $this->getModelManager()->getLabel($name);
87
    }
88
89
    /**
90
     * @return \Nip\Records\RecordManager
91
     */
92
    public function getModelManager()
93
    {
94
        return $this->getModel()->getManager();
95
    }
96
97
    public function process()
98
    {
99
        $this->saveToModel();
100
        $this->saveModel();
101
    }
102
103
    public function saveToModel()
104
    {
105
        $elements = $this->getElements();
0 ignored issues
show
Bug introduced by
It seems like getElements() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
106
        if (is_array($elements)) {
107
            foreach ($elements as $name => $element) {
108
                $this->getModel()->{$name} = $element->getValue('model');
109
            }
110
        }
111
    }
112
113
    public function saveModel()
114
    {
115
        $this->getModel()->save();
116
    }
117
118
    protected function _addModelFormMessage($form, $model)
119
    {
120
        $this->_messageTemplates[$form] = $this->getModelMessage($model);
0 ignored issues
show
Bug introduced by
The property _messageTemplates does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
121
122
        return $this;
123
    }
124
}
125