Completed
Pull Request — master (#21)
by Daniel
03:27 queued 01:17
created

RowData   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromObject() 0 14 2
A fromForm() 0 8 1
A getForm() 0 10 2
A getObject() 0 4 1
A getFormObject() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Symfony\Component\Form\FormView;
8
9
/**
10
 * TODO: Tests for this class.
11
 */
12
final class RowData
13
{
14
    private $object;
15
    private $form;
16
17
    private function __construct()
18
    {
19
    }
20
21
    public static function fromObject($object): RowData
22
    {
23
        if (!is_object($object)) {
24
            throw new \InvalidArgumentException(sprintf(
25
                'Object must be an object, got: "%s"',
26
                gettype($object)
27
            ));
28
        }
29
30
        $instance = new self();
31
        $instance->object = $object;
32
33
        return $instance;
34
    }
35
36
    public static function fromForm(FormView $form): RowData
37
    {
38
        $instance = new self();
39
        $instance->form = $form;
40
        $instance->object = $instance->getFormObject();
41
42
        return $instance;
43
    }
44
45
    public function getForm(): FormView
46
    {
47
        if (!$this->form) {
48
            throw new \InvalidArgumentException(
49
                'No form was set on row data.'
50
            );
51
        }
52
53
        return $this->form;
54
    }
55
56
    public function getObject()
57
    {
58
        return $this->object;
59
    }
60
61
    private function getFormObject()
62
    {
63
        if (!isset($this->form->vars['data'])) {
64
            throw new \RuntimeException(sprintf(
65
                'Expected "data" to be set on the form view. Actual view variables: "%s"',
66
                implode('", "', array_keys($this->form->vars))
67
            ));
68
        }
69
70
        return $this->form->vars['data'];
71
    }
72
}
73