Completed
Push — master ( c25a09...ded069 )
by Ryan
159:32 queued 132:18
created

SetErrorMessages::handle()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 6.7272
cc 7
eloc 12
nc 5
nop 3
1
<?php namespace Anomaly\Streams\Platform\Ui\Form\Command;
2
3
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
4
use Anomaly\Streams\Platform\Message\MessageBag;
5
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
6
use Illuminate\Contracts\Bus\SelfHandling;
7
use Illuminate\Http\Request;
8
use Illuminate\Translation\Translator;
9
10
/**
11
 * Class SetErrorMessages
12
 *
13
 * @link          http://anomaly.is/streams-platform
14
 * @author        AnomalyLabs, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 * @package       Anomaly\Streams\Platform\Ui\Form\Command
17
 */
18
class SetErrorMessages implements SelfHandling
19
{
20
21
    /**
22
     * The form builder.
23
     *
24
     * @var FormBuilder
25
     */
26
    protected $builder;
27
28
    /**
29
     * Create a new SetErrorMessages instance.
30
     *
31
     * @param FormBuilder $builder
32
     */
33
    public function __construct(FormBuilder $builder)
34
    {
35
        $this->builder = $builder;
36
    }
37
38
    /**
39
     * Handle the command.
40
     *
41
     * @param Request $request
42
     * @param MessageBag $messages
43
     * @param Translator $translator
44
     */
45
    public function handle(Request $request, MessageBag $messages, Translator $translator)
46
    {
47
        if ($this->builder->isAjax()) {
48
            return;
49
        }
50
51
        $errors = $this->builder->getFormErrors();
52
53
        $messages->error($errors->all());
54
55
        if ($request->segment(1) == 'admin' && ($stream = $this->builder->getFormStream()) && $stream->isTrashable()) {
56
57
            /* @var AssignmentInterface $assignment */
58
            foreach ($stream->getUniqueAssignments() as $assignment) {
59
                if ($this->builder->hasFormError($assignment->getFieldSlug())) {
60
                    $messages->warning(
61
                        $translator->trans(
62
                            'streams::validation.unique_trash',
63
                            [
64
                                'attribute' => '"' . $translator->trans($assignment->getFieldName()) . '"'
65
                            ]
66
                        )
67
                    );
68
                }
69
            }
70
        }
71
    }
72
}
73