Passed
Push — master ( 27e589...85326f )
by Gabriel
15:30 queued 12s
created

HasModelTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 26
c 1
b 0
f 0
dl 0
loc 116
rs 10
ccs 0
cts 39
cp 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A saveModel() 0 3 1
A getModelMessage() 0 3 1
A getDataFromModel() 0 7 4
A getModelManager() 0 3 1
A addModelError() 0 3 1
A addInputModelError() 0 3 1
A getModel() 0 3 1
A initialized() 0 4 1
A process() 0 4 1
A getModelLabel() 0 3 1
A _addModelFormMessage() 0 5 1
A setModel() 0 4 1
A saveToModel() 0 6 3
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
    public function initialized()
19
    {
20
        parent::initialized();
21
        $this->getDataFromModel();
22
    }
23
24
    /**
25
     * @param $name
26
     * @return $this
27
     */
28
    public function addModelError($name)
29
    {
30
        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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return $this->/** @scrutinizer ignore-call */ addError($this->getModelMessage($name));
Loading history...
31
    }
32
33
    /**
34
     * @param $name
35
     * @param array $variables
36
     * @return mixed
37
     */
38
    public function getModelMessage($name, $variables = [])
39
    {
40
        return $this->getModelManager()->getMessage('form.'.$name, $variables);
41
    }
42
43
    /**
44
     * @return Record
45
     */
46
    public function getModel()
47
    {
48
        return $this->model;
49
    }
50
51
    /**
52
     * @param Record $model
53
     * @return $this
54
     */
55
    public function setModel(Record $model)
56
    {
57
        $this->model = $model;
58
        return $this;
59
    }
60
61
    protected function getDataFromModel()
62
    {
63
        $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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $elements = $this->getElements();
Loading history...
64
        if (is_array($elements)) {
65
            foreach ($elements as $name => $element) {
66
                if (isset($this->getModel()->{$name})) {
67
                    $element->getData($this->getModel()->{$name}, 'model');
68
                }
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param $input
75
     * @param $name
76
     * @param array $variables
77
     * @return $this
78
     */
79
    public function addInputModelError($input, $name, $variables = [])
80
    {
81
        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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return $this->/** @scrutinizer ignore-call */ getElement($input)->addError($this->getModelMessage($name, $variables));
Loading history...
82
    }
83
84
    /**
85
     * @param $name
86
     * @return mixed
87
     */
88
    public function getModelLabel($name)
89
    {
90
        return $this->getModelManager()->getLabel($name);
91
    }
92
93
    /**
94
     * @return \Nip\Records\RecordManager
95
     */
96
    public function getModelManager()
97
    {
98
        return $this->getModel()->getManager();
99
    }
100
101
    public function process()
102
    {
103
        $this->saveToModel();
104
        $this->saveModel();
105
    }
106
107
    public function saveToModel()
108
    {
109
        $elements = $this->getElements();
110
        if (is_array($elements)) {
111
            foreach ($elements as $name => $element) {
112
                $this->getModel()->{$name} = $element->getValue('model');
113
            }
114
        }
115
    }
116
117
    public function saveModel()
118
    {
119
        $this->getModel()->save();
120
    }
121
122
    protected function _addModelFormMessage($form, $model)
123
    {
124
        $this->_messageTemplates[$form] = $this->getModelMessage($model);
0 ignored issues
show
Bug Best Practice introduced by
The property _messageTemplates does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
125
126
        return $this;
127
    }
128
}
129