Completed
Push — master ( 37b943...607dad )
by Filipe
06:01
created

EntityUpdateService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 92
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 4 1
A getData() 0 4 1
A setData() 0 5 1
A getForm() 0 4 1
A setForm() 0 11 2
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 6
    public function __construct(Entity $entity)
48
    {
49 6
        $this->setEntity($entity);
50 6
    }
51
52
    /**
53
     * Updates entity with provided data
54
     */
55 2
    public function update()
56
    {
57 2
        $this->entity->save($this->getData());
58 2
    }
59
60
    /**
61
     * Get current data to be updated
62
     * 
63
     * @return array
64
     */
65 4
    public function getData()
66
    {
67 4
        return $this->data;
68
    }
69
70
    /**
71
     * Set data for entity update
72
     * 
73
     * @param array $data
74
     * 
75
     * @return EntityUpdateService
76
     */
77 2
    public function setData(array $data)
78
    {
79 2
        $this->data = $data;
80 2
        return $this;
81
    }
82
83
    /**
84
     * Get entity for
85
     * 
86
     * @return EntityForm
87
     */
88 2
    public function getForm()
89
    {
90 2
        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 4
    public function setForm(EntityForm $form)
103
    {
104 4
        if (!$form->isValid()) {
105 2
            throw new InvalidFormDataException(
106 1
                "Submitted form data is not valid."
107 1
            );
108
        }
109 2
        $this->setData($form->getData());
110 2
        $this->form = $form;
111 2
        return $this;
112
    }
113
    
114
}