Completed
Push — master ( 2f5afb...f40faa )
by Filipe
02:44
created

EntityUpdateService::setForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service\Entity;
11
12
use Slick\Mvc\Exception\Service\InvalidFormDataException;
13
use Slick\Mvc\Form\EntityForm;
14
use Slick\Mvc\Service\EntityServiceInterface;
15
use Slick\Orm\Entity;
16
17
/**
18
 * Entity Update Service
19
 * 
20
 * @package Slick\Mvc\Service\Entity
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class EntityUpdateService extends AbstractEntityService implements
24
    EntityServiceInterface
25
{
26
27
    /**
28
     * @var array
29
     */
30
    protected $data;
31
32
    /**
33
     * @var Entity
34
     */
35
    protected $entity;
36
37
    /**
38
     * @var EntityForm
39
     */
40
    protected $form;
41
    
42
    /**
43
     * Entity Update Service need an entity.
44
     * 
45
     * @param Entity $entity
46
     */
47
    public function __construct(Entity $entity)
48
    {
49
        $this->setEntity($entity);
50
    }
51
52
    /**
53
     * Updates entity with provided data
54
     */
55
    public function update()
56
    {
57
        $this->entity->save($this->getData());
58
    }
59
60
    /**
61
     * Get current data to be updated
62
     * 
63
     * @return array
64
     */
65
    public function getData()
66
    {
67
        return $this->data;
68
    }
69
70
    /**
71
     * Set data for entity update
72
     * 
73
     * @param array $data
74
     * 
75
     * @return EntityUpdateService
76
     */
77
    public function setData(array $data)
78
    {
79
        $this->data = $data;
80
        return $this;
81
    }
82
83
    /**
84
     * Get entity for
85
     * 
86
     * @return EntityForm
87
     */
88
    public function getForm()
89
    {
90
        return $this->form;
91
    }
92
93
    /**
94
     * Set entity for
95
     * 
96
     * @param EntityForm $form
97
     * 
98
     * @return EntityUpdateService
99
     * 
100
     * @throws InvalidFormDataException If the provided form has invalid data
101
     */
102
    public function setForm(EntityForm $form)
103
    {
104
        if (!$form->isValid()) {
105
            throw new InvalidFormDataException(
106
                "Submitted form data is not valid."
107
            );
108
        }
109
        $this->setData($form->getData());
110
        $this->form = $form;
111
        return $this;
112
    }
113
    
114
}