EntityCreateMethods::add()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 26
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 4
nop 0
crap 20
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());
0 ignored issues
show
Bug introduced by
The method addNotice() does not exist on Psr\Log\LoggerInterface. Did you maybe mean notice()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
            $this->addErrorMessage($this->getInvalidFormDataMessage());
50
            return;
51
        } catch (\Exception $caught) {
52
            Log::logger()->addCritical(
0 ignored issues
show
Bug introduced by
The method addCritical() does not exist on Psr\Log\LoggerInterface. Did you maybe mean critical()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
                $caught->getMessage(),
54
                $form->getData()
55
            );
56
            $this->addErrorMessage($this->getGeneralErrorMessage($caught));
57
            return;
58
        }
59
        
60
        $this->addSuccessMessage(
61
            $this->getCreateSuccessMessage(
62
                $this->getUpdateService()->getEntity()
63
            )
64
        );
65
        $this->redirectFromCreated($this->getUpdateService()->getEntity());
66
    }
67
    
68
    /**
69
     * Get the create successful entity message
70
     * 
71
     * @param EntityInterface $entity
72
     * 
73
     * @return string
74
     */
75
    protected function getCreateSuccessMessage(EntityInterface $entity)
76
    {
77
        $singleName = $this->getEntityNameSingular();
78
        $message = "The {$singleName} '%s' was successfully created.";
79
        return sprintf($this->translate($message), $entity);
80
    }
81
82
    /**
83
     * Redirect after successful entity creation
84
     * 
85
     * @param EntityInterface $entity
86
     * 
87
     * @return $this|ControllerInterface|static
88
     */
89
    protected function redirectFromCreated(EntityInterface $entity)
90
    {
91
        return $this->redirect(
92
            $this->getBasePath().'/show/'.$entity->getId()
0 ignored issues
show
Bug introduced by
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
        );
94
    }
95
96
    /**
97
     * Get update service
98
     *
99
     * @return EntityUpdateService
100
     */
101
    abstract public function getUpdateService();
102
103
    /**
104
     * @return FormInterface|EntityForm
105
     */
106
    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...
107
108
    /**
109
     * Get invalid form data message
110
     *
111
     * @param \Exception $caught
112
     *
113
     * @return string
114
     */
115
    abstract protected function getGeneralErrorMessage(\Exception $caught);
116
117
    /**
118
     * Get invalid form data message
119
     *
120
     * @return string
121
     */
122
    abstract protected function getInvalidFormDataMessage();
123
124
    /**
125
     * Sets a value to be used by render
126
     *
127
     * The key argument can be an associative array with values to be set
128
     * or a string naming the passed value. If an array is given then the
129
     * value will be ignored.
130
     *
131
     * Those values must be set in the request attributes so they can be used
132
     * latter by any other middle ware in the stack.
133
     *
134
     * @param string|array $key
135
     * @param mixed        $value
136
     *
137
     * @return ControllerInterface
138
     */
139
    abstract public function set($key, $value = null);
140
141
    /**
142
     * Redirects the flow to another route/path
143
     *
144
     * @param string $path the route or path to redirect to
145
     *
146
     * @return ControllerInterface|self|$this
147
     */
148
    abstract public function redirect($path);
149
150
    /**
151
     * Add an error flash message
152
     *
153
     * @param string $message
154
     * @return self
155
     */
156
    abstract public function addErrorMessage($message);
157
158
    /**
159
     * Add a success flash message
160
     *
161
     * @param string $message
162
     * @return self
163
     */
164
    abstract public function addSuccessMessage($message);
165
166
    /**
167
     * Returns the translation for the provided message
168
     *
169
     * @param string $message
170
     * @param string $domain
171
     * @param string $locale
172
     *
173
     * @return string
174
     */
175
    abstract public function translate(
176
        $message, $domain = null, $locale = null
177
    );
178
179
    /**
180
     * Get entity singular name used on controller actions
181
     *
182
     * @return string
183
     */
184
    abstract protected function getEntityNameSingular();
185
}