Passed
Push — master ( 5a03f9...8e29e6 )
by Stephen
03:56 queued 01:10
created

ResponseMessages   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelShortName() 0 3 1
A successActionVerb() 0 6 2
A successMessage() 0 15 3
A failMessage() 0 4 1
A successNoun() 0 9 2
1
<?php
2
3
4
namespace Sfneal\CrudModelActions\Utils;
5
6
7
trait ResponseMessages
8
{
9
    /**
10
     * @var string Verb used to describe the action performed on the model (created, updated, etc.
11
     */
12
    private $successActionVerb;
13
14
    /**
15
     * @var string Message to session flash & log after a successful action
16
     */
17
    private $successMessage;
18
19
    /**
20
     * @var string Object which was manipulated (Client, Project, Task, etc...)
21
     */
22
    private $successNoun;
23
24
    /**
25
     * Response message sent on success
26
     *
27
     * @param string|null $message
28
     * @return string
29
     */
30
    protected function successMessage(string $message = null): string {
31
        // Set the message during runtime
32
        if (isset($message)) {
33
            $this->successMessage = $message;
34
        }
35
36
        // Return declared success message
37
        if (isset($this->successMessage)) {
38
            return $this->successMessage;
39
        }
40
41
42
        // Return the default success message
43
        else {
44
            return "{$this->successNoun()} has been {$this->successActionVerb()} successfully.";
45
        }
46
    }
47
48
    /**
49
     * Retrieve the 'noun' to be used as the object of the success message
50
     *
51
     * @param string|null $noun
52
     * @return string
53
     */
54
    protected function successNoun(string $noun = null): string
55
    {
56
        // Set the success noun if passed
57
        if (isset($noun)) {
58
            $this->successNoun = $noun;
59
        }
60
61
        // Return the declared success noun or use default
62
        return $this->successNoun ?? $this->getModelShortName();
63
    }
64
65
    /**
66
     * Action verb to be used in to the success message
67
     *
68
     * @param string|null $verb
69
     * @return string
70
     */
71
    protected function successActionVerb(string $verb = null): string
72
    {
73
        if (isset($verb)) {
74
            $this->successActionVerb = $verb;
75
        }
76
        return strtolower($this->successActionVerb ?? $this->model->mostRecentChange());
77
    }
78
79
    /**
80
     * Response message sent on failure
81
     *
82
     * @return string
83
     */
84
    protected function failMessage(): string
85
    {
86
        $model = strtolower($this->getModelShortName());
87
        return "Error! Unable to save or update the {$model}.";
88
    }
89
90
    /**
91
     * Retrieve the Model class's short name (without namespace)
92
     *
93
     * @return string
94
     */
95
    private function getModelShortName(): string
96
    {
97
        return getClassName($this->model, true, $this->model->getTable());
0 ignored issues
show
Bug introduced by
The function getClassName was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        return /** @scrutinizer ignore-call */ getClassName($this->model, true, $this->model->getTable());
Loading history...
98
    }
99
}
100