Passed
Push — master ( d8e1df...eb8037 )
by Razon
02:41
created

Form::handle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 15
rs 9.9666
c 1
b 0
f 0
ccs 7
cts 9
cp 0.7778
cc 4
nc 6
nop 0
crap 4.1755
1
<?php
2
namespace App\Form;
3
4
use yii\base\Model;
5
6
abstract class Form extends Model
7
{
8
    /**
9
     * @return self|mixed return model itself if has errors, otherwise returns handled's result.
10
     * @throws \Throwable rethrows exception if no error specified.
11
     */
12 3
    public function handle()
13
    {
14
        try {
15 3
            if (!$this->validate()) {
16 2
                return $this;
17
            }
18
19 1
            $data = $this->handleInternal();
20
            return $data;
21 1
        } catch (\Throwable $e) {
22 1
            if ($this->hasErrors()) {
23
                return $this;
24
            }
25
26 1
            throw $e;
27
        }
28
    }
29
    
30
    /**
31
     * @return mixed
32
     */
33
    abstract protected function handleInternal();
34
}