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

EntityCreateMethods::getCreateSuccessMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
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\Common\Log;
13
use Slick\Form\FormInterface;
14
use Slick\Mvc\ControllerInterface;
15
use Slick\Mvc\Exception\Service\InvalidFormDataException;
16
use Slick\Mvc\Form\EntityForm;
17
use Slick\Mvc\Service\Entity\EntityUpdateService;
18
use Slick\Orm\Entity;
19
use Slick\Orm\EntityInterface;
20
21
/**
22
 * Entity Create Methods
23
 * 
24
 * @package Slick\Mvc\Controller
25
 * @author  Filipe Silva <[email protected]>
26
 */
27
trait EntityCreateMethods
28
{
29
30
    /**
31
     * Handle the add entity request
32
     */
33
    public function add()
34
    {
35
        $form = $this->getForm();
36
        $this->set(compact('form'));
37
        
38
        if (!$form->wasSubmitted()) {
39
            return;
40
        }
41
        
42
        try {
43
            $this->getUpdateService()
44
                ->setForm($form)
0 ignored issues
show
Compatibility introduced by
$form of type object<Slick\Form\FormInterface> is not a sub-type of object<Slick\Mvc\Form\EntityForm>. It seems like you assume a concrete implementation of the interface Slick\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
45
                ->update();
46
            ;
47
        } catch (InvalidFormDataException $caught) {
48
            Log::logger()->addNotice($caught->getMessage(), $form->getData());
49
            $this->addErrorMessage($this->getInvalidFormDataMessage());
50
            return;
51
        } catch (\Exception $caught) {
52
            Log::logger()->addCritical(
53
                $caught->getMessage(),
54
                $form->getData()
55
            );
56
            $this->addErrorMessage($this->getGeneralErrorMessage($caught));
57
            return;
58
        }
59
        
60
        $this->addSuccessMessage(
61
            $this->getCreateSuccessMessage($this->getUpdateService()->getEntity())
62
        );
63
    }
64
    
65
    /**
66
     * Get the create successful entity message
67
     * 
68
     * @param EntityInterface $entity
69
     * 
70
     * @return string
71
     */
72
    protected function getCreateSuccessMessage(EntityInterface $entity)
73
    {
74
        $singleName = $this->getEntityNameSingular();
75
        $message = "The {$singleName} '%s' was successfully created.";
76
        return sprintf($this->translate($message), $entity);
77
    }
78
79
    /**
80
     * Get update service
81
     *
82
     * @return EntityUpdateService
83
     */
84
    abstract public function getUpdateService();
85
86
    /**
87
     * @return FormInterface|EntityForm
88
     */
89
    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...
90
91
    /**
92
     * Get invalid form data message
93
     *
94
     * @param \Exception $caught
95
     *
96
     * @return string
97
     */
98
    abstract protected function getGeneralErrorMessage(\Exception $caught);
99
100
    /**
101
     * Get invalid form data message
102
     *
103
     * @return string
104
     */
105
    abstract protected function getInvalidFormDataMessage();
106
107
    /**
108
     * Sets a value to be used by render
109
     *
110
     * The key argument can be an associative array with values to be set
111
     * or a string naming the passed value. If an array is given then the
112
     * value will be ignored.
113
     *
114
     * Those values must be set in the request attributes so they can be used
115
     * latter by any other middle ware in the stack.
116
     *
117
     * @param string|array $key
118
     * @param mixed        $value
119
     *
120
     * @return ControllerInterface
121
     */
122
    abstract public function set($key, $value = null);
123
124
    /**
125
     * Redirects the flow to another route/path
126
     *
127
     * @param string $path the route or path to redirect to
128
     *
129
     * @return ControllerInterface|self|$this
130
     */
131
    abstract public function redirect($path);
132
133
    /**
134
     * Add an error flash message
135
     *
136
     * @param string $message
137
     * @return self
138
     */
139
    abstract public function addErrorMessage($message);
140
141
    /**
142
     * Add a success flash message
143
     *
144
     * @param string $message
145
     * @return self
146
     */
147
    abstract public function addSuccessMessage($message);
148
149
    /**
150
     * Returns the translation for the provided message
151
     *
152
     * @param string $message
153
     * @param string $domain
154
     * @param string $locale
155
     *
156
     * @return string
157
     */
158
    abstract public function translate(
159
        $message, $domain = null, $locale = null
160
    );
161
162
    /**
163
     * Get entity singular name used on controller actions
164
     *
165
     * @return string
166
     */
167
    abstract protected function getEntityNameSingular();
168
}