Completed
Push — master ( c8924f...ae2ef6 )
by Filipe
02:36
created

FormAwareMethods::setUpdateService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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\Controller;
11
12
use Slick\Form\FormInterface;
13
use Slick\Mvc\Form\EntityForm;
14
use Slick\Mvc\Service\Entity\EntityUpdateService;
15
use Slick\Orm\Entity;
16
17
/**
18
 * Form Aware Methods
19
 * 
20
 * @package Slick\Mvc\Controller
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
trait FormAwareMethods
24
{
25
26
    /**
27
     * @var EntityUpdateService
28
     */
29
    protected $updateService;
30
31
    /**
32
     * @return FormInterface|EntityForm
33
     */
34
    abstract function getForm();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
36
    /**
37
     * Get invalid form data message
38
     *
39
     * @return string
40
     */
41
    protected function getInvalidFormDataMessage()
42
    {
43
        $singleName = $this->getEntityNameSingular();
44
        $message = "The {$singleName} could not be saved. Please check the " .
45
            "errors and try again.";
46
        return $this->translate($message);
47
    }
48
49
    /**
50
     * Get invalid form data message
51
     *
52
     * @param \Exception $caught
53
     *
54
     * @return string
55
     */
56
    protected function getGeneralErrorMessage(\Exception $caught)
57
    {
58
        $singleName = $this->getEntityNameSingular();
59
        $message = "There was an error when saving {$singleName} data: %s";
60
        return sprintf($this->translate($message), $caught->getMessage());
61
    }
62
63
    /**
64
     * Get entity singular name used on controller actions
65
     *
66
     * @return string
67
     */
68
    abstract protected function getEntityNameSingular();
69
70
    /**
71
     * Get update service
72
     *
73
     * @return EntityUpdateService
74
     */
75
    public function getUpdateService()
76
    {
77
        if (null == $this->updateService) {
78
            $this->setUpdateService(
79
                new EntityUpdateService($this->getNewEntity())
80
            );
81
        }
82
        return $this->updateService;
83
    }
84
85
    /**
86
     * Set update service
87
     *
88
     * @param EntityUpdateService $updateService
89
     *
90
     * @return EntityCreateMethods
0 ignored issues
show
Comprehensibility Bug introduced by
The return type EntityCreateMethods is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
91
     */
92
    public function setUpdateService(EntityUpdateService $updateService)
93
    {
94
        $this->updateService = $updateService;
95
        return $this;
96
    }
97
98
    /**
99
     * Get a new entity
100
     *
101
     * @return Entity
102
     */
103
    protected function getNewEntity()
104
    {
105
        $class = $this->getEntityClassName();
106
        return new $class();
107
    }
108
109
    /**
110
     * Gets the entity FQ class name
111
     *
112
     * @return string
113
     */
114
    abstract public function getEntityClassName();
115
116
    /**
117
     * Returns the translation for the provided message
118
     *
119
     * @param string $message
120
     * @param string $domain
121
     * @param string $locale
122
     *
123
     * @return string
124
     */
125
    abstract public function translate(
126
        $message, $domain = null, $locale = null
127
    );
128
}